简体   繁体   English

将ImageViews添加到RecyclerView项目布局

[英]Adding ImageViews to RecyclerView Item Layouts

I have a RecyclerView with items, which sometimes should show sometimes up to three icons. 我有一个带有项目的RecyclerView ,有时应有时最多显示三个图标。 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. 目前,我只是加入ImageViews在布局onBind梅托德动态,但有时ImageViews被添加到了错误的项目。 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. 在custom_row.xml中定义三个ImageVies或您命名的任何ImageVies。 Set the width on wrap content and the gravitiy on center_horizontal so it will shown nicely. 设置换行内容的宽度和center_horizo​​ntal上的gravitiy,这样就可以很好地显示出来。 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 . 在您的onBindViewHolder(ItemViewHolder ivHolder, int position)您必须执行以下操作:根据当前位置,您可以在GONE OR VISIBLE上设置1,2或3个imageview-visibilities。 So the Images will always be centered in the horizontal. 因此图像将始终以水平为中心。 Then you fill the imageviews with images. 然后,用图像填充imageviews。 I think thats it. 我想是的。 It worked for me. 它对我有用。

Dont forget to define the 3 Imageviews in your ItemViewHolder constructor. 不要忘记在ItemViewHolder构造函数中定义3个Imageview。

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: 或仅将网络映像Vol​​ley或jus切换到其NetworkImageView小部件,然后在适配器中:

image.setImageUrl(items.get(i).getImage(), imageLoader);

These may use different approaches but they all know what to do. 这些可能使用不同的方法,但他们都知道该怎么做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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