Android - RecyclerView with one layout, multiple setVisibility

I have basically everything in one layout, which has everything you need for the main channel of the application. All variable elements (images, video thumbnails, etc.) are first set to GONE and set to VISIBLE when necessary.

This can sometimes be due to the behavior of the RecyclerView , which should be GONE VISIBLE in the wrong places.

Example:

Element No. 1 contains text

Item No. 2 contains an image

Item No. 3 contains an Image

I continue to scroll to point no x, then scroll back and this is what I get:

Element No. 1 contains an image from element no x, sometimes item no 3

Item No. 2 contains an image

Item No. 3 contains an Image

I am using a custom ViewHolder that extends RecyclerView.ViewHolder . The purpose of CustomViewHolder is to declare and initialize the layout.

  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); } 

An example of how I upload an image:

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

I set each visibility to GONE if the URL is not received from the server

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

But somehow the image is still being reused on previous elements (yes, not only Item no 1). Sometimes an image is also mistakenly ImageView . Image D is supposed to be in ImageView D, but instead it is in ImageView A.

Any guidance on setting the RecyclerView up and down beautifully?

If I miss something or you need to provide more code, let me know: D

+2
source share
1 answer

You need to put an else condition as well. As an example below.

 // 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); } 

Do this for each of the elements that you have in the list, in case you set their visibility at runtime.

+6
source

All Articles