参考:https://www.jianshu.com/p/70316eb4e0f8
- 在app目录下的 build.gradle 中,添加
android { ...
dataBinding {
enabled = true
}
}
新版本统一改为:
android { ...
buildFeatures {
dataBinding true
// for view binding:
// viewBinding true
}
}
- 在 xml 文件的最外层根布局,使用 … 标签(可以使用 alt + enter 的快捷键,选择 convert to data binding layout)
原本的xml:
<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">
<TextView
android:id="@+id/tv_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
变更后的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>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
- 在 xml 文件的data标签中,创建 标签,定义需要使用到的对象,如:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import type="java.util.List" />
<import type="java.util.Map" />
<!--泛型的支持会在编译时期报红线,但是是可以直接运行的
但是需要通过转义字符才行,如:<号用<表示;>号用>表示;-->
<variable
name="list"
type="List<String>" />
<variable
name="map"
type="Map<String,Object>" />
<variable
name="array"
type="String[]" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{list[0]}" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{list.get(1)}" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@{map[`key0`]}" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{map.get(`key1`)}" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@{array[0]}" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{array[1]}" />
</LinearLayout>
</layout>
- 在Activity中初始化数据,设置这些变量
public class BasicActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityBasicBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_basic);
List<String> list = new ArrayList<>();
list.add("list1");
list.add("list2");
binding.setList(list);
HashMap<String, Object> map = new HashMap<>();
map.put("key0", "map_value0");
map.put("key1", "map_value1");
binding.setMap(map);
String[] arrays = {"字符串1", "字符串2"};
binding.setArray(arrays);
}
}
DataBinding在RecyclerView Adapter中的使用
https://my.oschina.net/u/4356937/blog/3792207
https://www.jianshu.com/p/4d30efa6b500
https://cloud.tencent.com/developer/article/1200774