一般情况下ListView类型用一个就可以满足需求,但是慢慢好会发现一个类型满足不了一些设计的需求了。会有这种情况出现,ListView的Item布局方式有两种或者说多种,参数也不相同。那么解决办法来了。
先写了个demo,先上最终效果图:
接下来介绍下项目结构图:
根据效果图可以看到,demo里只写了两种不同类型的item,当然类型的多少可以在此基础上进行扩展。
主要代码在适配器里处理:
import java.util.List; import android.content.Context; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import com.cs.cshuo.R; import com.cs.cshuo.bean.BaseItem; import com.cs.cshuo.bean.ItemBeanOne; import com.cs.cshuo.bean.ItemBeanTwo; import com.cs.cshuo.holder.ItemType; import com.cs.cshuo.holder.ViewHolderOne; import com.cs.cshuo.holder.ViewHolderTwo; public class MyAdapter extends BaseAdapter { private LayoutInflater mInflater = null; private List<BaseItem> mData = null;//要显示的数据 public MyAdapter(Context context, List<BaseItem> data){ this.mInflater = LayoutInflater.from(context); this.mData = data; } //添加一个新的Item,并通知listview进行显示刷新 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(final int position, View convertView, ViewGroup viewGroup) { View viewItem1 = null; View viewItem2 = null; int itemType = this.getItemViewType(position); if(itemType == ViewHolderOne.ITEM_VIEW_TYPE_0){ //第一种item ViewHolderOne viewHolder1 = null; if(convertView == null){ //没有缓存过 viewHolder1 = new ViewHolderOne(); viewItem1 = this.mInflater.inflate(R.layout.item_01, null, false); viewHolder1.tvName = (TextView)viewItem1.findViewById(R.id. tv01); viewItem1.setTag(viewHolder1); convertView = viewItem1; }else{ viewHolder1 = (ViewHolderOne)convertView.getTag(); } viewHolder1.tvName.setText(((ItemBeanOne) mData.get(position)).getName()); viewHolder1.tvName.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Log.e("", ""+((ItemBeanOne) mData.get(position)).getName()); } }); }else if(itemType == ViewHolderTwo.ITEM_VIEW_TYPE_1){ //第二种item ViewHolderTwo viewHolder2 = null; if(convertView == null){ //没有缓存过 viewHolder2 = new ViewHolderTwo(); viewItem2 = this.mInflater.inflate(R.layout.item_02, null, false); viewHolder2.tvName = (TextView)viewItem2.findViewById(R.id. tv02); viewHolder2.tvSex = (TextView)viewItem2.findViewById(R.id. tv02_01); viewItem2.setTag(viewHolder2); convertView = viewItem2; }else{ viewHolder2 = (ViewHolderTwo)convertView.getTag(); } viewHolder2.tvName.setText(((ItemBeanTwo)mData.get(position)).getName()); viewHolder2.tvSex.setText(((ItemBeanTwo)mData.get(position)).getSex()); } return convertView; } }
源码下载:files.cnblogs.com/files/yangcong/Cshuo.rar