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

Summary of common tools and third-party libraries for Android development

My name is Ryan Cooke and I work on the Core Expe...

Why can't you always catch flies?

Today, we are going to talk about a little creatu...

Super complete SEM optimization solution! 6,000-word practical case analysis

This article will teach you how to make an execut...

How is the speed of light measured?

Everyone knows that light travels very fast, trav...

Attention! my country will carry out its first mission to impact an asteroid →

Author: Shi Xiangqi and Li Chuanfu In the vast un...

How did the community achieve a growth of 0-500 in 2 months?

The ultimate goal of community operation is to ac...

Is the Vernal Equinox the Earth’s exclusive right?

Xinhua News Agency, Tianjin, March 19 (Reporter Z...

An inventory of the digital marketing industry in the first half of 2016!

In the first half of 2016, the market size of Chi...