关于BaseAdapter多类型页面复用问题

 MyAdapter

/**
 * @author ruyi
 * @version 创建时间:2016年5月12日 下午4:42:11
 * 
 */
public class MyAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<Person> mList = new ArrayList<Person>();


public MyAdapter(Context context) {
mContext = context;
}


public void setData(ArrayList<Person> list) {
mList = list;
}


@Override
public int getCount() {
return mList == null ? 0 : mList.size();
}


@Override
public Object getItem(int position) {
return mList == null ? null : mList.get(position);
}


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


@Override
public int getViewTypeCount() {
return 2;
}


@Override
public int getItemViewType(int position) {
Person p = mList.get(position);
return p == null ? 3 : p.type;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
Person p = mList.get(position);


ViewHolder1 viewHolder1 = null;
ViewHolder2 viewHolder2 = null;

if (convertView == null) {
if (getItemViewType(position) == 0) {
Log.i("ASDFGHJKL", "NULL ----------------0");
convertView = LayoutInflater.from(mContext).inflate(R.layout.item_1,null);
viewHolder1 = new ViewHolder1(convertView);
convertView.setTag(0x7f060001,viewHolder1);
} else{
Log.i("ASDFGHJKL", "NULL ----------------1");
convertView = LayoutInflater.from(mContext).inflate(R.layout.item_2,null);
viewHolder2 = new ViewHolder2(convertView);
convertView.setTag(0x7f060000,viewHolder2);
}

} else {
if (getItemViewType(position) == 0) {
Log.i("ASDFGHJKL", "TRUE ----------------0");
viewHolder1 = (ViewHolder1) convertView.getTag(0x7f060001);
}else{
Log.i("ASDFGHJKL", "TRUE ----------------1");
viewHolder2 = (ViewHolder2) convertView.getTag(0x7f060000);
}
}

if (getItemViewType(position) == 0) {
viewHolder1.name.setText(p.name);
}else{
viewHolder2.name.setText(p.name);
viewHolder2.age.setText(p.age);
}


return convertView;
}


public class ViewHolder1 {
private TextView name;


public ViewHolder1(View view) {
name = (TextView) view.findViewById(R.id.name_1);
}
}


public class ViewHolder2 {
private TextView name;
private TextView age;


public ViewHolder2(View view) {
name = (TextView) view.findViewById(R.id.name_2);
age = (TextView) view.findViewById(R.id.age_2);
}
}


}


第一点:对convertView.setTag(0x7f060000,viewHolder2) 第一个int 参数的限制问题

 public void setTag(int key, final Object tag) {
        // If the package id is 0x00 or 0x01, it's either an undefined package
        // or a framework id
        if ((key >>> 24) < 2) {
            throw new IllegalArgumentException("The key must be an application-specific "
                    + "resource id.");
        }


        setKeyedTag(key, tag);
    }


第二点:关于复用的总结

当第一屏全为类型0,继续向下滑动时,类型0的可复用已消失的类型0的view

当第一屏全为类型0,继续向下滑动时,类型1无法复用已消失的类型0的view,此时判断convertView也是null,也就是类型1可复用的view为空,即使类型0的可复用view存在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值