简体   繁体   English

数据绑定在Android IDE中如何工作?

[英]How does data-binding work in the Android IDE?

How data-binding work in the Android IDE? 数据绑定在Android IDE中如何工作? What is the behind-the-scenes process? 什么是幕后流程?

To nail down what I'm talking about, this guy tries to explain it but does an "eh" job of it. 为了弄清楚我在说什么,这个人试图解释它,但是对此做了“ eh”的工作。 I can't really understand him. 我不太了解他。

https://youtu.be/scJGJgcbMGQ https://youtu.be/scJGJgcbMGQ

What is Data Binding? 什么是数据绑定?

The principal of Data Binding is to synchronise two objects so they hold the same information. 数据绑定的原理是同步两个对象,以便它们保存相同的信息。 This is used in both directions, from UI object change to underlining object and vice versa. 从UI对象更改到下划线对象,这两个方向都可以使用,反之亦然。 Data Binding is often used between two different languages in our example XML and android. 在我们的示例XML和android中,经常在两种不同的语言之间使用数据绑定。 The Data Binding library provides all the necessary methods to import data and variables into the XML. 数据绑定库提供了将数据和变量导入XML的所有必要方法。

The advantages of data binding is a less confusing activity code and also avoiding Null pointer because of calling views which are not created or already destroyed by the framework. 数据绑定的优点是活动代码不太混乱,并且避免了Null指针,这是因为调用了未由框架创建或尚未破坏的视图。

In this example you see how to prepare the XML of an activity, to get ready for a object and helper Method. 在本示例中,您将看到如何准备活动的XML,以准备对象和辅助方法。


<data>
<!-- Import helper Method (static)-->
<import type="com.applications.startroutineactivity.helper.classes.Helper_MillisToString"/>
<!-- Prepare variable-->
<variable

name="Activity_Day"

type="com.applications.startroutineactivity.data_types_and_adapter.Obj_ActivityDay"/>

 </data>


<!-- this is an example how to use the object in xml-->
<TextView

android:id="@+id/tv_TakeTime_CurrentShownDateDateFormat"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginStart="8dp"

android:layout_alignTop="@+id/tv_TakeTime_UserHint"

android:clickable="true"

android:enabled="true"

android:hint="12.01.2018"

android:onClick="oc_tv_TakeTime_CurrentShownDateDateFormat"

android:text="@{Helper_MillisToString.getStringOutMillis(Activity_Day.getActivity_Day())}"

android:textColor="@color/Mercedes_WHITE"

android:textColorHint="@color/Mercedes_GREY"

android:textSize="@dimen/dim_TextSize_Normal_Big"

app:layout_constraintBottom_toBottomOf="@+id/bt_TakeTime_nextDate"

app:layout_constraintStart_toEndOf="@+id/bt_TakeTime_lastDate"

app:layout_constraintTop_toTopOf="@+id/bt_TakeTime_nextDate"/>

Use this in your activity class: 在您的活动班级中使用此命令:

ActivityTaketimeBinding mainBinding; //This always refers to the layout file name "_" turns into upperCase

//Use this in onCreate()

mainBinding = DataBindingUtil.setContentView(this, R.layout.activity_taketime);

//Use this to set an object
mainBinding.setActivityDay();//.setXXX is referring to the name of the variable in the XML file.

To enable Data binding add this to your build.gradle file. 要启用数据绑定,请将其添加到build.gradle文件中。

dataBinding {

enabled =true

}

buildToolsVersion '28.0.3'

Sync, clear and rebuild. 同步,清除和重建。 Also make sure to have installed all support packages from Sdk manager. 另外,请确保已安装Sdk管理器中的所有支持程序包。

Use this tutorial which is really nice. 使用教程非常好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM