The Android ListView has multiple item style handling methods

Source: Internet
Author: User

in the project development, I believe you may encounter a ListView in a number of different layouts, encountered this problem my general idea is to create multiple viewholder, At getviewtype time set different position of the item with different viewholder, well no nonsense so much directly on the code:

package com.sunny.youdao;import java.util.arraylist;import java.util.list;import  android.content.context;import android.util.log;import android.view.layoutinflater;import  android.view.view;import android.view.viewgroup;import android.widget.baseadapter;import  Android.widget.checkbox;import android.widget.imageview;import android.widget.linearlayout;import  android.widget.textview;public class myadapter extends baseadapter {private  Context mContext;private LinearLayout linearLayout = null;private  layoutinflater inflater;private list<string> list = new arraylist< String> ();p rivate textview tex;private final int view_type = 3;private  final int type_1 = 0;private final int type_2 = 1;private  final int type_3 = 2;public&nbsp Myadapter (context context, list<string> list)  {// todo auto-generated  constructor stubthis.mContext = context;this.list = list;inflater =  Layoutinflater.from (Mcontext);} @Overridepublic  int getcount ()  {// TODO  auto-generated method stub return list.size ();} @Overridepublic  object getitem (int position)  {// TODO  Auto-generated method stub return  List.get (position);} @Overridepublic  long getitemid (int position)  {// TODO  Auto-generated method stub return  Position;} Each Convert view will call this method to get the current view style      @Override     public  int getitemviewtype (int position)  {        //  TODO Auto-generated method stub        int  Viewtype = position%6;        if (Viewtype == 0)         return TYPE_1;         else if (viewtype < 3)              return type_2;        else if ( VIEWTYPE&NBSP;&LT;&NBSP;6)             return  type_3;        else             return TYPE_1;    }         //returns the number of styles      @Override     public int getviewtypecount ( )  {        // todo auto-generated method stub &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;RETURN&NBSP;3;&NBSP;&NBSP;&NBSP;&NBSP, @Overridepublic   View getview (int&Nbsp;position, view convertview, viewgroup parent)  {viewholder1 holder1 =  null;viewholder2 holder2 = null;viewholder3 holder3 = null;int type  = getitemviewtype (position);//  no convertview, need new out controls if  (Convertview == null) &NBSP;{LOG.E ("convertview = ",  "# # #convertView为空 # # #");//  determines the layout of new by the current desired style switch  ( Type)  {case type_1:convertview = inflater.inflate (R.layout.listitem1, parent,false); Holder1 = new viewholder1 ();holder1.textview =  (TextView)   Convertview.findviewbyid (;holder1.checkbox =  (CheckBox)  convertview.findviewbyid (; LOG.E ("convertview = ",  "layout style one"); Convertview.settag (holder1); Break;case type_2:convertview  = inflater.inflate (R.layout.listitem2, parent,false); Holder2 = new viewholder2 ( ); holder2.textview =  (TextView)  convertview.findviewbyid (R.ID.TEXTVIEW2); LOG.E ("convertview = ",  "layout Style II"); Convertview.settag (holder2); Break;case type_3:convertview  = inflater.inflate (R.layout.listitem3, parent,false); Holder3 = new viewholder3 ( );holder3.textview =  (TextView)  convertview.findviewbyid (R.ID.TEXTVIEW3); Holder3.imageview  =  (ImageView)  convertview.findviewbyid (; LOG.E ("convertview = ",  "layout style three"); Convertview.settag (holder3); break;}}  else {//  have Convertview, by style, get the layout switch  (type)  {case type_1:holder1 =   (ViewHolder1)  convertview.gettag (); LOG.E ("convertview= ",  "layout style one");break;case type_2:holder2 =  (viewHolder2)   Convertview.gettag (); LOG.E ("convertview= ",  "layout Style II");break;case type_3:holder3 =  (VIEWHOLDER3)   Convertview.gettag (); LOG.E ("convertview= ",  "layout style three"); break;}}   Set resource switch  (type)  {case type_1:holder1.textview.settext (integer.tostring (position)); Holder1.checkBox.setChecked (True); Break;case type_2:holder2.textview.settext (integer.tostring (position)); Break;case type_3:holder3.textview.settext (integer.tostring (position)); Holder3.imageView.setBackgroundResource (R.drawable.icon); break;} Return convertview;}   Control resources for each layout class viewholder1 {checkbox checkbox; Textview textview;} Class viewholder2 {textview textview;} class viewholder3 {imageview imageview; Textview textview;}}

The code is more intuitive and clear, the comments are more detailed, it is not detailed description, welcome to learn to communicate with US ~

This article is from the "Sunnygeek Technology blog" blog, please be sure to keep this source

The Android ListView has multiple item style handling methods

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: 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.