android数据绑定
In this tutorial, we’ll be discussing and implementing the RecyclerView Using Data Binding in our Android Application.
在本教程中,我们将在Android应用程序中讨论和实现RecyclerView using Data Binding。
Android RecyclerView数据绑定 (Android RecyclerView DataBinding)
In order to know the basics of Android DataBinding, do visit this tutorial.
为了了解Android DataBinding的基础知识,请访问本教程 。
Data Binding significantly reduces the boilerplate code. Here, we’ll learn how to implement DataBinding with a RecyclerView which has the ViewHolder pattern.
数据绑定大大减少了样板代码。 在这里,我们将学习如何使用具有ViewHolder模式的RecyclerView来实现DataBinding。
Also, we’ll understand how Data Binding makes it easy to generalise the Adapter classes.
此外,我们还将了解数据绑定如何使通用化Adapter类变得容易。
Finally, we’ll demonstrate how to directly pass the adapter object in the XML.
最后,我们将演示如何在XML中直接传递适配器对象。
入门 (Getting Started)
Add the following code in your app’s build.gradle
:
在您应用的build.gradle
添加以下代码:
android{
...
dataBinding {
enabled = true
}
...
}
Add the following dependency.
添加以下依赖项。
implementation 'com.android.support:design:28.0.0'
项目结构 (Project Structure)
In the below application we’ll load the data in the adapter rows of the RecyclerView from the XML using the <data>
. Also we’ll set the onClickListener methods in the layout rows itself.
在下面的应用程序中,我们将使用<data>
从XML将数据加载到RecyclerView的适配器行中。 另外,我们将在布局行本身中设置onClickListener方法。