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

Toutiao | Advertising Optimization Guide (Practical)

As one of the mainstream forms of mobile Internet...

How can artificial intelligence be applied in reality? Google is the key

The annual Google I/O developer conference arrive...

This 279 yuan gaming mouse may be as good as a thousand yuan product

1Good feel specially designed for gamers Thunder,...

Relying on yourself for everything is a sign of a person who has been hurt

From childhood to adulthood, I believe many peopl...

How to cancel the mini program authorized by Alipay?

Q: How to cancel the mini program authorized by A...

How to name a brand? There are several common methods

The name should tell potential customers what the...

8 secrets that Apple didn't tell you at the event

Recently, Apple released a new MacBook Air with R...

Carbon Science | What is the grid emission factor in the dual-carbon economy?

The grid emission factor is a key parameter for e...

Maybe this is the best way to monetize self-media...

With the rise of major online platforms, self-med...