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

5 minutes to show you how to play with App automation testing

Preface App automated testing has always been a f...

Is there only gas on a gas planet? If we want to land on Jupiter, can we do it?

There are eight planets in the solar system we li...

Restore a deeply demoted website from 0 to 4! How did it recover?

This was a website that seemed to have no room fo...

What does it mean for WeChat to open 11 interfaces?

WeChat has opened 11 JS-SDK interfaces, including...

Stock Broker Training Camp: Only Know How to Trade Stocks Episode 18

Stock Brother Training Camp: Only Know How to Tra...

How much does it cost to create a beauty app in Deyang?

The factors affecting the quotation of Deyang Bea...

The life-and-death feud between Steve Jobs and these six people

A new biography of Steve Jobs, released Tuesday, ...

Why do Emoji look different on different platforms?

Why does the same smiling emoji look so different...

Pinduoduo’s “Reading Month” Plan

Argentine writer Borges once said: "If there...