Does a replacement for Gallery widget with View recycling exist?

后端 未结 5 554
情深已故
情深已故 2020-12-04 22:40

The default Gallery widget on Android does not recycle views - everytime the view for a new position is called the widget always calls the getView method of the

5条回答
  •  感动是毒
    2020-12-04 23:33

    My solution was, in the end, going with @CommonsWare's suggestion to modify the Gallery source code. This is also required copying the following files:

    • AdapterView
    • AbsSpinner

    but these are pretty simple.

    After that I modified code to do the following:

    RecycleBin (AbsSpinner)

    • Place objects in the recycler one after another, rather than according to position
    • Retrieve objects from the bottom of the recycler, regardless of the position requested
    • The existing implementation assumed that each different position in the adapter resulted in a unique view. The changes above are only good if your Gallery contains only one type of item, if not you'll need to add some sort of key based on item type and the amount of that type required

    Gallery

    • Used reflection (ugh) to modify the private mGroupFlags variable of ViewGroup to allow child re-ordering - I also set a boolean value indicating whether the field access succeeded which I test before using the component.
    • Removed all calls to mRecycler.clear()
    • The number of items the gallery has to display changes as it scrolls and the existing implementation would clear the recycler when (a) setSelection was called (b) a motion scroll occurred

    With these modifications my counter in my newView method in my adapter reached... 7.

    Here is the code (Placed in the public domain 2013/08/07 under http://en.wikipedia.org/wiki/WTFPL)

提交回复
热议问题