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

 MyAdapter

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


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


public void setData(ArrayList 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存在

你可能感兴趣的:(android_实战)