Skip to content Skip to sidebar Skip to footer

Why Are My "posts" Loading In Different Sizes In Android Recyclerview?

Okay, this is how my recyclerview is showing my 'posts'. Can anyone tell me why? I will post source code as well. I have tried changing some of the code with no success. Also, when

Solution 1:

Regarding the question "Also, when I scroll through my RecyclerView, some of them resize to smaller or larger sizes." : As the name says 'Recycler', it reuses the ViewHolder objects. When you scroll down/up and when the view (holder) goes off the screen, the view object wont get destroyed but it will be used again (recycled) to hold/represent another data set that is becoming visible.

If data set at the new position that is becoming visible does not have data (may be null or empty etc) which in your case is profile_pic/thumbnail, the RecyclerView will use the the data of the recycled holder which is used to represent this position. That is why when you have many data sets than that screen can hold and when you scroll up/down, you see that everytime you scroll a view (make it visible and invisible), it will show different data depending on the (recycled)holder it uses at the moment. If your profile pics are different than just cat pic for every view, then you can witness this effect clearly.

What you've to do is that before you set the data (image, text etc) to a view, you can better reset it first. If it is image you can clear/reset as suggested here https://stackoverflow.com/a/8243184/3209739. Or you can have placeholder image or make it transparent whatever. Then this problem should not occur. I think for same reason, it shows the different sized images (thumb and profile pic).

Try resetting the views before you set to your actual data. Always better to follow this when RecyclerView is used.

Why do you want to have your own caching code ? Why can not you use Picasso image loader, Universal Image Loader or something that suits your requirement. Take a look here to know the different image loaders Picasso v/s Imageloader v/s Fresco vs Glide.

Solution 2:

You're not properly inflating your View in onCreateViewHolder() Instead of

View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item, null);

you should use

View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item, viewGroup, false);

See this answer for more details on the different LayoutInflater.inflate() methods.

Post a Comment for "Why Are My "posts" Loading In Different Sizes In Android Recyclerview?"