欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android Listview中显示不同的视图布局详解及实例代码

程序员文章站 2024-02-12 22:12:22
android listview中显示不同的视图布局 1. 使用场景 在重写listview的baseadapter时,我们常常在getview()方法中复用conve...

android listview中显示不同的视图布局

1. 使用场景

在重写listview的baseadapter时,我们常常在getview()方法中复用convertview,以提高性能。convertview在item为单一的同种类型布局时,能够回收并重用,但是多个item布局类型不同时,convertview的回收和重用会出现问题。比如有些行为纯文本,有些行则是图文混排,这里纯文本行为一类布局,图文混排的行为第二类布局。单一类型的listview很简单,下面着重介绍一下listview包含多种类型视图布局的情形。

2.listview包含不同item的布局

我们需要做这些工作:

  1)重写 getviewtypecount() – 该方法返回多少个不同的布局
  2)重写 getitemviewtype(int) – 根据position返回相应的item
  3)根据view item的类型,在getview中创建正确的convertview

3.案例

import java.util.arraylist; 

import android.app.activity; 
import android.content.context; 
import android.os.bundle; 
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.listview; 
import android.widget.textview; 

public class listviewtest extends activity { 
/** called when the activity is first created. */
  listview listview; 
  myadapter listadapter; 
  arraylist<string> liststring; 

  @override 
  public void oncreate(bundle savedinstancestate) { 
    super.oncreate(savedinstancestate); 
    setcontentview(r.layout.main); 
    listview = (listview)this.findviewbyid(r.id.listview); 
    liststring = new arraylist<string>(); 
    for(int i = 0 ; i < 100 ; i++) 
    { 
      liststring.add(integer.tostring(i)); 
    } 
    listadapter = new myadapter(this); 
    listview.setadapter(listadapter); 
  } 

  class myadapter extends baseadapter{ 
    context mcontext; 
    linearlayout linearlayout = null; 
    layoutinflater inflater; 
    textview tex; 
    final int view_type = 3; 
    final int type_1 = 0; 
    final int type_2 = 1; 
    final int type_3 = 2; 

    public myadapter(context context) { 
      // todo auto-generated constructor stub 
      mcontext = context; 
      inflater = layoutinflater.from(mcontext); 
    } 

    @override 
    public int getcount() { 
      // todo auto-generated method stub 
      return liststring.size(); 
    } 

    //每个convert view都会调用此方法,获得当前所需要的view样式 
    @override 
    public int getitemviewtype(int position) { 
      // todo auto-generated method stub 
      int p = position%6; 
      if(p == 0) 
        return type_1; 
      else if(p < 3) 
        return type_2; 
      else if(p < 6) 
        return type_3; 
      else
        return type_1; 
    } 

    @override 
    public int getviewtypecount() { 
      // todo auto-generated method stub 
      return 3; 
    } 

    @override 
    public object getitem(int arg0) { 
      // todo auto-generated method stub 
      return liststring.get(arg0); 
    } 

    @override 
    public long getitemid(int position) { 
      // todo auto-generated method stub 
      return position; 
    } 

    @override 
    public view getview(int position, view convertview, viewgroup parent) { 
      // todo auto-generated method stub 
      viewholder1 holder1 = null; 
      viewholder2 holder2 = null; 
      viewholder3 holder3 = null; 
      int type = getitemviewtype(position); 

      //无convertview,需要new出各个控件 
      if(convertview == null) 
      { 
        log.e("convertview = ", " null"); 

        //按当前所需的样式,确定new的布局 
        switch(type) 
        { 
        case type_1: 
          convertview = inflater.inflate(r.layout.listitem1, parent, false); 
          holder1 = new viewholder1(); 
          holder1.textview = (textview)convertview.findviewbyid(r.id.textview1); 
          holder1.checkbox = (checkbox)convertview.findviewbyid(r.id.checkbox); 
          log.e("convertview = ", "null type_1"); 
          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 = ", "null type_2"); 
          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(r.id.imageview); 
          log.e("convertview = ", "null type_3"); 
          convertview.settag(holder3); 
          break; 
        } 
      }else{ 
        //有convertview,按样式,取得不用的布局 
        switch(type) 
        { 
        case type_1: 
          holder1 = (viewholder1) convertview.gettag(); 
          log.e("convertview !!!!!!= ", "null type_1"); 
          break; 
        case type_2: 
          holder2 = (viewholder2) convertview.gettag(); 
          log.e("convertview !!!!!!= ", "null type_2"); 
          break; 
        case type_3: 
          holder3 = (viewholder3) convertview.gettag(); 
          log.e("convertview !!!!!!= ", "null type_3"); 
          break; 
        } 
      } 

    //设置资源 
      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; 
    } 
  } 

  //各个布局的控件资源 
  class viewholder1{ 
    checkbox checkbox; 
    textview textview; 
  } 

  class viewholder2{ 
    textview textview; 
  } 

  class viewholder3{ 
    imageview imageview; 
    textview textview; 
  } 
}

感谢阅读,希望能帮助到大家, 谢谢大家对本站的支持!