Android ListView optimization Best Practices

Source: Internet
Author: User

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:

  1. First, judge the status of the current ListView. Only when the ListView stops sliding can the new thread load data. other statuses are ignored.

  2. Use the getFirstVisiblePosition and getLastVisiblePosition methods to display items.

  3. 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 ~

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.