1. 程式人生 > >ListView不同佈局的item的複用方法詳解

ListView不同佈局的item的複用方法詳解

廢話不多說,直接上程式碼簡介。

一、MainActivity和佈局

public class HomeActivity extends Activity {
	@Bind(R.id.lv)
	ListView lv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);
		ButterKnife.bind(this);
		lv.setAdapter(new ListViewAdapter(this));
	}

}

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:divider="#ff7A4299"
        android:dividerHeight="1px" />

</RelativeLayout>

二、ListViewAdapter(介面卡)

public class ListViewAdapter extends BaseAdapter {
	private final static String[] TITLE = { "個人資料", "活動記錄", "打卡", "投票", "現場抽獎", "購物車", "購買記錄", "登出" };
	private final static int[] ICON = { R.drawable.ico_12, R.drawable.ico_13, R.drawable.ico_14, R.drawable.ico_15,
			R.drawable.ico_16, R.drawable.ico_17, R.drawable.ico_18, R.drawable.ico_19 };

	private Context context;

	public ListViewAdapter(Context context) {
		super();
		this.context = context;
	}

	@Override
	public int getCount() {

		return TITLE.length * 5;
	}

	@Override
	public Object getItem(int position) {

		return TITLE[position % TITLE.length];
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder vh = null;
		// TITLE.length 等於8,總共有40個item。
		// 我們前20個用R.layout.item_listview,後20個用R.layout.item2_listview
		if (position < 20) {
			// 要複用,這裡就要做文章了
			// 這裡,我們R.layout.item_listview泵出來,convertView =
			// View.inflate(context, R.layout.item_listview, null);
			// convertView這時是item第一種佈局,我們知道它是LinearLayout,我們完全可以自定義一個Item1LinearLayout繼承LinearLayout
			// 用Item1LinearLayout替代R.layout.item_listview裡面的根佈局LinearLayout
			// 這時,我們複用時,在判斷convertView是否是Item1LinearLayout或者是它的子類就行了
			if (convertView != null && convertView instanceof Item1LinearLayout) {
				vh = (ViewHolder) convertView.getTag();
			} else {
				// 這裡convertView==null 或者 convertView不是Item1LinearLayout不能複用
				// 重新泵佈局1、R.layout.item_listview
				convertView = View.inflate(context, R.layout.item_listview, null);
				// 避免重複new物件ViewHolder
				if (vh == null) {
					vh = new ViewHolder();
				}
				vh.iconIv = (ImageView) convertView.findViewById(R.id.icon);
				vh.titleTv = (TextView) convertView.findViewById(R.id.title);
				convertView.setTag(vh);
			}
			vh.titleTv.setText(TITLE[position % TITLE.length]);
			vh.iconIv.setImageResource(ICON[position % TITLE.length]);
		} else {
			// 同上
			// 用Item2LinearLayout替代R.layout.item2_listview裡面的根佈局LinearLayout
			// 這時,我們複用時,在判斷convertView是否是Item2LinearLayout或者是它的子類就行了
			if (convertView != null && convertView instanceof Item2LinearLayout) {
				vh = (ViewHolder) convertView.getTag();
			} else {
				// 這裡convertView==null 或者 convertView不是Item2LinearLayout不能複用
				// 重新泵布2、R.layout.item2_listview
				convertView = View.inflate(context, R.layout.item2_listview, null);
				// 避免重複new物件ViewHolder
				if (vh == null) {
					vh = new ViewHolder();
				}
				vh.titleTv = (TextView) convertView.findViewById(R.id.title);
				convertView.setTag(vh);

			}
			vh.titleTv.setText(TITLE[position % TITLE.length]);
		}

		return convertView;
	}

	class ViewHolder {
		private ImageView iconIv;
		private TextView titleTv;
	}

}

1.R.layout.item_listview
<com.example.picassso.view.Item1LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingBottom="12dp"
    android:paddingTop="12dp" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="10dp" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="15dp"
        android:text="個人資料"
        android:textColor="#6000"
        android:textSize="18sp" />

</com.example.picassso.view.Item1LinearLayout>
2.R.layout.item2_listview
<com.example.picassso.view.Item2LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingBottom="12dp"
    android:paddingTop="12dp" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="15dp"
        android:text="個人資料"
        android:textColor="#f00"
        android:textSize="18sp" />

</com.example.picassso.view.Item2LinearLayout>
3.Item1LinearLayout和Item2LinearLayout
public class Item1LinearLayout extends LinearLayout {

	public Item1LinearLayout(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public Item1LinearLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public Item1LinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		// TODO Auto-generated constructor stub
	}

}
public class Item2LinearLayout extends LinearLayout {

	public Item2LinearLayout(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public Item2LinearLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public Item2LinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		// TODO Auto-generated constructor stub
	}

}



複用item就是這麼個原理