android数据绑定_Android数据绑定高级概念

android数据绑定

In this tutorial we’ll look into some other tricky usages of Data Binding in our application. For a better understanding of this tutorial, do view the Android Data Binding Tutorial.

在本教程中,我们将研究应用程序中数据绑定的其他一些棘手用法。 为了更好地理解本教程,请查看Android数据绑定教程

Android数据绑定概述 (Android Data Binding Recap)

In the previous tutorial we looked into how Data Binding approach removes the need of using findViewById to hookup the UI view objects in our app code.
Another advantage of Data Binding over findViewById is that it enhances the performance of the application.
Using findViewById requires us to lookup the view in the entire hierarchy every time it’s invoked. Thus it requires a single pass through the entire view hierarchy for every child view. On the other hand, Data Binding uses just one single pass and stores all the field names of every child view layout. There’s no need for a second pass since we’ve already found all the views in the first pass itself.

在上一教程中,我们研究了数据绑定方法如何消除了使用findViewById连接应用程序代码中的UI视图对象的需求。
与findViewById相比,数据绑定的另一个优点是它可以提高应用程序的性能。
使用findViewById要求我们每次在视图中调用整个层次结构。 因此,它需要为每个子视图单次遍历整个视图层次结构。 另一方面,数据绑定仅使用一次传递,并存储每个子视图布局的所有字段名称。 不需要第二遍,因为我们已经在第一遍本身中找到了所有视图。

Android数据绑定概述 (Android Data Binding Overview)

Data Binding has lots more to do than just getting rid of findViewById. You must have experienced scenarios where a ListView or a RecyclerView or just a normal layout for the sake uses a data model to add the relevant information into the layout. Even if we get the field names of the view layouts in our MainActivity it still requires us to write a lot of repetitive code snippets. Let’s say we have a layout where we’re adding data from a DataModel.java class dynamically and updating the values after a specific interval. The following code snippet shows an example implementation of Data Binding when the data is supplied to the layout from a Data Model class.

数据绑定还有很多要做的事情,而不仅仅是摆脱findViewById。 您必须具有经验丰富的场景,其中ListView或RecyclerView或仅出于普通布局而使用数据模型将相关信息添加到布局中。 即使我们在MainActivity中获得了视图布局的字段名称,它仍然需要我们编写很多重复的代码片段。 假设我们有一个布局,其中要动态添加DataModel.java类中的数据,并在特定间隔后更新值。 下面的代码片段显示了将数据从数据模型类提供给布局时数据绑定的示例实现。

activity_main.xml

activity_main.xml

<layout xmlns:android="https://schemas.android.com/apk/res/android">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <TextView
            android:id="@+id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
</layout>

The DataModel.java class is given below.

下面给出了DataModel.java类。

public class DataModel {

    String name;
    int image;
    String summary;


    public DataModel(int image, String name, String summary) {

        this.name = name;
        this.summary = summary;
        this.image = image;
    }

    public int getImage() {
        return image;
    }

    public void setImage(int image) {
        this.image = image;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSummary() {
        return summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }
}

The MainActivity.java is given below.

MainActivity.java在下面给出。

public class MainActivity extends AppCompatActivity {

    ActivityMainBinding binding;
    Timer timer;
    int i = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

        DataModel dataModel = new DataModel(R.drawable.marsh, "Android MarshMallow", "Android 6.0");
        binding.imageView.setImageResource(dataModel.getImage());
        binding.name.setText(dataModel.getName());
        binding.summary.setText(dataModel.getSummary());

        timer= new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        if (i == 0) {
                            DataModel dataModel = new DataModel(R.drawable.lollipop, "Android Lollipop", "Android 5.0");
                            binding.imageView.setImageResource(dataModel.getImage());
                            binding.name.setText(dataModel.getName());
                            binding.summary.setText(dataModel.getSummary());

                            i=1;

                        } else {
                            
                            i=0;
                            DataModel dataModel = new DataModel(R.drawable.marsh, "Android MarshMallow", "Android 6.0");
                            binding.imageView.setImageResource(dataModel.getImage());
                            binding.name.setText(dataModel.getName());
                            binding.summary.setText(dataModel.getSummary());

                        }
                    }
                });
            }
        }, 3000, 3000); 
    }
}

We’ve used a timer that keeps changing the layout data alternately after every 3 seconds.
As you can see there is too much repetitive code for setText and setImageResource.
DataBinding allows us to directly assign the data from the DataModel class inside our xml layout. We’ll see how.

