我在 BaseAdapter 中使用具有不同布局的 gridview。如果那时视图被重用,我得到了 CastException。我该如何解决这个问题?
我的适配器代码是:
public View getView(int position, View v, ViewGroup parent) {
ViewHolder holder;
if (position == 0) {
if(v == null){
TestItemBinding binding = DataBindingUtil.
inflate(LayoutInflater.from(mContext), R.layout.test_item, parent, false);
holder = new ViewHolder();
holder.view = binding.getRoot();
} else {
holder = (GridViewHolder) v.getTag();
}
} else {
if(v == null){
TestItem2Binding binding = DataBindingUtil.
inflate(LayoutInflater.from(mContext), R.layout.test2_item, parent, false);
holder = new ViewHolder();
holder.view = binding.getRoot();
} else {
holder = (GridViewHolder) v.getTag();
}
}
setImage(holder.imageView, items.get(position));
return v;
}