[Android] principle of getview in listview + how to place multiple items in listview

Source: Internet
Author: User

Basics of listview and adapter

Working principle:

    1. For each item in the list, listview requires the adapter to "give me a view" (getview ).
    2. A New View is returned and displayed.

What if we want to display hundreds of millions of projects? Create a new view for each project? No! This is impossible!

In fact, Android caches the view for you.

There is a recycler component in Android that works as follows:

    1. If you have 1 billion items, only visible items are in memory, and others are in recycler.
    2. Listview first requests a type1 view (getview) and then requests other visible projects. Convertview is null in getview.
    3. When Item1 is out of the screen and a new project is down from the screen, listview requests a type1 view. Convertview is not a null value. Its value is item1. You only need to set new data and return convertview. You do not need to create a new view.

See the following example.CodeIn getview, system. Out is used for output.

Public class multipleitemslist extends listactivity {private mycustomadapter madapter; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); madapter = new mycustomadapter (); For (INT I = 0; I <50; I ++) {madapter. additem ("item" + I);} setlistadapter (madapter);} private class mycustomadapter extends baseadapter {private arraylist mdata = new arraylist (); Private layoutinflater minflater; Public mycustomadapter () {minflater = (layoutinflater) getsystemservice (context. layout_inflater_service);} public void additem (final string item) {mdata. add (item); notifydatasetchanged () ;}@ override public int getcount () {return mdata. size () ;}@ override Public String getitem (INT position) {return mdata. get (position) ;}@ override public long getitemid (INT position) {return position ;}@ override public view getview (INT position, view convertview, viewgroup parent) {system. out. println ("getview" + Position + "" + convertview); viewholder holder = NULL; If (convertview = NULL) {convertview = minflater. inflate (R. layout. item1, null); holder = new viewholder (); holder. textview = (textview) convertview. findviewbyid (R. id. text); convertview. settag (holder);} else {holder = (viewholder) convertview. gettag ();} holder. textview. settext (mdata. get (position); Return convertview;} public static class viewholder {public textview ;}}

 

RunProgramAnd then view the log in logcat.

 

Getview is called nine times, And convertview is null for all visible items (as shown below)

 

 
02-05 13:47:32. 559: INFO/system. out (947): getview 0 null02-05 13:47:32. 570: INFO/system. out (947): getview 1 null02-05 13:47:32. 589: INFO/system. out (947): getview 2 null02-05 13:47:32. 599: INFO/system. out (947): getview 3 null02-05 13:47:32. 619: INFO/system. out (947): getview 4 null02-05 13:47:32. 629: INFO/system. out (947): getview 5 null02-05 13:47:32. 708: INFO/system. out (947): getview 6 null02-05 13:47:32. 719: INFO/system. out (947): getview 7 null02-05 13:47:32. 729: INFO/system. out (947): getview 8 null

 

Then scroll down the list until item10 appears:

 

Convertview is still null, because recycler does not have a view (the edge of Item1 is still visible, at the top)

 

 
02-05 13:48:25. 169: INFO/system. Out (947): getview 9 null

 

Scroll back to list

 

Convertview is not a null value! Item1 leaves the screen and goes to recycler. Then item11 is created.

 

 
02-05 13:48:42. 879: INFO/system. Out (947): getview 10 Android. widget. linearlayout @ 43720.f8

 

Scroll again:

 14:01:31, 02-05. 069: INFO/system. out (947): getview 11 android. widget. linearlayout @ 437447d002-05 14:01:31. 142: INFO/system. out (947): getview 12 android. widget. linearlayout @ 43744ff802-05 14:01:31. 279: INFO/system. out (947): getview 13 android. widget. linearlayout @ 43743fa802-05 14:01:31. 350: INFO/system. out (947): getview 14 android. widget. linearlayout @ 4374582002-05 14:01:31. 429: INFO/system. out (947): getview 15 android. widget. linearlayout @ 4374604802-05 14:01:31. 550: INFO/system. out (947): getview 16 android. widget. linearlayout @ 4374687002-05 14:01:31. 669: INFO/system. out (947): getview 17 android. widget. linearlayout @ 4374709802-05 14:01:31. 839: INFO/system. out (947): getview 18 android. widget. linearlayout @ 437478c002-05 14:03:30. 900: INFO/system. out (947): getview 19 android. widget. linearlayout @ 43748df002-05 14:03:32. 069: INFO/system. out (947): getview 20 android. widget. linearlayout @ 43720.f8 

Convertview, as we expected, is not empty. After item11 leaves the screen, its view (@ 43720.f8) is used as convertview to hold item21.

Different project layout)

Let's take a slightly more complex example. Add some separation lines to the list in the above example.

