Android Listview中显示不同的视图布局详解及实例代码
AndroidListview中显示不同的视图布局
1.使用场景
在重写ListView的BaseAdapter时,我们常常在getView()方法中复用convertView,以提高性能。convertView在Item为单一的同种类型布局时,能够回收并重用,但是多个Item布局类型不同时,convertView的回收和重用会出现问题。比如有些行为纯文本,有些行则是图文混排,这里纯文本行为一类布局,图文混排的行为第二类布局。单一类型的ListView很简单,下面着重介绍一下ListView包含多种类型视图布局的情形。
2.ListView包含不同Item的布局
我们需要做这些工作:
1)重写getViewTypeCount()–该方法返回多少个不同的布局
2)重写getItemViewType(int)–根据position返回相应的Item
3)根据viewitem的类型,在getView中创建正确的convertView
3.案例
importjava.util.ArrayList; importandroid.app.Activity; importandroid.content.Context; importandroid.os.Bundle; importandroid.util.Log; importandroid.view.LayoutInflater; importandroid.view.View; importandroid.view.ViewGroup; importandroid.widget.BaseAdapter; importandroid.widget.CheckBox; importandroid.widget.ImageView; importandroid.widget.LinearLayout; importandroid.widget.ListView; importandroid.widget.TextView; publicclasslistViewTestextendsActivity{ /**Calledwhentheactivityisfirstcreated.*/ ListViewlistView; MyAdapterlistAdapter; ArrayList<String>listString; @Override publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); listView=(ListView)this.findViewById(R.id.listview); listString=newArrayList<String>(); for(inti=0;i<100;i++) { listString.add(Integer.toString(i)); } listAdapter=newMyAdapter(this); listView.setAdapter(listAdapter); } classMyAdapterextendsBaseAdapter{ ContextmContext; LinearLayoutlinearLayout=null; LayoutInflaterinflater; TextViewtex; finalintVIEW_TYPE=3; finalintTYPE_1=0; finalintTYPE_2=1; finalintTYPE_3=2; publicMyAdapter(Contextcontext){ //TODOAuto-generatedconstructorstub mContext=context; inflater=LayoutInflater.from(mContext); } @Override publicintgetCount(){ //TODOAuto-generatedmethodstub returnlistString.size(); } //每个convertview都会调用此方法,获得当前所需要的view样式 @Override publicintgetItemViewType(intposition){ //TODOAuto-generatedmethodstub intp=position%6; if(p==0) returnTYPE_1; elseif(p<3) returnTYPE_2; elseif(p<6) returnTYPE_3; else returnTYPE_1; } @Override publicintgetViewTypeCount(){ //TODOAuto-generatedmethodstub return3; } @Override publicObjectgetItem(intarg0){ //TODOAuto-generatedmethodstub returnlistString.get(arg0); } @Override publiclonggetItemId(intposition){ //TODOAuto-generatedmethodstub returnposition; } @Override publicViewgetView(intposition,ViewconvertView,ViewGroupparent){ //TODOAuto-generatedmethodstub viewHolder1holder1=null; viewHolder2holder2=null; viewHolder3holder3=null; inttype=getItemViewType(position); //无convertView,需要new出各个控件 if(convertView==null) { Log.e("convertView=","NULL"); //按当前所需的样式,确定new的布局 switch(type) { caseTYPE_1: convertView=inflater.inflate(R.layout.listitem1,parent,false); holder1=newviewHolder1(); holder1.textView=(TextView)convertView.findViewById(R.id.textview1); holder1.checkBox=(CheckBox)convertView.findViewById(R.id.checkbox); Log.e("convertView=","NULLTYPE_1"); convertView.setTag(holder1); break; caseTYPE_2: convertView=inflater.inflate(R.layout.listitem2,parent,false); holder2=newviewHolder2(); holder2.textView=(TextView)convertView.findViewById(R.id.textview2); Log.e("convertView=","NULLTYPE_2"); convertView.setTag(holder2); break; caseTYPE_3: convertView=inflater.inflate(R.layout.listitem3,parent,false); holder3=newviewHolder3(); holder3.textView=(TextView)convertView.findViewById(R.id.textview3); holder3.imageView=(ImageView)convertView.findViewById(R.id.imageview); Log.e("convertView=","NULLTYPE_3"); convertView.setTag(holder3); break; } }else{ //有convertView,按样式,取得不用的布局 switch(type) { caseTYPE_1: holder1=(viewHolder1)convertView.getTag(); Log.e("convertView!!!!!!=","NULLTYPE_1"); break; caseTYPE_2: holder2=(viewHolder2)convertView.getTag(); Log.e("convertView!!!!!!=","NULLTYPE_2"); break; caseTYPE_3: holder3=(viewHolder3)convertView.getTag(); Log.e("convertView!!!!!!=","NULLTYPE_3"); break; } } //设置资源 switch(type) { caseTYPE_1: holder1.textView.setText(Integer.toString(position)); holder1.checkBox.setChecked(true); break; caseTYPE_2: holder2.textView.setText(Integer.toString(position)); break; caseTYPE_3: holder3.textView.setText(Integer.toString(position)); holder3.imageView.setBackgroundResource(R.drawable.icon); break; } returnconvertView; } } //各个布局的控件资源 classviewHolder1{ CheckBoxcheckBox; TextViewtextView; } classviewHolder2{ TextViewtextView; } classviewHolder3{ ImageViewimageView; TextViewtextView; } }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!