介绍 (Introduction)
The RecyclerView is one of the Android components with a certain complexity in the implementation, and when it comes to having two nested RecyclerView the complexity can increase.
RecyclerView是Android组件之一,在实现中具有一定的复杂性,当涉及到两个嵌套的RecyclerView时,复杂性可能会增加。
In this tutorial we will learn how to nest two RecyclerView to achieve the result as shown in the following image.
在本教程中,我们将学习如何嵌套两个RecyclerView以实现结果,如下图所示。
![Image for post](https://miro.medium.com/freeze/max/9999/1*a19_DhG-w2OAdMIHy3Vt9w.gif)
应用程序模型 (Model of the application)
In this article we will try to have a data structure that will force us to have two nested RecyclerView, we will have a Book entity and another BookCategory entity.
在本文中,我们将尝试建立一个数据结构,该结构将迫使我们具有两个嵌套的RecyclerView,一个Book实体和另一个BookCategory实体。
@Parcelize
data class Book(
val id: String,
val title: String,
val imageRes: Int
) : Parcelable
@Parcelize
data class BookCategory(
val id: String,
val title: String,
val books: List<Book>
) : Parcelable
Gradle配置 (Gradle Configuration)
The dataBinding is a feature that is not active by default when we create a new project with Android Studio, so we will enable dataBinding in the build.gradle file of the app module.
当我们使用Android Studio创建新项目时, dataBinding是默认情况下不处于活动状态的功能,因此我们将在app模块的build.gradle文件中启用dataBinding 。
Note : To have access to the Parcelize annotation that we recently used on our entities it is also necessary to activate the experimental Android Extensions feature, this annotation allows to automatically generate the methods found in the Parcelable interface.
注意:要访问我们最近在实体上使用的Parcelize批注,还必须激活实验性Android扩展功能,此批注允许自动生成在Parcelable界面中找到的方法。
When you use dataBinding, it generates a certain amount of classes, that’s why you have to apply the kotlin-kapt plugin.
当您使用dataBinding时,它会生成一定数量的类,这就是为什么您必须应用kotlin-kapt插件的原因。
apply plugin: 'kotlin-kapt'
androidExtensions {
experimental = true
}
android {
dataBinding {
enabled true
}
// .....
}
书本单元:用于嵌套的RecyclerView (Book Cell : For Nested RecyclerView)
As our entities are ready, we are now going to create two layouts, one that will represent a Book and the other one that will represent a category of books, as we are going to use dataBinding it means that we are going to inject in the layout that represents a book item a book object , and in the layout that represents a category of books a BookCategory type object.
当我们的实体准备就绪时,我们现在将创建两个布局,一个布局将代表一本书 ,另一个布局将代表一类图书,因为我们将要使用dataBinding,这意味着我们将注入表示书籍项目的布局是book对象,在表示书籍类别的布局中是BookCategory类型的对象。
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<import type="androidx.core.content.ContextCompat" />
<variable
name="book"
type="org.ericampire.android.myapplication.Book" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imageView"
android:layout_width="150dp"
android:layout_height="200dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:scaleType="centerCrop"
android:src="@{ContextCompat.getDrawable(context, book.imageRes)}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintSt