android开发中Listview中显示不同的视图布局

一、问题的引出

如何在listview的不同位置显示不同类型的内容,如在某一行显示广告,其他行显示文本?

二、问题的简答

自定义Adapter,复写BaseAdapter中的getItemViewType(int position)方法,根据listview的位置号,通过一定的算法去返回不同类型的视图布局。然后在复写getView方法中,根据获得的视图布局类型,去加载特定的布局。这样就实现了在一个listview中去加载多种不同类型的视图布局。

实例代码:
public class MyAdapter extends BaseAdapter {

	Context mContext;
	LinearLayout linearLayout=null;
	LayoutInflater inflater;
	TextView tex;
	ArrayList<String> mListString;
	
	final int VIEW_TYPE=3;
	final int TYPE_1=0;
	final int TYPE_2=1;
	final int TYPE_3=2;
	
	public MyAdapter() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	public MyAdapter(Context context,ArrayList<String> listString){
		mContext=context;
		mListString=listString;
		inflater=LayoutInflater.from(mContext);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mListString.size();
	}
	
	//每个convert view 都会调用此方法,获得当前所需要的view样式
	@Override
	public int getItemViewType(int position) {
		
		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() {
		
		return 3;

	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mListString.get(position);
	}

	@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) {
			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);
				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);
				
				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);
				
				convertView.setTag(holder3);
				break;

			default:
				break;
			}
		}else {//有convertView,按样式,取得不同的布局
			
			switch (type) {
			case TYPE_1:
				holder1=(ViewHolder1) convertView.getTag();
				
				break;
			case TYPE_2:
				holder2=(ViewHolder2) convertView.getTag();
				break;
			case TYPE_3:
				holder3=(ViewHolder3) convertView.getTag();
				break;
				

			default:
				break;
			}
		}
		
		//设置资源
		switch (type) {
		case TYPE_1:
			holder1.textView.setText(getItem(position).toString());
			holder1.checkBox.setChecked(true);
			break;
		case TYPE_2:
			holder2.textView.setText(getItem(position).toString());
			break;
		case TYPE_3:
			holder3.textView.setText(Integer.toString(position));
			holder3.imageView.setBackgroundResource(R.drawable.ic_launcher);
			break;
		default:
			break;
		}
		
		return convertView;
	}
	
	
}

/**
 * 各个布局的控件资源
 * @author ly
 *
 */
class ViewHolder1{
	CheckBox checkBox;
	TextView textView;
}

class ViewHolder2{
	TextView textView;
	
}

class ViewHolder3{
	ImageView imageView;
	TextView textView;
}

三、参考资料

1.android开发中Listview中显示不同的视图布局:
http://m.android100.org/?host=www.android100.org&src=http%3A%2F%2Fwww.android100.org%2Fhtml%2F201401%2F31%2F5433.html

你可能感兴趣的:(android开发中Listview中显示不同的视图布局)