I have a RecyclerView
with items, which sometimes should show sometimes up to three icons.
At the moment I am just adding ImageViews
to the layout in the onBind
metod dynamically, but sometimes the ImageViews
are added to the wrong item.
I have read some other questions with Problems on loading images too, what's the best approach to do this?

- 11
- 1
-
In what situation does the error occurs – Sam Apr 02 '16 at 16:08
-
Can you post your adapter code – Sally Apr 02 '16 at 16:08
-
@Sam when I add a new item – JoeDoe Apr 02 '16 at 16:14
-
Thats because of the viewholder. I had the same problems ones but I solved it. Ill post my code in a min. – Oliver U. Apr 02 '16 at 16:18
-
can you show your RecyclerView.Adapter? – GPack Apr 02 '16 at 18:06
-
do you load images from local resources or the network? – kalin Apr 02 '16 at 23:35
-
Two things come to mind. (1) One row layout with three `ImageView`s, with `visibility` set to `gone` or `visible` depending on requirements, or (2) different view types for each row, by overriding [`getItemViewType()`](http://stackoverflow.com/a/26245463/1219389) – PPartisan Apr 02 '16 at 23:53
3 Answers
Define three ImageVies in your custom_row.xml or whatever you named it. Set the width on wrap content and the gravitiy on center_horizontal so it will shown nicely.
In your onBindViewHolder(ItemViewHolder ivHolder, int position)
you have to do the following:
Dependent on the current position you set 1,2 or 3 of the imageview-visibilities on GONE
OR VISIBLE
. So the Images will always be centered in the horizontal.
Then you fill the imageviews with images. I think thats it. It worked for me.
Dont forget to define the 3 Imageviews in your ItemViewHolder
constructor.

- 336
- 1
- 4
- 14
-
Thanks, but I really don't want/need any layout advise. The icons are not the only thing I have in my item layout. I know I can have three `ImageViews` in the layout from the beginning, but I thought there is a better way... – JoeDoe Apr 02 '16 at 16:49
ViewHolder can be recycled from an another position, thus if you add views in the onBindViewHolder it needs adding or removing (if exist) them on position's dependency.

- 2,494
- 4
- 19
- 50
this is a common issues as you need to sync access to the holder. There are a few quite good libraries to handle this and other cases of bitmap caching, reusing, pooling.
you can use glide for local and network images:
Glide.with(context).load(items.get(i).getImage()).into(viewHolder.image);
or for only network images Volley or jus swithing to their NetworkImageView widget and then in the adapter:
image.setImageUrl(items.get(i).getImage(), imageLoader);
These may use different approaches but they all know what to do.

- 3,546
- 2
- 25
- 31
-
Does this work with adding ImageViews or should I have three ImageViews in every item, even if they shouldn't show an image? – JoeDoe Apr 03 '16 at 13:15
-
u have to have the image views already in the holder. you create those in oncreate viewholder – kalin Apr 03 '16 at 13:56