话不多说,直接上代码
public class MyAdapter extends BaseAdapter {
List<Object> list = new ArrayList<>();
Context context = null;
LayoutInflater inflater = null;
public MyAdapter(Context context, List<Object> list) {
this.context = context;
this.list = list;
inflater = LayoutInflater.from(context);
}
/**
* 确定整个ListView一共会有多少个item
*/
@Override
public int getCount() {
return list.size();
}
/**
* 根据position从数据源中取数据对象
*/
@Override
public Object getItem(int position) {
return list.get(position);
}
/**
* 判定接下来的item是哪种类型
*/
@Override
public int getItemViewType(int position) {
return position % 3;
}
/**
* 所有item一共分为几类
*/
@Override
public int getViewTypeCount() {
return 3;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolderBase holder = null;
// 确定将要绘制的item是哪种类型
int viewType = getItemViewType(position);
switch (viewType) {
case 0:
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_layout0);
holder = new ViewHolder1(convertView);
} else {
holder = (ViewHolder1) convertView.getTag();
}
inflateItem0(list.get(position), (ViewHolder0) holder);
break;
case 1:
break;
case 2:
break;
}
return convertView;
}
private void inflateItem0(Object o, ViewHolder0 holder) {
holder.txtCommon.setText("xxx");
holder.myBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
private void inflateItem1(Object o, ViewHolder1 holder) {
}
private void inflateItem2(Object o, ViewHolder2 holder) {
}
abstract class ViewHolderBase {
//三种holder共有的属性可以提取到父类
@Bind(R.id.txtCommon)
TextView txtCommon;
public ViewHolderBase(View view) {
ButterKnife.bind(this, view);
}
}
class ViewHolder0 extends ViewHolderBase {
@Bind(R.id.myBtn)
Button myBtn;
public ViewHolder0(View view) {
super(view);
}
}
class ViewHolder1 extends ViewHolderBase {
@Bind(R.id.imageview)
ImageView imageview;
public ViewHolder1(View view) {
super(view);
}
}
class ViewHolder2 extends ViewHolderBase {
public ViewHolder2(View view) {
super(view);
}
}
}
体会getItemViewType和getViewTypeCount的作用。
另外,引入一个库,ButterKnife。