本文共 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。
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中直接传递适配器对象。
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'
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方法。
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() { ListdataModelList = 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.Adapterimplements 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()
对于立即执行数据绑定很重要。 否则会填充错误的视图。
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);
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/