Loading layout for different items in listview

Source: Internet
Author: User

When rewriting the baseadapter of listview, we often reuse convertview in the getview () method to optimize listview to improve performance. Convertview can be recycled and reused when items are of the same type. However, if multiple items have different layout types, the collection and reuse of convertview may fail. For example, some behaviors are plain text, while some rows are text-and-text mixing. Here, the pure text behavior is a type of layout, and the text-and-text mixing behavior is a type of second layout. A single type of listview is very simple. The following describes the situation that listview contains multiple types of view la S. First, you can see the figure effect:

Click to download the source code.













Before writing code, we need to do this and understand its role:

1) override getviewtypecount ()-the number of different la s returned by this method

2) override getitemviewtype (INT)-return the corresponding item according to position

3) Create the correct convertview in getview Based on The View Item type.

Let's take a look at how the code is implemented:

Mainactivity. Class

/***** Listviewdifferenttype * @ author yuanjunhua ** 6:23:39 */public class mainactivity extends activity {private listview; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); listview = (listview) findviewbyid (R. id. lsitview); string [] name1 = new string [] {"Beijing", "Shanghai", "Nanjing", "Zhengzhou", "Institute of Light Industry", "Jiangzhai Village "}; string [] name2 = new string [] {"Beijing", "Shanghai", "Nanjing", "Zhengzhou", "Light Industry College", "Jiangzhai Village "}; integer [] Id2 = new integer [] {R. drawable. jx_left_listitem_1, R. drawable. jx_left_listitem_2, R. drawable. jx_left_listitem_3, R. drawable. jx_left_listitem_4, R. drawable. jx_left_listitem_5, R. drawable. jx_left_listitem_6}; List <string> list1 = new arraylist <string> (); filllistmethod (list1, name1); List <Map <string, integer> list2 = new arraylist <Map <string, integer> (); fillmapmethod (list2, name2, Id2); log. D ("maplist", "maplist =" + list1); log. D ("strlist", "strlist =" + list2); listview. setadapter (New myadapter (this, list1, list2);} private void fillmapmethod (list <Map <string, integer> list, string [] Name, integer [] ID) {// todo auto-generated method stubfor (INT I = 0; I <name. length; I ++) {Map <string, integer> map = new hashmap <string, integer> (); map. put (name [I], Id [I]); list. add (MAP) ;}} private void filllistmethod (list <string> list, string [] Name) {// todo auto-generated method stubfor (INT I = 0; I <name. length; I ++) {list. add (name [I]) ;}@ overridepublic Boolean oncreateoptionsmenu (menu) {// inflate the menu; this adds items to the action bar if it is present. getmenuinflater (). inflate (R. menu. main, menu); Return true ;}}

Inherit baseadapter

/***** Listviewdifferenttype * @ author yuanjunhua ** 6:23:31 */public class myadapter extends baseadapter {private layoutinflater Li; private list <string> list; private list <Map <string, integer> map; private final int type_one = 0, type_two = 1, type_count = 2; Public myadapter (context, list <string> list, list <Map <string, integer> map) {// todo auto-generated constructor stubthis. list = List; this. map = map; Li = layoutinflater. from (context) ;}@ overridepublic int getcount () {// todo auto-generated method stubreturn list. size () + map. size ();}/** the number of different la s returned by this method */@ overridepublic int getviewtypecount () {// todo auto-generated method stubreturn type_count ;} /** return the corresponding item according to position */@ overridepublic int getitemviewtype (INT position) {// todo auto-generated method stubint po = position % 2; if (Po = type_one) return type_one; elsereturn type_two;} @ overridepublic object getitem (INT position) {// todo auto-generated method stubreturn list. get (position % 6) ;}@ overridepublic long getitemid (INT position) {// todo auto-generated method stubreturn position % 6 ;}@ overridepublic view getview (INT position, view convertview, viewgroup parent) {// todo auto-generated method stubviewholder1 VH1 = NULL; viewholder2 vh2 = NULL; int type = getitemviewtype (position); If (convertview = NULL) {Switch (type) {Case type_one: VH1 = new viewholder1 (); convertview = Li. inflate (R. layout. item_one, null); vh1.tv1 = (textview) convertview. findviewbyid (R. id. TV1); convertview. settag (VH1); break; Case type_two: vh2 = new viewholder2 (); convertview = Li. inflate (R. layout. item_two, null); vh2.tv2 = (textview) convertview. findviewbyid (R. id. TV2); vh2.img2 = (imageview) convertview. findviewbyid (R. id. img2); convertview. settag (vh2); break ;}} else {Switch (type) {Case type_one: VH1 = (viewholder1) convertview. gettag (); break; Case type_two: vh2 = (viewholder2) convertview. gettag (); break ;}} switch (type) {Case type_one: If (position <5) vh1.tv1. settext (list. get (position % 6)-(Position % 6)/2); If (position> 5) vh1.tv1. settext (list. get (position % 6)-(Position % 6)/2 + 3); break; Case type_two: int I = 0; string TXT = NULL; Map <string, integer> mapsi = NULL; If (position <6) {I = (position % 6)-(Position % 6 + 1)/2; mapsi = map. get (I); iterator <string> it = mapsi. keyset (). iterator (); If (it. hasnext () TXT = it. next () ;}if (position> 6) {I = (position % 6)-(Position % 6 + 1)/2 + 3; mapsi = map. get (I); iterator <string> it = mapsi. keyset (). iterator (); If (it. hasnext () TXT = it. next ();} vh2.tv2. settext (txt); vh2.img2. setbackgroundresource (mapsi. get (txt); log. D ("TXT", "TXT =" + txt); break;} return convertview;} static class viewholder1 {textview TV1;} static class viewholder2 {textview TV2; imageview img2 ;}}
The implementation results are just a few pieces of code !!

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.