适配器

时间:2016-03-08 14:41:26

标签: android android-databinding

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

1 个答案:

答案 0 :(得分:0)

您没有在视图中设置标记。您正在申请标签而不进行设置。 在两行[{1}}

之后添加此行v.setTag(holder)

如果不起作用,请尝试holder.view = binding.getRoot();