1

我在 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;
}
4

1 回答 1

0

您没有为您的视图设置标签。您正在请求一个标签而不设置它。v.setTag(holder)在你的两行之后添加这一行holder.view = binding.getRoot();

如果它不起作用,请尝试v.setTag(holder.view)

于 2016-10-03T14:14:35.250 回答