The Principle of getView in ListView and the Method of Solving Multiple-round Repeated Calls

Keywords: Java Android network Mobile

Links to the original text: http://www.cnblogs.com/lirizhi/p/3357771.html

The following article content is from the network copy and paste, because it was previously sorted into the notebook part can not find the source. If there is any infringement, please let us know.

 

[0] The working principle of getView in ListView:

[1]ListView asks adapter "give me a view" (getView) for each item of the list. (Get each item through getView)

[2]A new View is returned and displayed (retrieved and displayed)

So if we have a lot of data to display, does each Item repeat the action of creating a new View in getView? It takes a lot of resources to do duplicate things. Android actually provides us with a mechanism of duplication called Recycler.

The principle is described simply as follows:

When a complete ListView first appears, every Item is Null, and getView runs to the code segment that needs to inflate an Item. Assuming that the entire view can only display up to 10 items, when sliding to the 11th Item, the first item will be put in "recycler", if the 11th Item and "Recycle" If the view of the item of "er" is consistent, the Item in "Recycler" will be used to display it, so that the inflate will not be repeated again, which greatly saves the work of creating the View and is particularly important when large amounts of data need to be displayed.

The schematic diagram of the working principle is as follows:


Learn from oneself http://android.amberfog.com/?p=296:

Demo:

This is a getView method, and the other details of Code are not shown.

 

  1. static class ViewHolder   
  2.     {  
  3.         public ImageView localImageView = null;  
  4.         public TextView localTextView1 = null;  
  5.         public TextView localTextView2 = null;  
  6.         public TextView localTextView3 = null;  
  7.     }  
  8.       
  9.     public View getView(int paramInt, View paramView, ViewGroup paramViewGroup)  
  10.     {  
  11.         logger.i("This is position:" + paramInt);  
  12.         WrapperSonglist localUserShareSonglistEntity = (WrapperSonglist) getItem(paramInt);  
  13.         if(localUserShareSonglistEntity != null)  
  14.         {  
  15.             ViewHolder holder = null;  
  16.             if(paramView == null)  
  17.             {  
  18.                 this.logger.d("convertView == null,Then inflate and findViewById");  
  19.                 paramView = this.mInflater.inflate(R.layout.listitem04, paramViewGroup, false);  
  20.                 holder = new ViewHolder();  
  21.                 holder.localImageView = (ImageView) paramView.findViewById(R.id.listitem04ImageView);  
  22.                 holder.localTextView1 = (TextView) paramView.findViewById(R.id.listitem04TextView01);  
  23.                 holder.localTextView2 = (TextView) paramView.findViewById(R.id.listitem04TextView02);  
  24.                 holder.localTextView3 = (TextView) paramView.findViewById(R.id.listitem04TextView03);  
  25.                 paramView.setTag(holder);  
  26.             }  
  27.             else  
  28.             {  
  29.                 //Used ViewHolder to improve performance  
  30.                 this.logger.d("convertView != null,Then findViewById(get Holder)");  
  31.                 holder = (ViewHolder) paramView.getTag();  
  32.             }  
  33.             if(paramView != null)  
  34.             {  
  35.                 this.logger.d("convertView != null,Then SetValue");  
  36.                   
  37.                 String mstr = localUserShareSonglistEntity.getSonglistImage();  
  38.                 int id = localUserShareSonglistEntity.getSonglistId();  
  39.                 holder.localTextView1.setText("[id]:"+id+",bitmap:url:"+mstr);  
  40.                   
  41.                 String name = localUserShareSonglistEntity.getSonglistName();  
  42.                 holder.localTextView2.setText("[Name]:"+name);  
  43.                   
  44.                 String url = localUserShareSonglistEntity.getSonglistUrl();  
  45.                 holder.localTextView3.setText("[Url]:"+url);  
  46.             }  
  47.         }  
  48.         return paramView;  
  49.     }  

 

 

When we first started Listview, we showed only five Item s, and getView was called five times:

 


Printed logs are as follows: as you can see from 0 to 4 are null, we need to do the inflate action.

 

  1. 01-12 17:58:22.144: ERROR/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:69 getView ] - This is position:0  
  2. 01-12 17:58:22.154: DEBUG/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:80 getView ] - convertView == null,Then inflate and findViewById  
  3. 01-12 17:58:22.174: DEBUG/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:98 getView ] - convertView != null,Then SetValue  
  4. 01-12 17:58:22.174: ERROR/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:69 getView ] - This is position:1  
  5. 01-12 17:58:22.174: DEBUG/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:80 getView ] - convertView == null,Then inflate and findViewById  
  6. 01-12 17:58:22.184: DEBUG/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:98 getView ] - convertView != null,Then SetValue  
  7. 01-12 17:58:22.184: ERROR/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:69 getView ] - This is position:2  
  8. 01-12 17:58:22.184: DEBUG/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:80 getView ] - convertView == null,Then inflate and findViewById  
  9. 01-12 17:58:22.194: DEBUG/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:98 getView ] - convertView != null,Then SetValue  
  10. 01-12 17:58:22.194: ERROR/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:69 getView ] - This is position:3  
  11. 01-12 17:58:22.194: DEBUG/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:80 getView ] - convertView == null,Then inflate and findViewById  
  12. 01-12 17:58:22.204: DEBUG/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:98 getView ] - convertView != null,Then SetValue  
  13. 01-12 17:58:22.204: ERROR/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:69 getView ] - This is position:4  
  14. 01-12 17:58:22.204: DEBUG/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:80 getView ] - convertView == null,Then inflate and findViewById  
  15. 01-12 17:58:22.214: DEBUG/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:98 getView ] - convertView != null,Then SetValue<strong>  
  16. </strong>  


