Android Jetpack之DataBinding的使用

Android Jetpack之DataBinding的使用

一、介绍

DataBinding 是Google在Jetpack中的一部分,主要是用于数据绑定用的,可以使得数据与页面绑定在一起,那么Jetpack的这个DataBinding,对于我们Android开发者来说意味着什么呢?

我们知道,我们早起的Android架构一般使用的是MVC架构,导致页面代码臃肿不堪,而且存在内存泄露,过了一段时间,MVP架构随之诞生,但是MVP同样存在他自身的问题,比如我们在使用P层的时候需要手动解除与页面的关联而避免内存泄露,再到了最近两年,MVVM架构逐渐的进入了Android我们开发者的眼中,在使用MVVM过程中,DataBinding扮演着重要的角色。

二、使用方法

gradle中配置

android {
 //将databinding 打开
 dataBinding {
 enabled = true
 }
}

layout中使用

<?xml version="1.0" encoding="utf-8"?>
<!--layout标签-->
<layout>
 <!--需要导入的数据,test为String类型-->
 <data>
 <variable
 name="test"
 type="String" />
 </data>
 <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".TestActivity">
​
 <!--在布局中的使用-->
 <TextView
 android:text="@{test}"
 android:textSize="20sp"
 app:layout_constraintBottom_toBottomOf="parent"
 app:layout_constraintTop_toTopOf="parent"
 app:layout_constraintEnd_toEndOf="parent"
 app:layout_constraintStart_toStartOf="parent"
 android:id="@+id/textView"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 />
 </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

Activity中得到当前页面的DataBinding对象

public class TestActivity extends AppCompatActivity {
​
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 //根据布局创建对应的DataBinding对象
 //fragment或者其他view可以使用 DataBindingUtil.inflate(),viewDataBinding.getRoot()就是你的布局对象
 ActivityTestBinding viewDataBinding = DataBindingUtil.setContentView(this, R.layout.activity_test);
 //直接传入String,由于textview绑定了test,数据会自动加载
 viewDataBinding.setTest("DataBinding的使用");
 //也可以通过textView对象直接setText
 viewDataBinding.textView.setText("DataBinding的使用");
 }
}

当然我们也可以在layout布局文件中导入我们自定义的对象

public class TestData {
 private String test;
​
 public TestData(String test) {
 this.test = test;
 }
​
 public String getTest() {
 return test;
 }
​
 public void setTest(String test) {
 this.test = test;
 }
}
<?xml version="1.0" encoding="utf-8"?>
<!--layout标签-->
<layout>
 <!--需要导入的数据,test为String类型-->
 <data>
 <variable
 name="test"
 type="String" />
 <variable
 name="testData"
 type="com.test.TestData" />
 </data>
 <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".TestActivity">
​
 <!--在布局中的使用-->
 <TextView
 android:text="@{testData.test}"
 android:textSize="20sp"
 app:layout_constraintBottom_toBottomOf="parent"
 app:layout_constraintTop_toTopOf="parent"
 app:layout_constraintEnd_toEndOf="parent"
 app:layout_constraintStart_toStartOf="parent"
 android:id="@+id/textView"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 />
 </androidx.constraintlayout.widget.ConstraintLayout>
</layout>
ActivityTestBinding viewDataBinding = DataBindingUtil.setContentView(this, R.layout.activity_test);
viewDataBinding.setTestData(new TestData("DataBinding的使用"));

三、DataBinding高级用法

DataBinding还有更高级的用法,你也可以导入你自己定义的类,并在layout中使用任何你导入的类,但是还有一种就是通过DataBinding实现数据双向绑定,或者跟LiveData配合一起使用。

这里DataBinding简单的用法介绍结束了,详细用法建议去官网看DataBinding的使用。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。