简体   繁体   中英

Adding ImageViews to RecyclerView Item Layouts

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?

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.

ViewHolder可以从另一个位置回收,因此,如果在onBindViewHolder中添加视图,则需要根据位置的依赖关系添加或删除视图(如果存在)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM