Recyclerview和处理不同类型的排通货膨胀 [英] Recyclerview and handling different type of row inflation

查看:166
本文介绍了Recyclerview和处理不同类型的排通货膨胀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用新的RecyclerView工作,但我找不到与不同类型的行/ cardviews得到充气一个RecyclerView的例子。

I'm trying to work with the new RecyclerView, but I could not find an example of a RecyclerView with different types of rows/cardviews getting inflated.

使用的ListView我重写getviewtypecount和getitemviewtype,处理的行不同的类型。

With ListView I override the getviewtypecount and getitemviewtype, for handling different types of rows.

我应该做到像旧的方式还是应该做一些与布局管理?我想知道,如果有人可以点我到正确的方向。因为我只能找到的例子有一种类型。

Am I supposed to do it like the "old" way or should I do something with LayoutManager? I was wondering if someone could point me to the right direction. Because I can only find examples with one type.

我想有稍微不同的卡的列表。或者我应该只使用与它的内部cardviews滚动视图......让它没有适配器和recyclerview?

I want to have a list of slightly different cards. Or should I just use a scrollview with cardviews inside of it...make it without the adapter and recyclerview?

推荐答案

处理类似的iOS的UITableView的行/段逻辑不是简单的安卓,因为它是在iOS上,但是,当你使用RecyclerView - 的灵活性是什么你能做的就是更大。

Handling the rows / sections logic similar to iOS's UITableView is not as simple in Android as it is in iOS, however, when you use RecyclerView - the flexibility of what you can do is far greater.

在最后,这是所有关于你如何找出你的显示适配器什么类型的视图。一旦你明白了想通了,应该很容易帆船(不是真的,但至少你有一个排序的)。

In the end, it's all about how you figure out what type of view you're displaying in the Adapter. Once you got that figured out, it should be easy sailing (not really, but at least you'll have that sorted).

该适配器公开了两个方法,你应该重写:

The Adapter exposes two methods which you should override:

getItemViewType(int position)

此方法的默认实现总是返回0,这表明只有1类型的视图。你的情况,这是不是这样,所以你需要找到一种方法,断言其行对应的视图类型。不同于iOS系统中,管理这个与行路段,在这里你将有只有一个索引依靠,你将需要使用您的开发人员技能知道什么时候的位置相关于一个节头,当它关联到一个正常一行。

This method's default implementation will always return 0, indicating that there is only 1 type of view. In your case, it is not so, and so you will need find a way to assert which row corresponds to which view type. Unlike iOS, which manages this for with rows and sections, here you will have only one index to rely on, and you'll need to use your developer skills to know when a position correlates to a section header, and when it correlates to a normal row.

createViewHolder(ViewGroup parent, int viewType)

您需要反正重写此方法,但通常人们只是忽视了viewType参数。根据该视图类型,则需要抬高正确的布局资源,并相应地创建您的观点持有者。该RecyclerView将处理中,避免了不同的看法类型的撞击的方式回收利用不同的视图类型。

You need to override this method anyway, but usually people just ignore the viewType parameter. According to the view type, you'll need to inflate the correct layout resource and create your view holder accordingly. The RecyclerView will handle recycling different view types in a way which avoids clashing of different view types.

在你的布局管理,你需要的工作有点困难。唯一的API,你真的有工作,是 findViewByPosition(INT位置)这使得在某个位置给定的观点。因为你可能想展示出来的不同取决于什么的键入的这种观点是,你有几种选择:

In your LayoutManager, you'll need to work a bit harder. The only API you really have to work with is findViewByPosition(int position) which gives a given view at a certain position. Since you'll probably want to lay it out differently depending on what type this view is, you have a few options:

  1. 通常使用ViewHolder模式时,你设定的观点持有者视图的标签。您可以使用此运行时在布局管理器中找出什么类型的观点是在视图持有人添加字段,它的前presses这一点。

  1. Usually when using the ViewHolder pattern, you set the view's tag with the view holder. You could use this during runtime in the layout manager to find out what type the view is by adding a field in the view holder which expresses this.

既然你需要一个函数,它决定了哪个位置关联到哪个视图类型,你还不如让这种方法全局访问以某种方式(可能是单件类,其管理的数据?),然后就可以简单地根据位置查询相同的方法

Since you'll need a function which determines which position correlates to which view type, you might as well make this method globally accessible somehow (maybe a singleton class which manages the data?), and then you can simply query the same method according to the position.

这篇关于Recyclerview和处理不同类型的排通货膨胀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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