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

Keep the moral bottom line: 15 kinds of "rogue" methods of APP promotion

Rogue promotion methods are methods that some man...

Brand marketing innovation theory!

In the previous article "Brand Innovation Th...

6 ways to promote brand marketing on Xiaohongshu

Today I want to share with you some little knowle...

How to choose promotion channels? Look at these 5 principles and 4 dimensions

Under the epidemic, traffic has shifted online an...

The process of event planning is sorted out, save it!

Users, that is, traffic, are the audience groups ...

How to plan an offline event at zero cost?

Not long ago, on World Intellectual Property Day,...

Quantum interference makes counting stars easier

The exploration of the deep night sky has been wi...

Analysis of the gameplay and underlying logic of private domain traffic maps

In this article, the author sorted out and analyz...