3

以下是我的onBindViewHolder(inside MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>)代码的一部分

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    // - get element from your dataset at this position
    StatusItem item = mDataset.get(position);
    //......
    //Add content and timing to the textview
    String content = item.getContent();

    holder.mTextViewTime.setText(timing);
    //Set the img
    holder.imgViewIcon.setImageDrawable(item.getProfileDrawable());
    //Set content image (for Instagram)
    holder.mImageViewContentPic.setImageDrawable(item.getContentDrawable());
    //HIDE THE VIEW Start
    if(item.getContentDrawable() == null){
        holder.mImageViewContentPic.setVisibility(View.GONE);
    }
    //HIDE THE VIEW End
}

该部件HIDE THE VIEW未按预期工作。当我向下滚动时,视图工作正常。但是,当我开始向上滚动,即重新访问以前的视图时,应该是的 ImageViewsVISIBLE变为GONE,尽管我检查了我的数据集并确认它没有被修改。尝试在视图上调用其他方法也会给出不稳定的结果(数据集中的位置和项目不匹配)。

似乎视图持有者没有绑定到 RecyclerView 内的特定位置。

如果我删除该HIDE THE VIEW部分,代码将按预期工作。有没有办法解决这个问题并在我的情况下动态隐藏视图?

注意:我使用了一些 AsyncTasks 来更新数据集并调用notifyDataSetChanged(),如果这是相关的。

4

2 回答 2

9
###This is the solution to your problem:###

holder.mImageViewContentPic.setVisibility(View.VISIBLE);
if(item.getContentDrawable() == null){
        holder.mImageViewContentPic.setVisibility(View.GONE);
    }
于 2014-09-27T16:20:20.820 回答
0

因为RecyclerViewrecycle使用得很好,ViewHolderA可能会被用作ViewHolderB,所以你需要具体化a的每个属性,ViewHolder以防某些属性附加到错误的对象上。

于 2014-11-25T03:47:57.253 回答