Dynamic draggable list Dynmiclist,scrollview

Source: Internet
Author: User

Although this is achieved on the 2D toolkit, the following will try to write a common point.
The so-called dynamiclist, refers to if there is a scrollview, the inside of the item too much. You need to instantiate a number of item and each frame needs to process the data for each item. This time, in order to reduce the performance of the pressure will be made into a circular list, through the last position with the first item exchange, only instantiate more than one item to repeat the required data. Other words. There are 100 of data. Implemented with 10 item.
Let's declare a few variables here. float _itemstride;//width of each itemList<data> _itemdatalist;//item's Data listList<transform> _cachedcontentitems;//Now in use of item (activeself = = False)List<transform> _unusedcontentitems;//The item is not used now(activeself = = True)int _firstcacheditem;//The item in the first useint maxvisiableitems;//Maximum number of item displays
First,First you need to determine a data: How many item (ScrollView) to instantiate in this
The basic idea here is. How big an item is scrollview the box. Except for a moment (except for an endless number of large counts, such as 10.2 count 11), you know how many item the box can hold. It's actually going to be more than one, so the maximum item count is +1.
The code is:
(Tk2d does not count the item's high ... )

Second,Instantiate enough item. Put in the ScrollView, and placed each item in the appropriate position (calculate the spacing, this again Ngui with Uigrid can, tk2d need to calculate their own). You also need to use _unusedcontentitems to save each item conveniently after control. There is no data for each item at this time. The data needs to be maintained with another list _itemdatalist.
Fill data to _itemdatalist (not yet item)
Next is the logic that shows what should not be displayed, move item top, populate the data into item. So the logic of the whole function is to always refresh the updatalistgraphics logic during the sliding process. So we need to talk about the refresh logic right now.
Third,1. The first step calculates the number of the first item that is currently visible. Calculation method: ((Total item size (that is, content box size)-ScrollView size (viewable size)) * The current sliding ratio (0-1, slide to the low side is 1))/Itemstride (item high) first calculates the size of the upper part, and then the item is higher. The code is as follows:
2. Second step if the item quantity has to be processed when adding or deletingSimply, recalculate the content size and put all item setActive (false) into the unusedlist. All flag bits are reset. Swipe to revert to the previous position.here's a key. Can see here the size of the contentlength is to calculate all the item data multiplied by their high, that is to say 100 data, 10 item words, here is multiplied by
Then count the last item's number.
The third step, the more hidden awayIn two cases: 1. The top item in the Pull-up box can be turned off. (Firstcacheditem < Firstvisiableitem based on the first item that was just calculated) The value maintained on the top of the article Firstcacheditem update turns off the current first item. This item is added to the unusedlist. Cachedlist the first one to be erased.
2. When you pull down, the bottom item out of the display box can be turned off. (Firstcacheditem + cachedcontentitems.count) > Lastvisibleitem. The exact logic is the same as above.
The fourth step is to show the display. There are also two cases, and one more is to be aware of when there is no item. Customizelistobject can see. The main responsibility here is to put the data in each item. and placement. Here's a very important point! You can see that the position is calculated by multiplying the number by the item's height. In the process of sliding Ngui change is the Uigrid localposition, in the tk2d change is the content of the localposition, both are the same. Other words. Each item is localposition in the process of sliding. To say is that. Dynamiclist in fact, the item on the display with the general ScrollView is the same. The only control is to hide the unused item and place it on the item's original data when needed.

Is Dynamiclist, is the general ScrollView. The difference is that the left and right sides of the red box is not instantiated, but the actual order is so. (As you can imagine, dynamiclist actually has such a long content) so each item's position is so placed, do not need to move these item to move.
So each item is actually equivalent to an item in the item pool. Fill in the data when needed. (If it is a previous 2d game, you need to calculate the new item where to put it, because the use of scrollview, in fact, each item location is one after another, just move the parent node)
Then it shows the new item shown above because it adds one, so the first item number goes forward, gets the unused list first transform, and so on, use the transform from the unused list to delete the list cached add to the top. Use this transform. Open it
The following realistic item is displayed. It shows this whole section here.

This is done!!

Dynamic draggable list Dynmiclist,scrollview

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.