Android ListView optimization Best Practices
I have a blog to teach you how to use convertView and viewHolder (static) to improve ListView freezing. However, even if convertView and viewHolder are used to load a large number of complicated la S and images in ListView, listView is stuck. This article mainly discusses how to ensure the smoothness of ListView while loading complicated list_item.
The core idea is
Listen to slide data loading and load data asynchronously.
The getView function must not be time-consuming. time-consuming tasks must be asynchronously loaded.
Main Methods:
First, judge the status of the current ListView. Only when the ListView stops sliding can the new thread load data. other statuses are ignored.
Use the getFirstVisiblePosition and getLastVisiblePosition methods to display items.
Time-consuming tasks must not be performed in the getView method. It is best to perform them asynchronously.
The Code is as follows:
1 // 1. determine listView status 2 AbsListView. onScrollListener onScrollListener = new AbsListView. onScrollListener () {// ListView 3 // touch event 4 5 public void onScroll (AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {6} 7 8 public void onScrollStateChanged (AbsListView view, int scrollState) {9 switch (scrollState) {10 case AbsListView. onScrollListener. SCROLL_STATE_FLING: // sliding status 11 threadFlag = false; 12 break; 13 case AbsListView. onScrollListener. SCROLL_STATE_IDLE: // stop 14 threadFlag = true; 15 startThread (); // start a new thread and load 16 break data; 17 case AbsListView. onScrollListener. SCROLL_STATE_TOUCH_SCROLL: // touch listView18 threadFlag = false; 19 break; 20 default: 21 // Toast. makeText (contextt, default, 22 // Toast. LENGTH_SHORT ). show (); 23 break; 24} 25} 26 };
I believe that the above three points will allow us to use ListView freely ~