ListView不同布局的item的复用方法详解

时间:2021-10-11 19:32:57

废话不多说,直接上代码简介。

一、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}}

ListView不同布局的item的复用方法详解

复用item就是这么个原理