我如何使用不同的卡布局不同的内容? [英] How do I use different card layouts for varying content?

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

问题描述

显示一定的布局卡,使用时 CardView ,是很简单的。

Displaying a certain layout for a card, when using CardView, is simple enough.

我创建了一个XML布局我的卡,并实例使用XML布局视图 LayoutInflater

I've created an XML layout for my card, and instantiate that XML layout to the View using LayoutInflater.

更具体地说:

View largeCardView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_view, viewGroup, false);

这就是所谓的的onCreate()方法我 RecyclerAdapter 。在的onCreate 方法,然后返回这个 largeCar​​dVew 。这完美的作品,并为它们在XML文件中布局的显示卡。

Which is called in the onCreate() method of my RecyclerAdapter. The onCreate method then returns this largeCardVew. This works perfectly and the cards are displayed as they are laid out in the XML file.

但是,如果我想为不同的内容显示不同的卡是什么?是否有可能显示不同的事情不同的卡,但他们都在同一个 RecyclerView

But what if I want to display different cards for different content? Is it possible to show different cards for different things, but have them all in the same RecyclerView?

例如,如果我的手之前知道会有卡传递到一个的TextView 一个字符串,这个字符串只能有两个值大和小,我怎么能使用一种与大的文字使用XML文件,并与小的文字使用?另一个XML文件

For example, if I know before hand that there will be a string passed into a TextView in the card, and this string can only have two values "Large" and "Small", how can I use one type of XML file to use with the "Large" text, and another XML file to use with the "Small" text?

下面是一个例子(从谷歌播放):

Here is an example (taken from Google Play):

在这里输入的形象描述

正如你可以在图片中看到,有两种不同类型的卡,有不同的布局(忽略橙色按钮)。有没有一种方法来实现类似的事情,并有卡在同一个 RecyclerView

As you can see in the picture, there are two different card types, with different layouts (ignore the orange button). Is there a way to achieve a similar thing, and have the cards in the same RecyclerView?

推荐答案

这是类似于你将如何创建一个单一的卡片布局,但不是在一个的.xml 和单一的 ViewHolder ,每个布局都需要有它的的.xml 文件中定义它和 ViewHolder 类。

It's similar to how you would create a single card layout, but instead of having a single .xml and a single ViewHolder, each layout needs to have its own .xml file defining it, and a ViewHolder class.

这被认为是非常重要的方法,就是<一个href=\"https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html#getItemViewType%28int%29\"相对=nofollow> getItemViewType(INT位置) 在RecyclerView.Adapter类方法。默认情况下,该方法返回0,假设一个单一的布局 viewType 正在使用(在这种情况下, cardView )。由于不是单一类型的更多需要,我不得不定义。

The method which was found to be very important, is the getItemViewType(int position) method in the RecyclerView.Adapter class. By default, this method returns 0, assuming a single layout for the viewType being used (in this case, a cardView). Since more than a single type is needed, I had to define them.

然后我创建了一个整数数组(名为 cardViewTypes ),定义哪些布局习惯于哪种类型的数据。既然你应该已经有某种形式的数据集,为卡片的内容(无论它是一个列表的HashMap ) ,重要的是要确保数据的视图类型相匹配。

I then created an array of integers (called cardViewTypes), defining which layouts get used for which type of data. Since you should already have some sort of dataset for the content of the cards (whether it's a List or a HashMap), it's important to make sure that the data matches up with the view types.

在我的情况,我想有4种不同的卡的布局,所以我定义各自在自己的的.xml 文件,并给他们唯一的名称。然后,在我的 RecyclerView.Adapter 类,我创建了一个单独的类,它扩展 ViewHolder 为每个布局。我确信,在我的 viewType 阵列中的每个整数匹配了与我的数据和名为 getItemViewType

In my case, I wanted to have 4 different card layouts, so I defined each in their own .xml file and gave them unique names. Then, in my RecyclerView.Adapter class, I created a separate class which extended ViewHolder for each layout. I made sure that each integer in my viewType array matched up with my data and called getItemViewType.

我然后检查在 RecyclerView.Adapter ,和膨胀相应的视图的 onCreateViewHolder 方法视图类型,然后在 onBindViewHolder约束(ViewHolder,INT)

I then check the view type in the onCreateViewHolder method of the RecyclerView.Adapter, and inflate the corresponding view, which is then bound in onBindViewHolder(ViewHolder, int).

和结果的不同卡布局不同的内容。

And the result is differing card layouts for different content.

如果这没有完全意义的,本教程异构卡布局是一个很大的帮助。

If this didn't totally make sense, this tutorial on Heterogeneous Card Layouts was a great help.

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

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