Android Basics Getting Started tutorial--2.4.8 ListView Item multi-layout implementation

Source: Internet
Author: User

Android Basics Getting Started tutorial--2.4.8 ListView Item multi-layout implementation

tags (space delimited): Android Basics Getting Started Tutorial

Introduction to this section:

This section is the last section of this section of the ListView, which brings you to the implementation of the ListView multi-layout item,
What is a ListView item multi-layout, for example, QQ this chat list:

If he is using a ListView, then there are two different item on a ListView!
One left and one right, hey, this section will teach you how to achieve the multi-layout of the ListView!

1. Key explanations:

The override Getitemviewtype () method corresponds to which category the view is, and the Getviewtypecount () method Iew returns
Total number of categories! And then GetView there call Getitemviewtype get the corresponding category, then load the corresponding view!

2. Code implementation:

Here's what to do with the two layouts in the previous section, and then write a few more places in the adapter rewrite key:

Mutilayoutadapter.java:

/** * Created by Jay on 2015/9/23 0023. * * Public  class mutilayoutadapter extends baseadapter{    //Define two categories of flags    Private Static Final intType_book =0;Private Static Final intType_app =1;PrivateContext Mcontext;PrivateArraylist<object> Mdata =NULL; Public Mutilayoutadapter(Context mcontext,arraylist<object> Mdata) { This. Mcontext = Mcontext; This. Mdata = Mdata; }@Override     Public int GetCount() {returnMdata.size (); }@Override     PublicObjectGetItem(intPosition) {returnMdata.get (position); }@Override     Public Long Getitemid(intPosition) {returnPosition }//multi-layout core, through this judging category    @Override     Public int Getitemviewtype(intPosition) {if(Mdata.get (position)instanceofAPP) {returnType_app; }Else if(Mdata.get (position)instanceofBook) {returnType_book; }Else{return Super. Getitemviewtype (position); }    }//Number of categories    @Override     Public int Getviewtypecount() {return 2; }@Override     PublicViewGetView(intPosition, View Convertview, ViewGroup parent) {intType = Getitemviewtype (position); ViewHolder1 Holder1 =NULL; ViewHolder2 Holder2 =NULL;if(Convertview = =NULL){Switch(type) { CaseType_app:holder1 =NewViewHolder1 (); Convertview = Layoutinflater.from (Mcontext). Inflate (R.layout.item_one, parent,false);                   Holder1.img_icon = (ImageView) Convertview.findviewbyid (R.id.img_icon);                   Holder1.txt_aname = (TextView) Convertview.findviewbyid (r.id.txt_aname); Convertview.settag (R.id.tag_app,holder1); Break; CaseType_book:holder2 =NewViewHolder2 (); Convertview = Layoutinflater.from (Mcontext). Inflate (R.layout.item_two, parent,false);                   Holder2.txt_bname = (TextView) Convertview.findviewbyid (r.id.txt_bname);                   Holder2.txt_bauthor = (TextView) Convertview.findviewbyid (R.id.txt_bauthor); Convertview.settag (R.ID.TAG_BOOK,HOLDER2); Break; }        }Else{Switch(type) { CaseType_app:holder1 = (ViewHolder1) convertview.gettag (R.id.tag_app); Break; CaseType_book:holder2 = (ViewHolder2) convertview.gettag (R.id.tag_book); Break; }} Object obj = mdata.get (position);//Set the value of the control        Switch(type) { CaseType_app:app app = (app) obj;if(App! =NULL) {Holder1.img_icon.setImageResource (App.getaicon ());                Holder1.txt_aname.setText (App.getaname ()); } Break; CaseType_book:book book = (book) obj;if(Book! =NULL) {Holder2.txt_bname.setText (Book.getbname ());                Holder2.txt_bauthor.setText (Book.getbauthor ()); } Break; }returnConvertview; }//Two different viewholder    Private Static  class ViewHolder1{ImageView Img_icon;    TextView Txt_aname; }Private Static  class ViewHolder2{TextView Txt_bname;    TextView Txt_bauthor; }}

Here is a place to pay attention to, Convertview.settag (r.id.tag_app,holder1); we are usually directly
Settag (object), this is the overloaded method of Settag, the parameter is a unique key and a subsequent object!
Only!!! I started with Type_book as the first parameter and then reported the following error:

The key must is an application-specific resource ID
That's the first one. The only way to define a final type of int variable and hard-coded a value is to be impossible.
The practice here is to add directly to the Strings.xml:

    <itemname="Tag_APP" type="id"></item>    <itemname="Tag_Book" type="id"></item>

Of course, you can also create a ids.xml file under Res/values/, put the above code up!
In addition to this, there is a point to note that the classification of the mark to start from 0, or will report the following
Such an error:

Mainactivity.java:

 Public  class mainactivity extends appcompatactivity {    Private Static Final intType_book =0;Private Static Final intType_app =1;PrivateListView list_content;PrivateArraylist<object> Mdata =NULL;PrivateMutilayoutadapter Myadapter =NULL;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main);//Data preparation:Mdata =NewArraylist<object> (); for(inti =0; I < -; i++) {Switch((int) (Math.random () *2)){ CaseTYPE_BOOK:mData.add (NewBook ("The first line of code","Guo Lin")); Break; CaseTYPE_APP:mData.add (NewAPP (R.mipmap.iv_icon_baidu,"Baidu")); Break;        }} list_content = (ListView) Findviewbyid (r.id.list_content); Myadapter =NewMutilayoutadapter (mainactivity. This, Mdata);    List_content.setadapter (Myadapter); }}

The above randomly generates 0 and 1,0 to add a book object to the collection, and 1 to add an App Object!

3. Code Download:

Listviewdemo6.zip

This section summarizes:

OK, this section explains the implementation of the ListView item multi-layout, which is the rewrite of two methods,
Then GetView () make a judgment, set a different layout ~ The code is very simple ~
The knowledge of the ListView is over, of course, the knowledge of the ListView is more than that,
asynchronous loading, optimization and so on, these we will be in the advanced part of the study ~ said so much, thank you ~

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Basics Getting Started tutorial--2.4.8 ListView Item Multi-layout implementation

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.