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