简体   繁体   English

如何针对不同的内容使用不同的卡片布局?

[英]How do I use different card layouts for varying content?

Displaying a certain layout for a card, when using CardView , is simple enough. 使用CardView ,显示卡的某个布局非常简单。

I've created an XML layout for my card, and instantiate that XML layout to the View using LayoutInflater . 我为我的卡创建了一个XML布局,并使用LayoutInflater将该XML布局实例化为View。

More specifically: 进一步来说:

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

Which is called in the onCreate() method of my RecyclerAdapter . 这是在我的RecyclerAdapteronCreate()方法中调用的。 The onCreate method then returns this largeCardVew . 然后onCreate方法返回此largeCardVew This works perfectly and the cards are displayed as they are laid out in the XML file. 这非常有效,并且卡片在XML文件中显示时显示。

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 ? 是否可以针对不同的东西显示不同的卡片,但是将它们全部放在同一个RecyclerView

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? 例如,如果我事先知道将有一个字符串传递到卡中的TextView ,并且此字符串只能有两个值“Large”和“Small”,我如何使用一种类型的XML文件来使用“大”文本和另一个与“小”文本一起使用的XML文件?

Here is an example (taken from Google Play ): 以下是一个示例(摘自Google Play ):

在此输入图像描述

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 ? 有没有办法实现类似的东西,并在同一RecyclerView有卡?

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. 它类似于创建单个卡布局的方式,但不是只有一个.xml和一个ViewHolder ,每个布局都需要有自己的 .xml文件来定义它,以及一个ViewHolder类。

The method which was found to be very important, is the getItemViewType(int position) method in the RecyclerView.Adapter class. 发现非常重要的方法是RecyclerView.Adapter类中的getItemViewType(int position)方法。 By default, this method returns 0, assuming a single layout for the viewType being used (in this case, a cardView ). 默认情况下,此方法返回0,假设使用的是viewType的单个布局(在本例中为cardView )。 Since more than a single type is needed, I had to define them. 由于需要多种类型,我必须定义它们。

I then created an array of integers (called cardViewTypes ), defining which layouts get used for which type of data. 然后我创建了一个整数数组(称为cardViewTypes ),定义哪些布局用于哪种类型的数据。 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. 由于您应该已经拥有某种卡片内容的数据集(无论是List还是HashMap ),因此确保数据与视图类型匹配非常重要。

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. 在我的情况下,我想要有4种不同的卡布局,所以我在他们自己的.xml文件中定义了每个,并为它们提供了唯一的名称。 Then, in my RecyclerView.Adapter class, I created a separate class which extended ViewHolder for each layout. 然后,在我的RecyclerView.Adapter类中,我创建了一个单独的类,为每个布局扩展了ViewHolder I made sure that each integer in my viewType array matched up with my data and called getItemViewType . 我确保我的viewType数组中的每个整数都与我的数据匹配并调用getItemViewType

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) . 然后我检查RecyclerView.AdapteronCreateViewHolder方法中的视图类型,并膨胀相应的视图,然后绑定在onBindViewHolder(ViewHolder, int)

And the result is differing card layouts for different content. 结果是不同内容的卡布局不同。

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

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