Listview中显示不同的视图布局

1.使用场景

  在重写ListView的BaseAdapter时,我们常常在getView()方法中复用convertView,以提高性能。convertViewItem为单一的同种类型布局时,能够回收并重用,但是多个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;
  }

}

http://www.cnblogs.com/devinzhang/archive/2012/07/02/2573554.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值