DataBinding(一)
一、应用 DataBinding + LiveData 双向监听
- 1.1、在
build.gradle
中配置
dataBinding {
enabled = true
}
- 1.2、model 类
public class Student {
public final ObservableField<String> name = new ObservableField<>();
public void setText(String text) {
name.set(text);
}
}
- 1.3、ViewModel 类
public class MyViewModule extends ViewModel {
public MutableLiveData<Student> mStudent = new MutableLiveData<>();
public MyViewModule() {
mStudent.setValue(new Student());
mStudent.getValue().setText("ssss");
}
public void onStudentClick(View view){
mStudent.getValue().setText("aaaaa");
}
}
- 1.4、布局类
<?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 class="DataBindingActivityBinging">
<variable
name="viewModel"
type="com.example.designdemo.jetpack.databinding.MyViewModule" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".jetpack.databinding.DataBindingActivity">
<TextView
android:id="@+id/name_tv"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="@{viewModel.mStudent.name}"
android:textSize="25sp"
android:layout_marginTop="30dp"
android:background="#EA0707"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/name_et"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="@={viewModel.mStudent.name}"
android:textSize="25sp"
android:layout_marginTop="30dp"
android:background="#31EA07"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/name_tv" />
<Button
android:id="@+id/stu_btn"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="点击"
android:textSize="25sp"
android:onClick="@{(v)->viewModel.onStudentClick(v)}"
android:layout_marginTop="30dp"
android:background="#31EA07"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/name_et" />
<ImageView
android:id="@+id/stu_img"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="30dp"
android:background="#31EA07"
android:gravity="center"
android:text="点击"
android:textSize="25sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/stu_btn"
app:setImageView="@{R.drawable.circle_blue}" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
- 1.5、实现类
public class DataBindingActivity extends AppCompatActivity {
private static final String TAG = "DataBindingActivity";
public static void startDataBindingActivity(Activity activity){
Intent intent = new Intent(activity, DataBindingActivity.class);
activity.startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DataBindingActivityBinging binging = DataBindingUtil.setContentView(this,
R.layout.activity_data_binding);
MyViewModule myViewModule = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory())
.get(MyViewModule.class);
//绑定 ViewModel
binging.setViewModel(myViewModule);
//绑定生命周期 Lifecycle,感应变化
binging.setLifecycleOwner(this);
myViewModule.mStudent.observe(this, new Observer<Student>() {
@Override
public void onChanged(Student student) {
Log.i(TAG, student.name.get());
}
});
}
}
- BindingAdapter 的使用
public class MyDataBindAdapter {
//这里 requireAll = false 表示我们可以使用这两个两个属性中的任一个或同时使用,
//如果requireAll = true 则两个属性必须同时使用,不然报错。默认为 true。
@BindingAdapter(value = {"setImageView"}, requireAll = false)
public static void setImageView(ImageView view, int id){
view.setBackgroundResource(id);
}
}
二、原理分析
2.1、布局文件分析
假如我们XML的布局是下面的样式:
<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">
//是给 databinding 用的
<data class="JetpackActivityBinding">
<variable
name="student"
type="com.example.designdemo.jetpack.databinding.Student" />
</data>
//给Activity 用的
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".jetpack.JetpackActivity">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/lifecycle"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="20dp"
android:text="Lifecycle"
android:textAllCaps="false"
android:textSize="18sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/data_bind_btn"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="20dp"
android:text="dataBinding"
android:textAllCaps="false"
android:textSize="18sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/lifecycle" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
会在app\build\intermediates\data_binding_layout_info_type_merge\debug\out
目录中生产对应XML
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Layout bindingClass="JetpackActivityBinding" directory="layout"
filePath="app\src\main\res\layout\activity_jetpack.xml" isBindingData="true"
isMerge="false" layout="activity_jetpack" modulePackage="com.example.designdemo"
rootNodeType="androidx.constraintlayout.widget.ConstraintLayout">
<ClassNameLocation endLine="5" endOffset="38" startLine="5" startOffset="17" />
<Variables name="student" declared="true"
type="com.example.designdemo.jetpack.databinding.Student">
<location endLine="8" endOffset="71" startLine="6" startOffset="8" />
</Variables>
<Targets>
<Target tag="layout/activity_jetpack_0"
view="androidx.constraintlayout.widget.ConstraintLayout">
<Expressions />
<location endLine="53" endOffset="55" startLine="12" startOffset="4" />
</Target>
<Target id="@+id/lifecycle" view="Button">
<Expressions />
<location endLine="35" endOffset="63" startLine="25" startOffset="16" />
</Target>
<Target id="@+id/data_bind_btn" view="Button">
<Expressions />
<location endLine="48" endOffset="73" startLine="38" startOffset="16" />
</Target>
</Targets>
</Layout>
可以看到,这里定义了多个Target标签,这些Target的定义,其实就是定义对应的tag,将tag与自己的布局中的对应的View的id对应起来经过DataBinding变化后的布局,会多出tag。
最后会在app/build/imtermediates/incremental/mergeDebugResources/stripped.dir/layout/activity_main.xml
中生成共我们Activity用的布局文件
<?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"
android:tag="layout/activity_jetpack_0"
tools:context=".jetpack.JetpackActivity">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/lifecycle"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="20dp"
android:text="Lifecycle"
android:textAllCaps="false"
android:textSize="18sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/data_bind_btn"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="20dp"
android:text="dataBinding"
android:textAllCaps="false"
android:textSize="18sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/lifecycle" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
2.2、代码分析
首先有此处为入口
DataBindingActivityBinging binging = DataBindingUtil.setContentView(this,R.layout.activity_data_binding);`
进入DataBindingUtil.setContentView
,其实DataBindingUtil的setContentView()方法,主要就是调用activity的setContentView设置布局,并且绑定添加对应的View
public static <T extends ViewDataBinding> T setContentView(@NonNull Activity activity,
int layoutId) {
return setContentView(activity, layoutId, sDefaultComponent);
}
public static <T extends ViewDataBinding> T setContentView(@NonNull Activity activity,
int layoutId, @Nullable DataBindingComponent bindingComponent) {
// 调用Activity的setContentView设置布局
activity.setContentView(layoutId);
View decorView = activity.getWindow().getDecorView();
// 获取 DecorView 中的 content (根布局)
ViewGroup contentView = (ViewGroup) decorView.findViewById(android.R.id.content);
return bindToAddedViews(bindingComponent, contentView, 0, layoutId);
}
DataBindingUtil.bindToAddedViews()
private static <T extends ViewDataBinding> T bindToAddedViews(DataBindingComponent component,
ViewGroup parent, int startChildren, int layoutId) {
final int endChildren = parent.getChildCount();
final int childrenAdded = endChildren - startChildren;
// 如果childrenAdded==1,则就只有一个子View
// 如果不等于1,则有多个
if (childrenAdded == 1) {
final View childView = parent.getChildAt(endChildren - 1);
return bind(component, childView, layoutId);
} else {
final View[] children = new View[childrenAdded];
//遍历每个布局
for (int i = 0; i < childrenAdded; i++) {
children[i] = parent.getChildAt(i + startChildren);
}
return bind(component, children, layoutId);
}
}
然后到DataBindingUtil.bind()
static <T extends ViewDataBinding> T bind(DataBindingComponent bindingComponent, View[] roots,
int layoutId) {
return (T) sMapper.getDataBinder(bindingComponent, roots, layoutId);
}
static <T extends ViewDataBinding> T bind(DataBindingComponent bindingComponent, View root,
int layoutId) {
return (T) sMapper.getDataBinder(bindingComponent, root, layoutId);
}
这里的sMapper是一个DataBinderMapper对象,其实现类是
DataBinderMapperImpl,DataBinderMapperImpl是通过apt注解处理器生成的。这里的sMapper.getDataBinder()其实就是调用的MergedDataBinderMapper的getDataBinder()方法而 sMapper中的数据,其实就是DataBinderMapperImpl的构造器中调用其父类。
MergedDataBinderMapper
的addMapper()
方法添加的对象
public class DataBinderMapperImpl extends MergedDataBinderMapper {
DataBinderMapperImpl() {
addMapper(new com.example.designdemo.DataBinderMapperImpl());
}
}
在DataBinding中有两·个DataBinderMapperImpl·类,一个是上面这个在androidx.databinding
包下,继承了MergedDataBinderMapper
的,另一个是在com.example.databindingdemo
应用包下,直接继承DataBinderMapper
。其实MergedDataBinderMapper
也是继承自DataBinderMapper
。
- 调用
MergedDataBinderMapper.getDataBinder()
public ViewDataBinding getDataBinder(DataBindingComponent bindingComponent, View view,
int layoutId) {
// mMappers集合中的数据就是来源于androidx.databinding.DataBinderMapperImpl
// 的构造器中调用addMapper方法传入的对象添加的
// 所以这里的mapper就是com.example.databindingdemo.DataBinderMapperImpl对象
for(DataBinderMapper mapper : mMappers) {
ViewDataBinding result = mapper.getDataBinder(bindingComponent, view, layoutId);
if (result != null) {
return result;
}
}
if (loadFeatures()) {
return getDataBinder(bindingComponent, view, layoutId);
}
return null;
}
我们可以看上面getDataBinder
明显少了tag的解析。实际执行的是build\generated\ap_generated_sources\debug\out
中的DataBinderMapperImpl.getDataBinder
这里要注意两点,就是如果是布局的顶层View,比如tag为layout/activity_main_0,那么就会new一个ActivityMainBindingImpl对象。
@Override
public ViewDataBinding getDataBinder(DataBindingComponent component, View view, int layoutId) {
int localizedLayoutId = INTERNAL_LAYOUT_ID_LOOKUP.get(layoutId);
if(localizedLayoutId > 0) {
final Object tag = view.getTag();
if(tag == null) {
throw new RuntimeException("view must have a tag");
}
switch(localizedLayoutId) {
case LAYOUT_ACTIVITYDATABINDING: {
if ("layout/activity_data_binding_0".equals(tag)) {
return new DataBindingActivityBingingImpl(component, view);
}
throw new IllegalArgumentException("The tag for activity_data_binding is invalid. Received: " + tag);
}
case LAYOUT_ACTIVITYJETPACK: {
if ("layout/activity_jetpack_0".equals(tag)) {
return new JetpackActivityBindingImpl(component, view);
}
throw new IllegalArgumentException("The tag for activity_jetpack is invalid. Received: " + tag);
}
}
}
return null;
}
@Override
public ViewDataBinding getDataBinder(DataBindingComponent component, View[] views, int layoutId) {
if(views == null || views.length == 0) {
return null;
}
int localizedLayoutId = INTERNAL_LAYOUT_ID_LOOKUP.get(layoutId);
if(localizedLayoutId > 0) {
final Object tag = views[0].getTag();
if(tag == null) {
throw new RuntimeException("view must have a tag");
}
switch(localizedLayoutId) {
}
}
return null;
}
我们看JetpackActivityBindingImpl
构造器的方法,进行一些View的绑定操作,将通过tag取出的View与JetpackActivityBindingImpl中对应的View属性进行绑定。
public JetpackActivityBindingImpl(@Nullable androidx.databinding.DataBindingComponent bindingComponent, @NonNull View root) {
this(bindingComponent, root, mapBindings(bindingComponent, root, 3, sIncludes, sViewsWithIds));
}
private JetpackActivityBindingImpl(androidx.databinding.DataBindingComponent bindingComponent, View root, Object[] bindings) {
super(bindingComponent, root, 0
, (android.widget.Button) bindings[2]
, (android.widget.Button) bindings[1]
);
this.mboundView0 = (androidx.constraintlayout.widget.ConstraintLayout) bindings[0];
this.mboundView0.setTag(null);
setRootTag(root);
// listeners
invalidateAll();
}
在这里,会调用了一个mapBindings
方法,第三个参数是一个3,这个3的意思是activity_main.xml布局文件中有3个节点。
ViewDataBinding.mapBinding()
这里的主要工作,就是将布局中的View保存在对应的bindings数组中,然后取出这个数组中的数据赋值给ActivityMainBindingImpl中的View。
//mapBindings就会返回一个Object[] bindings数组
private static void mapBindings(DataBindingComponent bindingComponent, View view,
Object[] bindings, ViewDataBinding.IncludedLayouts includes, SparseIntArray viewsWithIds,
boolean isRoot) {
final int indexInIncludes;
// 判断View是否已经存在绑定,如果已经绑定,则直接return
final ViewDataBinding existingBinding = getBinding(view);
if (existingBinding != null) {
return;
}
// 获取View的tag标签
Object objTag = view.getTag();
final String tag = (objTag instanceof String) ? (String) objTag : null;
boolean isBound = false;
// 如果tag是根布局,并且是以layout开头的tag
if (isRoot && tag != null && tag.startsWith("layout")) {
final int underscoreIndex = tag.lastIndexOf('_');
if (underscoreIndex > 0 && isNumeric(tag, underscoreIndex + 1)) {
final int index = parseTagInt(tag, underscoreIndex + 1);
// 将根布局标签对应的View放在bindings数组中
if (bindings[index] == null) {
bindings[index] = view;
}
indexInIncludes = includes == null ? -1 : index;
isBound = true;
} else {
indexInIncludes = -1;
}
} else if (tag != null && tag.startsWith(BINDING_TAG_PREFIX)) {
int tagIndex = parseTagInt(tag, BINDING_NUMBER_START);
if (bindings[tagIndex] == null) {
bindings[tagIndex] = view;
}
isBound = true;
indexInIncludes = includes == null ? -1 : tagIndex;
} else {
// Not a bound view
indexInIncludes = -1;
}
if (!isBound) {
final int id = view.getId();
if (id > 0) {
int index;
if (viewsWithIds != null && (index = viewsWithIds.get(id, -1)) >= 0 &&
bindings[index] == null) {
bindings[index] = view;
}
}
}
if (view instanceof ViewGroup) {
final ViewGroup viewGroup = (ViewGroup) view;
final int count = viewGroup.getChildCount();
int minInclude = 0;
for (int i = 0; i < count; i++) {
final View child = viewGroup.getChildAt(i);
boolean isInclude = false;
if (indexInIncludes >= 0 && child.getTag() instanceof String) {
String childTag = (String) child.getTag();
if (childTag.endsWith("_0") &&
childTag.startsWith("layout") && childTag.indexOf('/') > 0) {
// This *could* be an include. Test against the expected includes.
int includeIndex = findIncludeIndex(childTag, minInclude,
includes, indexInIncludes);
if (includeIndex >= 0) {
isInclude = true;
minInclude = includeIndex + 1;
final int index = includes.indexes[indexInIncludes][includeIndex];
final int layoutId = includes.layoutIds[indexInIncludes][includeIndex];
int lastMatchingIndex = findLastMatching(viewGroup, i);
if (lastMatchingIndex == i) {
bindings[index] = DataBindingUtil.bind(bindingComponent, child,
layoutId);
} else {
final int includeCount = lastMatchingIndex - i + 1;
final View[] included = new View[includeCount];
for (int j = 0; j < includeCount; j++) {
included[j] = viewGroup.getChildAt(i + j);
}
bindings[index] = DataBindingUtil.bind(bindingComponent, included,
layoutId);
i += includeCount - 1;
}
}
}
}
if (!isInclude) {
mapBindings(bindingComponent, child, bindings, includes, viewsWithIds, false);
}
}
}
}