我们使用了一个计时器,该计时器每3秒钟会不断交替更改布局数据。
如您所见,setText和setImageResource的重复代码太多。
DataBinding允许我们直接在xml布局中分配来自DataModel类的数据。 我们将看看如何。

Android数据绑定项目结构 (Android Data Binding Project Structure)

Android数据绑定代码 (Android Data Binding Code)

We define the DataModel class variable object in the xml as shown in the layout below.

我们在xml中定义了DataModel类变量对象,如下图所示。

activity_main.xml

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="https://schemas.android.com/apk/res/android">

    <data>
        <variable
            name="data"
            type="com.journaldev.advanceddatabinding.DataModel" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@{data.image}" />

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{data.name}" />

        <TextView
            android:id="@+id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{data.summary}" />
    </LinearLayout>
</layout>

DataBinding has its own expression language for layout files. It allows us to assign the data directly in the xml attributes.

DataBinding具有自己的布局文件表达语言。 它允许我们直接在xml属性中分配数据。

  • <data> is put in the layout and serves as as wrapper for variables used in the layout tag can also be used to import types we need in your work example a View type as shown below:
    <data>
        <import type="android.view.View" alias="v"/>
    </data>

    The alias attribute is used to assign the import type a name for convenience.

    <data>放在布局中,用作布局标签中使用的变量的包装,也可以用于导入工作示例中所需的类型View类型,如下所示:

    为了方便起见, alias属性用于为导入类型分配名称。

  • <variable> contains name and type describing the name of the variable and its full name respectively (including the package name)

    <variable>包含名称和类型,分别描述变量的名称及其全名(包括程序包名称)
  • @{} container used for describing the expression. For example, displaying field names or displaying data using expressions and operators(we’ll look at this later)

    @ {}容器,用于描述表达式。 例如,显示字段名称或使用表达式和运算符显示数据(我们将在后面介绍)

The MainActivity.java is given below.

MainActivity.java在下面给出。

public class MainActivity extends AppCompatActivity {

    ActivityMainBinding binding;
    Timer timer;
    int i = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

        DataModel dataModel = new DataModel(R.drawable.marsh, "Android MarshMallow", "Android 6.0");
        binding.setData(dataModel);

        timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (i == 0) {
                            DataModel dataModel = new DataModel(R.drawable.lollipop, "Android Lollipop", "Android 5.0");
                            binding.setData(dataModel);
                            i = 1;
                        } else {
                            i = 0;
                            DataModel dataModel = new DataModel(R.drawable.marsh, "Android MarshMallow", "Android 6.0");
                            binding.setData(dataModel);
                        }
                    }
                });
            }
        }, 3000, 3000); // End of your timer code.
    }

    @BindingAdapter({"android:src"})
    public static void setImageViewResource(ImageView imageView, int resource) {
        imageView.setImageResource(resource);
    }
}

Doesn’t it look a lot cleaner and more readable now!

它现在看起来更干净,更易读了!

Few inferences drawn from the above code:

从以上代码中得出的推论很少:

  • The DataModel object is directly set to binding object as
    binding.setData(dataModel);.
    The method setData is generated from the variable name defined in the xml layout

    将DataModel对象直接设置为绑定对象,如下所示
    binding.setData(dataModel);
    setData方法是从xml布局中定义的变量名生成的
  • ImageView is a source attribute. For the BindingAdapter to recognise the android:src attribute we explicitly define it in the MainActivity.java as
    @BindingAdapter({"android:src"})
        public static void setImageViewResource(ImageView imageView, int resource) {
            imageView.setImageResource(resource);
        }

    ImageView是一个源属性。 为了使BindingAdapter能够识别android:src属性,我们在MainActivity.java中将其显式定义为

Android数据绑定应用程序输出 (Android Data Binding App Output)

The output of the application in action is given below.

android data binding advanced concepts

实际应用程序的输出如下。

The expression language provided by DataBinding has much more to use than just specifying the data.
We can specify the xml attributes based on certain conditions as shown below.

DataBinding提供的表达语言要使用的不仅仅是指定数据。
我们可以根据某些条件指定xml属性,如下所示。

android:text="@{data.name != null ? data.name : data.summary}"

The above line can be be written in a more compact form:

上面的行可以用更紧凑的形式编写:

android:text="@{data.name ?? data.summary}"

This brings an end to this tutorial. You can download the Android AdvanceDataBinding Project from the below link.

本教程到此结束。 您可以从下面的链接下载Android AdvanceDataBinding项目

翻译自: https://www.journaldev.com/11950/android-data-binding-advanced-concepts

android数据绑定

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值