博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android数据绑定_Android RecyclerView数据绑定
阅读量:2533 次
发布时间:2019-05-11

本文共 7672 字,大约阅读时间需要 25 分钟。

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 .

为了了解Android DataBinding的基础知识,请访问 。

Data Binding significantly reduces the boilerplate code. Here, we’ll learn how to implement DataBinding with a which has the ViewHolder pattern.

数据绑定大大减少了样板代码。 在这里,我们将学习如何使用具有ViewHolder模式的来实现DataBinding。

Also, we’ll understand how Data Binding makes it easy to generalise the classes.

此外,我们还将了解数据绑定如何使通用化类变得容易。

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方法。

(Code)

The code for the DataModel.java class is given below:

下面给出了DataModel.java类的代码:

package com.journaldev.androidrecyclerviewdatabinding;public class DataModel {    public String androidVersion, androidName;    public DataModel(String androidName, String androidVersion) {        this.androidName = androidName;        this.androidVersion = androidVersion;    }}

The code for the activity_main.xml layout is given below:

下面给出了activity_main.xml布局的代码:

MainActivity.java

MainActivity.java

package com.journaldev.androidrecyclerviewdatabinding;import android.databinding.DataBindingUtil;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.support.v7.widget.LinearLayoutManager;import com.journaldev.androidrecyclerviewdatabinding.databinding.ActivityMainBinding;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity {    private ActivityMainBinding binding;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);        populateData();    }    private void populateData() {        List
dataModelList = new ArrayList<>(); dataModelList.add(new DataModel("Android Oreo", "8.1")); dataModelList.add(new DataModel("Android Pie", "9.0")); dataModelList.add(new DataModel("Android Nougat", "7.0")); dataModelList.add(new DataModel("Android Marshmallow", "6.0")); MyRecyclerViewAdapter myRecyclerViewAdapter = new MyRecyclerViewAdapter(dataModelList, this); binding.setMyAdapter(myRecyclerViewAdapter); }}

The layout for each row of the RecyclerView is defined in item_row.xml.

RecyclerView的每一行的布局在item_row.xml定义。

Inside the data tag, we pass two variables – a DataModel reference and a reference of the CustomClickListener interface whose method is called in the CardView.

在数据标签内,我们传递了两个变量–一个DataModel引用和一个CustomClickListener接口的引用,该接口的方法在CardView中被调用。

The code for the CustomClickListener.java is defined below:

下面定义了CustomClickListener.java的代码:

package com.journaldev.androidrecyclerviewdatabinding;public interface CustomClickListener {    void cardClicked(DataModel f);}

The code for the MyRecyclerViewAdapter.java class is given below:

下面给出了MyRecyclerViewAdapter.java类的代码:

package com.journaldev.androidrecyclerviewdatabinding;import android.content.Context;import android.databinding.DataBindingUtil;import android.support.v7.widget.RecyclerView;import android.view.LayoutInflater;import android.view.ViewGroup;import android.widget.Toast;import java.util.List;import com.journaldev.androidrecyclerviewdatabinding.databinding.ItemRowBinding;public class MyRecyclerViewAdapter extends RecyclerView.Adapter
implements CustomClickListener { private List
dataModelList; private Context context; public MyRecyclerViewAdapter(List
dataModelList, Context ctx) { this.dataModelList = dataModelList; context = ctx; } @Override public MyRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { ItemRowBinding binding = DataBindingUtil.inflate( LayoutInflater.from(parent.getContext()), R.layout.item_row, parent, false); return new ViewHolder(binding); } @Override public void onBindViewHolder(ViewHolder holder, int position) { DataModel dataModel = dataModelList.get(position); holder.bind(dataModel); holder.itemRowBinding.setItemClickListener(this); } @Override public int getItemCount() { return dataModelList.size(); } public class ViewHolder extends RecyclerView.ViewHolder { public ItemRowBinding itemRowBinding; public ViewHolder(ItemRowBinding itemRowBinding) { super(itemRowBinding.getRoot()); this.itemRowBinding = itemRowBinding; } public void bind(Object obj) { itemRowBinding.setVariable(BR.model, obj); itemRowBinding.executePendingBindings(); } } public void cardClicked(DataModel f) { Toast.makeText(context, "You clicked " + f.androidName, Toast.LENGTH_LONG).show(); }}

In order to pass the data to the XML counterpart we bind it using itemRowBinding.setVariable(BR.model, obj);.

为了将数据传递给XML副本,我们使用itemRowBinding.setVariable(BR.model, obj);将其绑定itemRowBinding.setVariable(BR.model, obj);

executePendingBindings() is important in order to execute the data binding immediately. Otherwise it can populate incorrect view.

executePendingBindings()对于立即执行数据绑定很重要。 否则会填充错误的视图。

Difference between setVariable() and setModel()
setVariable()和setModel()之间的区别

setVariable() is used in generic circumstances when the type of the data is not known.

在不知道数据类型的一般情况下,将使用setVariable()

setModel() is auto-generated. We can use the following instead of holder.bind(dataModel);.

setModel()是自动生成的。 我们可以使用以下内容代替holder.bind(dataModel);

holder.itemRowBinding.setModel(dataModel);

使用数据绑定在RecyclerView XML中传递适配器实例 (Passing the Adapter instance in RecyclerView XML Using Data Binding)

Thanks to data binding we can further reduce the boilerplate code in our MainActivity.java by passing the adapter instance in the XML inside the android:adapter attribute as shown below:

由于数据绑定,我们可以通过在android:adapter属性内的XML中传递适配器实例,从而进一步减少MainActivity.java的样板代码,如下所示:

activity_main.xml

activity_main.xml

In the MainActivity.java we can set the Adapter in the following way now:

在MainActivity.java中,我们现在可以通过以下方式设置适配器:

MyRecyclerViewAdapter myRecyclerViewAdapter = new MyRecyclerViewAdapter(dataModelList, this);binding.setMyAdapter(myRecyclerViewAdapter);

So there’s no need to even initialize RecyclerView in the Activity class.

因此,甚至不需要在Activity类中初始化RecyclerView。

The output of the above application in action is given below:

上面应用程序的输出如下:

That brings an end to this tutorial. You can download the project from the link below.

这样就结束了本教程。 您可以从下面的链接下载项目。

翻译自:

android数据绑定

转载地址:http://ymqzd.baihongyu.com/

你可能感兴趣的文章
win10+mongodb安装配置
查看>>
window7修改hosts文件
查看>>
【Leetcode_easy】720. Longest Word in Dictionary
查看>>
地铁时光机第一阶段冲刺一
查看>>
Code Smell那么多,应该先改哪一个?
查看>>
站立会议02(一期)
查看>>
oracle数据库导入导出问题
查看>>
Android中的动画
查看>>
LeetCode 119 Pascal's Triangle II
查看>>
【Noip2015pj】求和
查看>>
深入理解JavaScript——闭包
查看>>
C#-WebForm-css box-shadow 给边框添加阴影效果
查看>>
objective-c 编程总结(第七篇)运行时操作 - 动态属性
查看>>
C_数据结构_链表
查看>>
kettle-连接控件
查看>>
Coursera--Neural Networks and Deep Learning Week 2
查看>>
C#中的委托和事件(续)【来自张子扬】
查看>>
机器学习部分国内牛人
查看>>
模拟Sping MVC
查看>>
Luogu 3261 [JLOI2015]城池攻占
查看>>