1

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?

JoeDoe
  • 11
  • 1

3 Answers3

0

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.

Oliver U.
  • 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
0

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.

GPack
  • 2,494
  • 4
  • 19
  • 50
0

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.

kalin
  • 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