问题
I am wondering if there's a way I can pre-load a couple of views in Android ListView?
For example, by default, when a listview items scrolls out of the screen, it will become a convert view and will be re-decorated as the next view on screen. My problem is, each listview item is loading a pretty large size image from memory, and it will stuck scrolling if it begin load right before it is gonna show up.
I was wondering if there's a way I can specify the threshold of loading views, for example, to preload 3 views rather than 1?
Thank you
回答1:
There is a bit different approach for this:
First, remove your loading actions from adapter's getView(). This method should be as lite as possible. Load a first batch of images in background and pass it to adapter. (store in List, for example)
Second. Set AbsListView.OnScrollListener on your listView, override onScroll() and calculate the amount of views user has scrolled: int count = firstVisibleItem + visibleItemCount;. If this int exceeds the number of images you loaded, start showing a progress bar and load your images in background thread, supplying them to your adapter (add to your List). Call notifyDataSetChanged() on your adapter after load is complete and you are done.
You can load as many items as you should.
来源:https://stackoverflow.com/questions/21994986/optimize-android-listviews-getview