2016-03-31 79 views
2

我基本上都在一个布局,其中有我的应用程序的主要饲料所需的一切。所有可变项目(图像,视频缩略图等)首先设置为GONE,当需要时设置为VISIBLEAndroid - RecyclerView与一个布局,多setVisibility

问题有时可能是由于RecyclerView的回收行为,应该是GONE的项目是VISIBLE错误的地方。

实施例:

货号1包含文本

货号2包含图像

货号3包含图片

我不断向下滚动项的NO x,然后向上滚动,这里是我得到的:

货号1包含图像从项目的NO x,有时没有项目3

货号2包含图像

货号3包含图片

我使用的自定义ViewHolder其中extends RecyclerView.ViewHolderCustomViewHolder的用途是用于布局声明和初始化。

ProgressBar progressBar; 
    View viewDimmer; 
    RelativeLayout postListWrapper; 

    ... 

    public ObjectViewHolder(View v) { 
     super(v); 
     progressBar = (ProgressBar)v.findViewById(R.id.post_inscroll_progressBar); 
     viewDimmer = (View)v.findViewById(R.id.post_inscroll_viewDimmer); 
     postListWrapper = (RelativeLayout)v.findViewById(R.id.post_inscroll_postListWrapper); 
    } 

的我是如何加载图像的一个例子:

Picasso.with(context) 
    .load(youtubeThumbnailUrl) 
    .fit() 
    .centerCrop() 
    .into(
     ((ObjectViewHolder) holder).userPostYoutubeImage 
    ); 

我已经设置每个能见度GONE如果从服务器

((ObjectViewHolder) holder).userPostImageWrapper.setVisibility(View.GONE); 
((ObjectViewHolder) holder).userPostYoutubeImageWrapper.setVisibility(View.GONE); 

没有获得URL但不知何故,图像仍然在以前的项目上重复使用(是的,不仅是产品编号1)。有时图片也出错了ImageView。图像D应该在ImageView D中,但它代替ImageView A.

任何设置RecyclerView的指南都很好吗?

如果我错过了什么,或者需要提供更多的代码,请你告诉我:d

回答

6

你需要把else条件了。像下面的例子。

// if no url is found from server 
if(url == null){ 
    ((ObjectViewHolder) holder).userPostImageWrapper.setVisibility(View.GONE); 
    ((ObjectViewHolder) holder).userPostYoutubeImageWrapper.setVisibility(View.GONE); 

} else { 
    // Some url has found 
    ((ObjectViewHolder) holder).userPostImageWrapper.setVisibility(View.VISIBLE); 
    ((ObjectViewHolder) holder).userPostYoutubeImageWrapper.setVisibility(View.VISIBLE); 
} 

这样做对每个你得有作为的情况下列表项的你在运行时设置其可见性的项目。

+3

这可能值得说明为什么这很重要。原因是持有人被重复使用。 – Knossos

+0

我只是被打了一巴掌。谢谢Reaz告诉我使用其他方式,谢谢@Knossos告诉我持有者已被重用!因此,即使我们移动到1号持有人,持有人号码3内的可见性仍然存在。 –

+0

@Knossos非常感谢您的补充。是的,这是值得分享的。 –