You need to do this:

    1. Duplicate (@ override) Write getviewtypecount ()-return how many different la s you have
    2. Override getitemviewtype (INT)-view type ID returned by position
    3. Create the correct convertview in getview Based on The View Item type.

The following code is used:

 

Public class multipleitemslist extends listactivity {private mycustomadapter madapter; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); madapter = new mycustomadapter (); For (INT I = 1; I <50; I ++) {madapter. additem ("item" + I); if (I % 4 = 0) {madapter. addseparatoritem ("separator" + I) ;}} setlistadapter (madapter);} private class mycustomadap TER extends baseadapter {Private Static final int type_item = 0; Private Static final int type_separator = 1; Private Static final int type_max_count = type_separator + 1; private arraylist mdata = new arraylist (); private layoutinflater minflater; private treeset mseparatorsset = new treeset (); Public mycustomadapter () {minflater = (layoutinflater) getsystemservice (context. layout_inflater_servic E);} public void additem (final string item) {mdata. add (item); notifydatasetchanged ();} public void addseparatoritem (final string item) {mdata. add (item); // save separator position mseparatorsset. add (mdata. size ()-1); notifydatasetchanged () ;}@ override public int getitemviewtype (INT position) {return mseparatorsset. contains (position )? Type_separator: type_item;} @ override public int getviewtypecount () {return type_max_count;} @ override public int getcount () {return mdata. size () ;}@ override Public String getitem (INT position) {return mdata. get (position) ;}@ override public long getitemid (INT position) {return position ;}@ override public view getview (INT position, view convertview, viewgroup parent) {viewholder holder = NULL; int type = getitemviewtype (position); system. out. println ("getview" + Position + "" + convertview + "type =" + type); If (convertview = NULL) {holder = new viewholder (); Switch (type) {Case type_item: convertview = minflater. inflate (R. layout. item1, null); holder. textview = (textview) convertview. findviewbyid (R. id. text); break; Case type_separator: convertview = minflater. inflate (R. layout. item2, null); holder. textview = (textview) convertview. findviewbyid (R. id. textseparator); break;} convertview. settag (holder);} else {holder = (viewholder) convertview. gettag ();} holder. textview. settext (mdata. get (position); Return convertview;} public static class viewholder {public textview ;}}

 

Run the program and you will see a split line for every four items

Check the log. No exception occurs. All convertviews are empty.

 

 
02-05 15:19:03. 080: INFO/system. out (1035): getview 0 null type = 002-05 15:19:03. 112: INFO/system. out (1035): getview 1 null type = 002-05 15:19:03. 130: INFO/system. out (1035): getview 2 null type = 002-05 15:19:03. 141: INFO/system. out (1035): getview 3 null type = 002-05 15:19:03. 160: INFO/system. out (1035): getview 4 null type = 102-05 15:19:03. 170: INFO/system. out (1035): getview 5 null type = 002-05 15:19:03. 180: INFO/system. out (1035): getview 6 null type = 002-05 15:19:03. 190: INFO/system. out (1035): getview 7 null type = 002-05 15:19:03. 210: INFO/system. out (1035): getview 8 null type = 002-05 15:19:03. 210: INFO/system. out (1035): getview 9 null type = 1

Scroll list:

02-05 15:19:54. 160: INFO/system. out (1035): getview 10 null type = 002-05 15:19:57. 440: INFO/system. out (1035): getview 11 android. widget. linearlayout @ 43744528 type = 002-05 15:20:01. 310: INFO/system. out (1035): getview 12 android. widget. linearlayout @ 43744eb0 type = 002-05 15:20:01. 880: INFO/system. out (1035): getview 13 android. widget. linearlayout @ 437456d8 type = 002-05 15:20:02. 869: INFO/system. out (1035): getview 14 null type = 102-05 15:20:06. 489: INFO/system. out (1035): getview 15 android. widget. linearlayout @ 43745f00 type = 002-05 15:20:07. 749: INFO/system. out (1035): getview 16 android. widget. linearlayout @ 43747170 type = 002-05 15:20:10. 250: INFO/system. out (1035): getview 17 android. widget. linearlayout @ 43747998 type = 002-05 15:20:11. 661: INFO/system. out (1035): getview 18 android. widget. linearlayout @ 437481c0 type = 002-05 15:20:13. 180: INFO/system. out (1035): getview 19 android. widget. linearlayout @ 437468a0 type = 102-05 15:20:16. 900: INFO/system. out (1035): getview 20 android. widget. linearlayout @ 437489e8 type = 002-05 15:20:25. 690: INFO/system. out (1035): getview 21 android. widget. linearlayout @ 4374a8d8 type = 0

Convertview is empty for the split line until the first split line is visible. when it leaves the screen, the view goes to recycler and convertview starts to take effect.

From http://android.amberfog.com /? P = 296

Download Code: multipleitemslist.zip-source code

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.