[Android] the principle and solution of getview in listview

Source: Internet
Author: User

[0] 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 (display is returned after obtaining it)

If we need to display a large amount of data, will each item repeat the action of creating a new view in getview? This will consume a lot of resources to execute repetitive tasks. In fact, Android provides us with a set of reuse mechanisms called"Recycler ":

The principle is described as follows:

When a complete listview appears for the first time, each item is null. When getview is executed, it runs to the code segment that requires an inflate item. Assume that the entire view can only display up to 10 items, when sliding to 11th items, the first item will be put into "recycler". If the first item is consistent with the view of the item placed in "recycler, the items in "recycler" are used for display, so that you do not need to repeat the inflate operation. This greatly saves the creation of views and is especially important when you need to display a large amount of data.

The working principle is as follows:


Learning from http://android.amberfog.com /? (P = 296:

Demo:

This is a getview method. The code for other details is not displayed.

static class ViewHolder {public ImageView localImageView = null;public TextView localTextView1 = null;public TextView localTextView2 = null;public TextView localTextView3 = null;}public View getView(int paramInt, View paramView, ViewGroup paramViewGroup){logger.i("This is position:" + paramInt);WrapperSonglist localUserShareSonglistEntity = (WrapperSonglist) getItem(paramInt);if(localUserShareSonglistEntity != null){ViewHolder holder = null;if(paramView == null){this.logger.d("convertView == null,Then inflate and findViewById");paramView = this.mInflater.inflate(R.layout.listitem04, paramViewGroup, false);holder = new ViewHolder();holder.localImageView = (ImageView) paramView.findViewById(R.id.listitem04ImageView);holder.localTextView1 = (TextView) paramView.findViewById(R.id.listitem04TextView01);holder.localTextView2 = (TextView) paramView.findViewById(R.id.listitem04TextView02);holder.localTextView3 = (TextView) paramView.findViewById(R.id.listitem04TextView03);paramView.setTag(holder);}else{//Used ViewHolder to improve performancethis.logger.d("convertView != null,Then findViewById(get Holder)");holder = (ViewHolder) paramView.getTag();}if(paramView != null){this.logger.d("convertView != null,Then SetValue");String mstr = localUserShareSonglistEntity.getSonglistImage();int id = localUserShareSonglistEntity.getSonglistId();holder.localTextView1.setText("[id]:"+id+",bitmap:url:"+mstr);String name = localUserShareSonglistEntity.getSonglistName();holder.localTextView2.setText("[Name]:"+name);String url = localUserShareSonglistEntity.getSonglistUrl();holder.localTextView3.setText("[Url]:"+url);}}return paramView;}

When we start listview for the first time and only five items are displayed, getview is called five times:


The printed log is as follows: we can see that 0-4 is null. We need to perform the inflate action,

01-12 17:58:22.144: ERROR/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:69 getView ] - This is position:001-12 17:58:22.154: DEBUG/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:80 getView ] - convertView == null,Then inflate and findViewById01-12 17:58:22.174: DEBUG/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:98 getView ] - convertView != null,Then SetValue01-12 17:58:22.174: ERROR/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:69 getView ] - This is position:101-12 17:58:22.174: DEBUG/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:80 getView ] - convertView == null,Then inflate and findViewById01-12 17:58:22.184: DEBUG/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:98 getView ] - convertView != null,Then SetValue01-12 17:58:22.184: ERROR/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:69 getView ] - This is position:201-12 17:58:22.184: DEBUG/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:80 getView ] - convertView == null,Then inflate and findViewById01-12 17:58:22.194: DEBUG/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:98 getView ] - convertView != null,Then SetValue01-12 17:58:22.194: ERROR/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:69 getView ] - This is position:301-12 17:58:22.194: DEBUG/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:80 getView ] - convertView == null,Then inflate and findViewById01-12 17:58:22.204: DEBUG/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:98 getView ] - convertView != null,Then SetValue01-12 17:58:22.204: ERROR/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:69 getView ] - This is position:401-12 17:58:22.204: DEBUG/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:80 getView ] - convertView == null,Then inflate and findViewById01-12 17:58:22.214: DEBUG/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:98 getView ] - convertView != null,Then SetValue

When we slide down one position with caution and there are just 6th items, the first item does not disappear,


At this time, only one log for obtaining 6th items is printed, as shown below: we can see that the 6th items are still null, and the new one needs to be generated by inflate.

01-12 18:02:37.623: ERROR/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:69 getView ] - This is position:501-12 18:02:37.623: DEBUG/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:80 getView ] - convertView == null,Then inflate and findViewById01-12 18:02:37.633: DEBUG/MusicDemo(1272): @kesen@ [ main: Activity4Adapter.java:98 getView ] - convertView != null,Then SetValue

If you move down the list at this time, 1st items will be put into recycler, and 7th items will not be null at this time, but will use the view of 1st items, as shown below:


From the log below, we can see that it is not null since 7th.

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 10 items are displayed in total, no matter whether they are sliding up or down, they are no longer null. This means that the recycle view is used at this time and the inflate is no longer performed, obviously, this saves a lot of repetitive operations.

[1] repeated call to getview Solution

In the preceding example, we can see that getview is called every time you slide to the item to be displayed. In theory, it is 10 items. If all items are displayed, getview () is called () for 10 times, why is it strange sometimes that 10 items may call getview 20 times or even 40-50 times for one display? I'm sure many people have encountered this problem.

After checking for a long time, I did not find the root cause. I just knew that we should set the height to fill_parent or the specified height value when defining the listview in the XML file (this method is obviously unreliable, if the height is set, how does one adapt to different screens)

Therefore, we recommend the following:

<Listview Android: Id = "@ + ID/activity04_list" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"
/>

Some people on the internet explained that it was because of the problem of the listview item height calculation method. After trying it, it was still effective. I hope I will have the opportunity to explain the problem after I understand the principle, we can use this operation first. After setting it, we will find that one item is not slide before calling getview. This is a reasonable call, there will be no tricky calls with only 10 items but dozens of such calls, which also saves a lot of resources to avoid repeated useless work.

Thank you!



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.