嵌套的recyclerview和数据绑定

介绍 (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

应用程序模型 (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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值