Android ListView Optimization Best Practices

Android ListView Optimization Best Practices

This blog teaches you how to use convertView and viewHolder(static) to improve ListView lag. However, when ListView loads a large number of complex layouts and pictures, ListView still lags even with convertView and viewHolder. This article mainly discusses how to ensure the smoothness of ListView while loading complex list_items.

The core idea is

Listen for sliding data loading and load data asynchronously.

The getView function must not be time-consuming. Time-consuming tasks must be loaded asynchronously.

The main methods:

  1. First determine the current state of ListView. Only when ListView stops sliding will a new thread be started to load data. Other states are ignored.

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

  3. Time-consuming tasks must not be performed in the getView method, but should be performed asynchronously.

The specific code is as follows:

  1. //Judge 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 state  
  11. threadFlag = false ;
  12. break ;
  13. case AbsListView.OnScrollListener.SCROLL_STATE_IDLE: // Stop  
  14. threadFlag = true ;
  15. startThread(); //Start a new thread and load data  
  16. break ;
  17. case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL: // Touch listView  
  18. threadFlag = false ;
  19. break ;
  20. default :
  21. // Toast.makeText(contextt, "default",  
  22. // Toast.LENGTH_SHORT).show();  
  23. break ;
  24. }
  25. }
  26. };

I believe that if you do the above three points, you can use ListView with ease.

<<:  Go 1.4 is officially released, officially supports Android

>>:  How to develop an Apple App of the Year? See what the founder of Replay said

Recommend

A comprehensive summary of App promotion channels in 2019!

Performance Marketing We define any promotion tha...

User Activation: Breaking Down Luckin Coffee’s New User Activation Process

There are many ways to attract new customers to y...

The Value of Code Review — Why, When, and How?

[[143001]] For many companies, code review is an ...

IFR: India's new installations of industrial robots reached 4,945 in 2021

India has reached a new record of 4,945 new indus...

LeEco Supercar overturned, Wuliangye was caught "drunk driving" again

On January 2, Chery Automobile announced that aft...

Have you done 2 hours of outdoor activities every day?

In recent years, the incidence of myopia in my co...

Solution: Not receiving Windows 8.1 Update push

Windows 8.1 Update has been officially released fo...

Examples of ways and methods to attract new members to the community!

What are the common ways to attract new members t...