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"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><ImageViewandroid:id="@+id/imageView"android:layout_width="wrap_content"android:layout_height="wrap_content"/><TextViewandroid:id="@+id/name"android:layout_width="wrap_content"android:layout_height="wrap_content"/><TextViewandroid: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;@Overrideprotected 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() {@Overridepublic void run() {runOnUiThread(new Runnable() {@Overridepublic 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><variablename="data"type="com.journaldev.advanceddatabinding.DataModel" /></data><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:orientation="vertical"><ImageViewandroid:id="@+id/imageView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@{data.image}" /><TextViewandroid:id="@+id/name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@{data.name}" /><TextViewandroid: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;@Overrideprotected 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() {@Overridepublic void run() {runOnUiThread(new Runnable() {@Overridepublic 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.

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

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项目

Download Android Data Binding Advanced Project下载Android数据绑定高级项目

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

android数据绑定

android数据绑定_Android数据绑定高级概念相关推荐

  1. android数据绑定_Android数据绑定

    android数据绑定 With the introduction of Android M, one big change that's come slightly less noticed is ...

  2. Android开发之TextView高级应用

    Android开发之TextView高级应用 我们平时使用TextView往往让它作为一个显示文字的容器,但TextView的功能并不局限于此.以下就和大家分享一下TextView的一些使用技巧. A ...

  3. Android设备音频部分一些概念

    Android设备音频部分一些概念 备注:基于高通平台 听筒是用来听对方传送过来的声音,手机放的MP3和开机铃声是从喇叭发出的. 听筒模式,就是手机上一般听电话的内置小耳机,声音较小. 扬声器模式,就 ...

  4. 数据库:学好SQL必须知道的10个高级概念

    今天给大家分享学好SQL必须知道的10个高级概念. 1.常见表表达式(CTEs) 如果您想要查询子查询,那就是CTEs施展身手的时候 - CTEs基本上创建了一个临时表. 使用常用表表达式(CTEs) ...

  5. android开发JNI之高级篇

    android开发JNI之高级篇 一,JNI介绍 百度百科 : http://baike.baidu.com/view/1272329.htm 二,JNI开发教程 1,JNI注册方式有两种,一种是静态 ...

  6. android开发笔记之高级主题—传感器的简单介绍

    今天我们开始进入讲解android中的一些高级主题的用法,比如传感器.GPS.NFC.语音和人脸识别等. 这次来对传感器的一个简单介绍: Android平台支持三大类的传感器: 位移传感器 这些传感器 ...

  7. Android AP 和 BP的概念

    Android AP 和 BP的概念 AP:Application Processor,即应用芯片: BP:Baseband Processor,即基带芯片: FCC(美国联邦通信委员会)认证要求将A ...

  8. SQL 开发的十个高级概念

    导读 SQL 是一种每位数据开发者必备的开发语言,不同的用户使用 SQL 语言的程度不同,最开始接触到的 SQL 就是 SELECT ,INSERT, UPDATE, DELETE 以及 WHERE ...

  9. Android app应用开发高级进阶系列专栏解读

    1.前言 在从事android app开发的几年里,最开始接触做android 都是从app开发开始做的,在做app的这几年中把积累下来的做的一些功能,都整理出来了作为自己的技术资料,在以后开发类似的 ...

最新文章

  1. java object转list_这份Java开发规范,让你100%受益!
  2. bn层初始化参数_神经网络参数初始化方式
  3. iOS开发(8)UISwitch
  4. java 运行时类型_Java基础之RTTI 运行时类型识别
  5. 初识Buildroot
  6. php 监控nginx日志,nginx启用status监控服务器状态的方法详解
  7. 整数运算、浮点运算与溢出
  8. Mybatis源码概览(一)
  9. HTML和小程序的 12 种 flex 布局
  10. ArmLinux BOOTLOADER全程详解
  11. 计算机专业代码qian,专业分类号及学科代码对照表.doc
  12. 历届二级c语言pdf,国家计算机二级C语言历年真题与答案.pdf
  13. 信息系统服务器(机柜)鉴定表,机房建设标准及管理规范
  14. Python进阶笔记(2):自动化处理文件
  15. c# 通过查看注册表的方式来判断电脑安装office的版本
  16. Google Play的APK下载(APK Downloader)解决方案
  17. 计算机应用应届求职简历,计算机应用应届生个人简历模板
  18. 双线一柱变色MACD指标
  19. 淘宝618列车喵币自动做任务app 懒人一键安装包
  20. html5基于Echarts制作3D炫酷世界地图地球制作,地图勾起动画,非常炫酷。

热门文章

  1. C#中自己动手创建一个Web Server(非Socket实现)
  2. 预写式日志 - postgresql之WAL(Write Ahead Log)
  3. 基于Android的手机邮件收发(JavaMail)之二( Welcome.java 和 ReceiveAndSend.java )
  4. 【备忘】一段用于在论坛上插入Flash内容的JavaScript代码
  5. Sql 语句:显示 Sql Server 中所有表中的信息
  6. [转载] 致 Python 初学者
  7. [转载] JAVA中分为基本数据类型及引用数据类型
  8. PHP 实例 AJAX 与 MySQL
  9. hdu 4421(枚举+2-sat)
  10. vc 可用的正则表达式库