前言
DataBinging是jetpack的一个组件,意在降低xml和活动的耦合性,让我们来写一个小demo来认识一下databinding,熟悉一下使用流程
项目流程
1.gradle中对DataBinding进行配置
在该文件的defaultConfig中添加下面的命令
dataBinding {
enabled = true
}
2.xml页面
效果如下:
点击next按钮可以切换图片,还有昵称(效果图中没显示)
xml:
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<variable
name="beauty"
type="com.lbj23.myapplication.beans.Beauty" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="207dp"
android:layout_height="250dp"
android:layout_marginTop="80dp"
android:contentDescription="@string/beauty_img"
android:scaleType