以下是我的 onBindViewHolder
的部分代码(在 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
没有按预期工作。
当我向下滚动时, View 正常工作。但是,当我开始向上滚动时,即重新访问以前的 View 时,应该是 VISIBLE
的 ImageViews变成 GONE
,虽然我检查了我的数据集并验证它没有被修改。尝试在 View 上调用其他方法也会产生不稳定的结果(数据集中的位置和项目不匹配)。
似乎 View 持有者没有绑定(bind)到 RecyclerView 内的特定位置。
如果我删除 HIDE THE VIEW
,代码将按预期工作部分。
有什么方法可以解决这个问题并在我的案例中动态隐藏 View 吗?
注意:我使用了一些 AsyncTasks 来更新数据集并调用 notifyDataSetChanged()
,如果这是相关的。
最佳答案
###This is the solution to your problem:###
holder.mImageViewContentPic.setVisibility(View.VISIBLE);
if(item.getContentDrawable() == null){
holder.mImageViewContentPic.setVisibility(View.GONE);
}
关于android - RecyclerView.Adapter 中的 ViewHolder 不特定于位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26076522/