如何使用不同的布局相同RecyclerView适配器 [英] How to use the same RecyclerView Adapter for different layouts

查看:150
本文介绍了如何使用不同的布局相同RecyclerView适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发在很大程度上依赖于RecyclerView的使用的应用程序。

I am developing an application which heavily relies on the usage of RecyclerView.

我真的需要知道如何使用不同的项目布局相同RecyclerView。布局的一个例子是:

I really need to know how to use the same RecyclerView for different item layouts. An example of layouts are:

1)的名称和描述列表项

1) A list item with a name and description

2)的图像和名称

您可以看到,它们是相似的,但与布局略有改动。

You can see that they are similar, but with slight changes to the layout.

在此先感谢!

推荐答案

由于XML的recyclerview项目不依赖于那种你会在它膨胀的项目,你可以继续使用相同的回收视图布局文件中为三个片段。

Since the recyclerview item in xml doesn't depend on the kind of items that you will inflating in it, you can go ahead and use the same recycler view layout file for the three fragments.

至于适配器,您的名单似乎是均匀的(即单种视图项)。它您使用3种不同的适配器为每个案件最好的。您可以自定义构造函数中添加自定义的辅助方法,为每一种适配器根据您的方便。

As for the adapters, your lists seem to be homogeneous (i.e., single kind of view item). Its best that you use 3 different adapters for each case. You can customise the constructor, add custom helper methods for each kind of adapter as per your convenience.

现在,如果你有一个异构的列表中,你将不得不重写 getItemViewType()在适配器和 onCreateViewHolder适当地使用这个() onBindViewHolder()

Now if you have a heterogeneous list, you will have to override getItemViewType() in your adapter and use this appropriately in onCreateViewHolder() and onBindViewHolder()

希望这有助于! :)

这篇关于如何使用不同的布局相同RecyclerView适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