ListItem









How do I use List Item
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: ScrollSnap.cs Copy
107         void Awake()
108         {
109             lerp = false;
110
111             scrollRect = gameObject.GetComponent();
112             scrollRectTransform = gameObject.GetComponent();
113             listContainerTransform = scrollRect.content;
114             listContainerRectTransform = listContainerTransform.GetComponent();
115
116             rectTransform = listContainerTransform.gameObject.GetComponent();
117             UpdateListItemsSize();
118             UpdateListItemPositions();
119
120             PageChanged(CurrentPage());
121
122             if (nextButton)
123             {
124                 nextButton.GetComponent
File name: ScrollSnap.cs Copy
144         public void UpdateListItemsSize()
145         {
146             float size = 0;
147             float currentSize = 0;
148             if (direction == ScrollSnap.ScrollDirection.Horizontal)
149             {
150                 size = scrollRectTransform.rect.width / itemsVisibleAtOnce;
151                 currentSize = listContainerRectTransform.rect.width / itemsCount;
152             }
153             else
154             {
155                 size = scrollRectTransform.rect.height / itemsVisibleAtOnce;
156                 currentSize = listContainerRectTransform.rect.height / itemsCount;
157             }
158
159             itemSize = size;
160
161             if (linkScrolrectScrollSensitivity)
162             {
163                 scrollRect.scrollSensitivity = itemSize;
164             }
165
166             if (autoLayoutItems && currentSize != size && itemsCount > 0)
167             {
168                 if (direction == ScrollSnap.ScrollDirection.Horizontal)
169                 {
170                     foreach (var tr in listContainerTransform)
171                     {
172                         GameObject child = ((Transform)tr).gameObject;
173                         if (child.activeInHierarchy)
174                         {
175                             var childLayout = child.GetComponent();
176
177                             if (childLayout == null)
178                             {
179                                 childLayout = child.AddComponent();
180                             }
181
182                             childLayout.minWidth = itemSize;
183                         }
184                     }
185                 }
186                 else
187                 {
188                     foreach (var tr in listContainerTransform)
189                     {
190                         GameObject child = ((Transform)tr).gameObject;
191                         if (child.activeInHierarchy)
192                         {
193                             var childLayout = child.GetComponent();
194
195                             if (childLayout == null)
196                             {
197                                 childLayout = child.AddComponent();
198                             }
199
200                             childLayout.minHeight = itemSize;
201                         }
202                     }
203                 }
204             }
205         }
File name: ScrollSnap.cs Copy
207         public void UpdateListItemPositions()
208         {
209             if (!listContainerRectTransform.rect.size.Equals(listContainerCachedSize))
210             {
211                 // checking how many children of list are active
212                 int activeCount = 0;
213
214                 foreach (var tr in listContainerTransform)
215                 {
216                     if (((Transform)tr).gameObject.activeInHierarchy)
217                     {
218                         activeCount++;
219                     }
220                 }
221
222                 // if anything changed since last check reinitialize anchors list
223                 itemsCount = 0;
224                 Array.Resize(ref pageAnchorPositions, activeCount);
225
226                 if (activeCount > 0)
227                 {
228                     pages = Mathf.Max(activeCount - itemsVisibleAtOnce + 1, 1);
229
230                     if (direction == ScrollDirection.Horizontal)
231                     {
232                         // looking for list spanning range min/max
233                         scrollRect.horizontalNormalizedPosition = 0;
234                         listContainerMaxPosition = listContainerTransform.localPosition.x;
235                         scrollRect.horizontalNormalizedPosition = 1;
236                         listContainerMinPosition = listContainerTransform.localPosition.x;
237
238                         listContainerSize = listContainerMaxPosition - listContainerMinPosition;
239
240                         for (var i = 0; i < pages; i++)
241                         {
242                             pageAnchorPositions[i] = new Vector3(
243                                 listContainerMaxPosition - itemSize * i,
244                                 listContainerTransform.localPosition.y,
245                                 listContainerTransform.localPosition.z
246                             );
247                         }
248                     }
249                     else
250                     {
251                         //Debug.Log ("-------------looking for list spanning range----------------");
252                         // looking for list spanning range
253                         scrollRect.verticalNormalizedPosition = 1;
254                         listContainerMinPosition = listContainerTransform.localPosition.y;
255                         scrollRect.verticalNormalizedPosition = 0;
256                         listContainerMaxPosition = listContainerTransform.localPosition.y;
257
258                         listContainerSize = listContainerMaxPosition - listContainerMinPosition;
259
260                         for (var i = 0; i < pages; i++)
261                         {
262                             pageAnchorPositions[i] = new Vector3(
263                                 listContainerTransform.localPosition.x,
264                                 listContainerMinPosition + itemSize * i,
265                                 listContainerTransform.localPosition.z
266                             );
267                         }
268                     }
269
270                     UpdateScrollbar(linkScrolbarSteps);
271                     startingPage = Mathf.Min(startingPage, pages);
272                     ResetPage();
273                 }
274
275                 if (itemsCount != activeCount)
276                 {
277                     PageChanged(CurrentPage());
278                 }
279
280                 itemsCount = activeCount;
281                 listContainerCachedSize.Set(listContainerRectTransform.rect.size.x, listContainerRectTransform.rect.size.y);
282             }
283
284         }
File name: ScrollSnap.cs Copy
336         void LateUpdate()
337         {
338             UpdateListItemsSize();
339             UpdateListItemPositions();
340
341             if (lerp)
342             {
343                 UpdateScrollbar(false);
344
345                 listContainerTransform.localPosition = Vector3.Lerp(listContainerTransform.localPosition, lerpTarget, 7.5f * Time.deltaTime);
346
347                 if (Vector3.Distance(listContainerTransform.localPosition, lerpTarget) < 0.001f)
348                 {
349                     listContainerTransform.localPosition = lerpTarget;
350                     lerp = false;
351
352                     UpdateScrollbar(linkScrolbarSteps);
353                 }
354
355                 //change the info bullets at the bottom of the screen. Just for visual effect
356                 if (Vector3.Distance(listContainerTransform.localPosition, lerpTarget) < 10f)
357                 {
358                     PageChanged(CurrentPage());
359                 }
360             }
361
362             if (fastSwipeTimer)
363             {
364                 fastSwipeCounter++;
365             }
366         }
File name: ScrollSnap.cs Copy
372         public void NextScreen()
373         {
374             UpdateListItemPositions();
375
376             if (CurrentPage() < pages - 1)
377             {
378                 lerp = true;
379                 lerpTarget = pageAnchorPositions[CurrentPage() + 1];
380
381                 PageChanged(CurrentPage() + 1);
382             }
383         }
File name: ScrollSnap.cs Copy
386         public void PreviousScreen()
387         {
388             UpdateListItemPositions();
389
390             if (CurrentPage() > 0)
391             {
392                 lerp = true;
393                 lerpTarget = pageAnchorPositions[CurrentPage() - 1];
394
395                 PageChanged(CurrentPage() - 1);
396             }
397         }

Download file with original file name:ListItem

ListItem 158 lượt xem

Gõ tìm kiếm nhanh...