Display of different layout itemview using a ListView

Source: Internet
Author: User

The implementation of the interface effect as shown, no adjustment can be made on the layout, so look ugly.
Two different itemview are shown in a ListView, mainly using the Getitemviewtype () method in Baseadapter and the Getviewtypecount () method. The following is the detailed implementation steps: 1. As with custom adapter, you need to write a itemview XML layout file. Only in this case you need to write two XML layout files, which correspond to two different itemview. The code for the two XML files is as follows: Layout 1
Layout 2
2. Customize a adapter class to inherit from Baseadapter. and realizethe Getitemviewtype () method, and the Getviewtypecount () method. As the name implies, the Getitemviewtype () method is used to obtain the type of itemview that is currently required to be drawn, and the Getviewtypecount () method is used to obtain the different kinds of itemview, that is, the total number of different kinds of itemview. the code for the custom adapter is as follows:
/** * Created by Kent on 2014/12/12. */public class Myadapter extends Baseadapter {private context Mcontext = null;//context Private Layoutinflater minflate    R = null;        Private list<baseitem> Mdata = null;//The data to be displayed public myadapter (context context, list<baseitem> data) {        This.minflater = Layoutinflater.from (context);    This.mdata = data;        }//Add a new item and notify the ListView to show Refresh public void AddItem (Baseitem newitem) {this.mData.add (newitem);    This.notifydatasetchanged ();    } @Override public int getitemviewtype (int position) {return mdata.get (position). Getitem_type ();    } @Override public int getviewtypecount () {return itemtype.item_type_max_count;        } @Override public int getcount () {if (Mdata = = null) {return 0;    } return This.mData.size ();    } @Override public Object getItem (int i) {return mdata.get (i);       } @Override public long getitemid (int i) { return i;         } @Override public view getView (int position, view Convertview, ViewGroup viewgroup) {view viewItem1 = null;        View viewItem2 = null;        int itemType = This.getitemviewtype (position);            if (ItemType = = viewholder1.item_view_type_1) {//First ITEM ViewHolder1 viewHolder1 = null;                if (Convertview = = null) {//Not cached ViewHolder1 = new ViewHolder1 ();                ViewItem1 = this.mInflater.inflate (R.layout.list_view_item_1, NULL, FALSE);                        Viewholder1.textview = (TextView) Viewitem1.findviewbyid (r.id.                Main_activity_list_view_item_1_textview);                        Viewholder1.imageview = (ImageView) Viewitem1.findviewbyid (r.id.                Main_activity_list_view_item_1_imageview);                Viewitem1.settag (ViewHolder1);            Convertview = viewItem1;          }else{viewHolder1 = (ViewHolder1) convertview.gettag ();  } ViewHolder1.textView.setText (((ItemBean1) mdata.get (position)). GetName ());        ViewHolder1.imageView.setBackgroundResource (R.drawable.ic_launcher);            }else if (ItemType = = viewholder2.item_view_type_2) {//second ITEM ViewHolder2 viewHolder2 = null;                if (Convertview = = null) {//Not cached ViewHolder2 = new ViewHolder2 ();                VIEWITEM2 = this.mInflater.inflate (R.layout.list_view_item_2, NULL, FALSE);                        Viewholder2.textview1 = (TextView) Viewitem2.findviewbyid (r.id.                Main_activity_list_view_item_2_textview);                        Viewholder2.textview2 = (TextView) Viewitem2.findviewbyid (r.id.                Main_activity_list_view_item_2_textview_2);                Viewitem2.settag (VIEWHOLDER2);            Convertview = viewItem2;            }else{ViewHolder2 = (ViewHolder2) convertview.gettag (); } viewHolder2.textView1.setText(((ItemBean2) mdata.get (position)). GetName ());        ViewHolder2.textView2.setText (((ItemBean2) mdata.get (position)). GetAddress ());    } return Convertview; }}
First of all, to say the ListView drawing principle, when the ListView is drawn, first call the GetCount () method to determine the ListView item number, and then draw each item when the GetView method to draw, GetItem and Getitemid are called when the ListView responds to user operation time.Here's an introduction to the GetView () method, which is called when each item is drawn, and it returns a View object, which is the view that needs to be drawn.Public View GetView (int position, view Convertview, ViewGroup viewgroup): Where the position parameter is the position of the currently drawn item, the Convertview parameter is the view currently being drawn, which is used primarily for caching, and the ViewGroup parameter is the parent control of the current view.AboutThe Getitemviewtype () method, and the Getviewtypecount () method, are discussed later. 3. ListView Data Source:Usually the data inside the ListView is derived from a list collection, and the elements in the list collection are data of the map type. That is: list<map<string, object>> data for each MAP type corresponds to the content that needs to be displayed in a itemview. In this case, the data source is defined from the object, and since this example shows a different item, we customize two beans that correspond to the data in both item. The code is as follows:
/** * Created by Kent on 2014/12/15. */public class Baseitem {    private int item_type = 0;    Public Baseitem (int item_type) {        this.item_type = Item_type;    }    public int Getitem_type () {        return item_type;    }    public void Setitem_type (int item_type) {        this.item_type = Item_type;    }}
Because each item requires a type variable to identify the current item, a base class is defined: The Baseitem class, which contains only one Item_type attribute that identifies the kind of item.The subclass two inherits from it:
/** * Created by Kent on 2014/12/15. */public class ItemBean1 extends baseitem{    private String name = NULL;    Private String ImagePath = null;    Public ItemBean1 (int item_type, string name, String imagePath) {        super (item_type);        this.name = name;        This.imagepath = ImagePath;    }    Public String GetName () {        return name;    }    public void SetName (String name) {        this.name = name;    }    Public String Getimagepath () {        return imagePath;    }    public void setImagePath (String imagePath) {        this.imagepath = ImagePath;    }    public int Getitemtype () {        return super.getitem_type ();    }    public void Setitemtype (int itemType) {        super.setitem_type (itemType);}    }
4. Code in Mainactivity:
public class Mainactivity extends Activity {private ListView ListView = null;    Adapter private Myadapter myadapter = null;    Data private list<baseitem> mdata = null;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Findviewsbyid ();        Init ();    Addlisteners ();    } private void Findviewsbyid () {This.listview = (ListView) Findviewbyid (R.id.main_activity_listview);        } private void Init () {this.mdata = new arraylist<baseitem> ();        This.mData.add (New ItemBean1 (Viewholder1.item_view_type_1, "name1", "iamgePath1"));        This.mData.add (New ItemBean1 (Viewholder1.item_view_type_1, "name2", "iamgePath2"));        This.mData.add (New ItemBean1 (Viewholder1.item_view_type_1, "name2", "iamgePath2"));        This.mData.add (New ItemBean2 (viewholder2.item_view_type_2, "name1", "Address1")); This.mData.add (New ItemBean2(Viewholder2.item_view_type_2, "name1", "Address1"));        This.mData.add (New ItemBean2 (viewholder2.item_view_type_2, "name1", "Address1"));        This.mData.add (New ItemBean1 (Viewholder1.item_view_type_1, "name2", "iamgePath2"));        This.mData.add (New ItemBean2 (viewholder2.item_view_type_2, "name1", "Address1"));        This.mData.add (New ItemBean1 (Viewholder1.item_view_type_1, "name2", "iamgePath2"));        This.mData.add (New ItemBean1 (Viewholder1.item_view_type_1, "name2", "iamgePath2"));        This.mData.add (New ItemBean1 (Viewholder1.item_view_type_1, "name2", "iamgePath2"));        This.mData.add (New ItemBean2 (viewholder2.item_view_type_2, "name1", "Address1"));        This.mData.add (New ItemBean2 (viewholder2.item_view_type_2, "name1", "Address1"));        This.mData.add (New ItemBean1 (Viewholder1.item_view_type_1, "name2", "iamgePath2"));        This.mData.add (New ItemBean1 (Viewholder1.item_view_type_1, "name2", "iamgePath2")); This.mData.add (New ItemBean1 (viewholdEr1.        Item_view_type_1, "name2", "iamgePath2"));        This.mData.add (New ItemBean1 (Viewholder1.item_view_type_1, "name2", "iamgePath2"));        This.mData.add (New ItemBean2 (viewholder2.item_view_type_2, "name1", "Address1"));        This.mData.add (New ItemBean2 (viewholder2.item_view_type_2, "name1", "Address1"));        This.mData.add (New ItemBean1 (Viewholder1.item_view_type_1, "name2", "iamgePath2"));        This.mData.add (New ItemBean1 (Viewholder1.item_view_type_1, "name2", "iamgePath2"));        This.mData.add (New ItemBean1 (Viewholder1.item_view_type_1, "name2", "iamgePath2"));        This.mData.add (New ItemBean1 (Viewholder1.item_view_type_1, "name2", "iamgePath2"));        This.mData.add (New ItemBean1 (Viewholder1.item_view_type_1, "name2", "iamgePath2"));        This.mData.add (New ItemBean2 (viewholder2.item_view_type_2, "name1", "Address1"));        This.myadapter = new Myadapter (this, this.mdata);    This.listView.setAdapter (This.myadapter); } private void Addlisteners (){    }} 
5. Viewholder, cache optimization for Itemview:
/** * Created by Kent on 2014/12/12. */public Final class ViewHolder1 {public    static final int item_view_type_1 = 0;    Public TextView TextView = null;    Public ImageView ImageView = null;}
/** * Created by Kent on 2014/12/12. */public Final class ViewHolder2 {public    static final int item_view_type_2 = 1;    Public TextView textView1 = null;    Public TextView textView2 = null;}
/** * Created by Kent on 2014/12/15. */public class ItemType {public    static final int item_type_max_count = 2;}
6. Realize the display of different item types;First look at the Getitemviewtype () method;
public int Getitemviewtype (int position) {        return mdata.get (position). Getitem_type ();    }
This method returns the type of Itemview currently being drawn, and the type data is stored in the ItemType property of each bean in the ListView data Source list.Look again at the Getviewtypecount () method:
public int Getviewtypecount () {        return itemtype.item_type_max_count;    }
This method returns the number of different item types.Finally, take a look at the most important GetView () method:
 Public View GetView (int position, view Convertview, ViewGroup viewgroup) {view viewItem1 = null;        View viewItem2 = null;        int itemType = This.getitemviewtype (position);            if (ItemType = = viewholder1.item_view_type_1) {//First ITEM ViewHolder1 viewHolder1 = null;                if (Convertview = = null) {//Not cached ViewHolder1 = new ViewHolder1 ();                ViewItem1 = this.mInflater.inflate (R.layout.list_view_item_1, NULL, FALSE);                        Viewholder1.textview = (TextView) Viewitem1.findviewbyid (r.id.                Main_activity_list_view_item_1_textview);                        Viewholder1.imageview = (ImageView) Viewitem1.findviewbyid (r.id.                Main_activity_list_view_item_1_imageview);                Viewitem1.settag (ViewHolder1);            Convertview = viewItem1;            }else{viewHolder1 = (ViewHolder1) convertview.gettag (); } viewholder1.textView.settext (((ItemBean1) mdata.get (position)). GetName ());        ViewHolder1.imageView.setBackgroundResource (R.drawable.ic_launcher);            }else if (ItemType = = viewholder2.item_view_type_2) {//second ITEM ViewHolder2 viewHolder2 = null;                if (Convertview = = null) {//Not cached ViewHolder2 = new ViewHolder2 ();                VIEWITEM2 = this.mInflater.inflate (R.layout.list_view_item_2, NULL, FALSE);                        Viewholder2.textview1 = (TextView) Viewitem2.findviewbyid (r.id.                Main_activity_list_view_item_2_textview);                        Viewholder2.textview2 = (TextView) Viewitem2.findviewbyid (r.id.                Main_activity_list_view_item_2_textview_2);                Viewitem2.settag (VIEWHOLDER2);            Convertview = viewItem2;            }else{ViewHolder2 = (ViewHolder2) convertview.gettag (); } viewHolder2.textView1.setText ((ItemBean2) mdata.get (position). GetName ());        ViewHolder2.textView2.setText (((ItemBean2) mdata.get (position)). GetAddress ());    } return Convertview; }
As I have already said, GetView is called when each item is drawn, so you want to display different types of item, and in this method, you can draw differently depending on the type of item.First, by
int itemType = This.getitemviewtype (position);
Gets the kind of item that is currently being drawn. Then different layouts are imported according to the different kinds.
ViewItem1 = this.mInflater.inflate (R.layout.list_view_item_1, NULL, FALSE);
This method uses Viewholder for the Itemview cache. The content is not explained here.
Source: http://download.csdn.net/detail/hhzz1504042001/8262015



Display of different layout itemview using a ListView

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.