When we carefully slide down a position and the sixth Item just appears, the first one has not disappeared yet.

 


At this time, only one logo to get the sixth Item is printed, as follows: you can see whether the sixth Item is null, and you need to inflate out a new one.

 

  1. 01-12 18:02:37.623: ERROR/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:69 getView ] - This is position:5  
  2. 01-12 18:02:37.623: DEBUG/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:80 getView ] - convertView == null,Then inflate and findViewById  
  3. 01-12 18:02:37.633: DEBUG/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:98 getView ] - convertView != null,Then SetValue<strong>  
  4. </strong>  


If you slide down the list at this time, the first item will be placed in Recycler at this time, and the seventh Item will no longer be null at this time, but will use the View of the first item, as follows:

 


From the Log below, you can see that it's not null from the seventh.

01-12 18:52:36.243: ERROR/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:69 getView ] - This is position:6

01-12 18:52:36.243: DEBUG/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:89 getView ] - convertView != null,Then findViewById
01-12 18:52:36.243: DEBUG/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:98 getView ] - convertView != null,Then SetValue
01-12 18:52:36.693: ERROR/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:69 getView ] - This is position:7
01-12 18:52:36.693: DEBUG/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:89 getView ] - convertView != null,Then findViewById
01-12 18:52:36.693: DEBUG/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:98 getView ] - convertView != null,Then SetValue
01-12 18:52:37.024: ERROR/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:69 getView ] - This is position:8
01-12 18:52:37.024: DEBUG/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:89 getView ] - convertView != null,Then findViewById
01-12 18:52:37.034: DEBUG/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:98 getView ] - convertView != null,Then SetValue
01-12 18:52:37.604: ERROR/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:69 getView ] - This is position:9
01-12 18:52:37.604: DEBUG/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:89 getView ] - convertView != null,Then findViewById
01-12 18:52:37.604: DEBUG/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:98 getView ] - convertView != null,Then SetValue

After all the item s are displayed, no matter whether they are sliding up or down, they are no longer NULL, which means that the view in Recycle is used at this time instead of re-inflating. Obviously, this saves a lot of repetitive operations.

Solution to Repeated Multi-Round Call of getView

When we use listview. Sometimes when you customize adapter, do you find that when you print the log in getview, you call it many times? Sometimes four times. Some are serious even up to 10 times when we move in the listview. Each column will be called many times, which greatly affects the efficiency! In fact, this is related to the mechanism of listview itself on android. Let me begin by introducing:

In layout, we only have a list view. All right. When we set the height to wrap_content. Load a few lines in the listview. The log is printed in getview. Is it a duplicate call? That's a good way to do it. Set the height to fill_parent. Is the log found or repeated calls made at this time? Let's see if the higher level of Listview is also set to fill_parent, if not. Please change it. If it is... Then I really haven't encountered repeated calls! Because it's been tested several times!

If we have more than one Listview in it. A complex and beautiful layout may have many. Listview is somewhere in the layout. Sometimes it's bad luck. You will find that you call getview many times. When I test. Up to 230 times... As one can imagine. This is quite slow. And every move is called so many times! In this case, when modifying the layout, we should consider the following two points: 1. First, consider which is more important, the requirement layout and the performance. 2. Consider which layout control around listview affects it!

If there is no significant performance impact, the requirement must be the same layout. That's mainly about layout. See if there are other ways to optimize listview, provided that the layout can not be adjusted at all. If it can be adjusted, the layout will not change much. And listview can be well optimized. That's certainly optimized! When we optimize. First, you need to see if there are controls that affect the redrawing of Listview, such as. If it has controls above and below. And high is wrap_content, so you have to set it to fill_parent or fixed high. This way listview won't be redrawn at high altitude, which is the main point. Are there controls in the left and right (there aren't so many controls in a mobile page when lists are used)? Yes, then we have to adjust the same settings as the high ones. Make sure that listview is fixed in that place. Otherwise, you just wait for him to call it again.

That's all. The most important thing is when we do the layout. Use the attributes of each control skillfully and understand the principles of each control. In this way, when we do UI design, we can combine well!

 

 
 
Completion of the text

Reprinted at: https://www.cnblogs.com/lirizhi/p/3357771.html

Posted by okuto1973 on Thu, 25 Jul 2019 23:15:03 -0700