DataBinding的基本使用

目录

  • 一、DataBinding的应用场景
  • 二、MVC、MVP和MVVM框架的使用场景
  • 三、DataBinding的使用
    • 1. Java版本的使用
    • 2、Kotlin版本的使用
  • 四、DataBinding源码分析
    • 1. DataBindingUtil.setContentView的原理
    • 2. binding.setUser(user) 解析

一、DataBinding的应用场景

  DataBinding是谷歌15年推出的Library。DataBinding支持双向绑定,能大大减少绑定app逻辑与layout文件的“胶水代码”,例如setText、findViewById等代码。双向绑定,指的是将数据与界面绑定起来,当数据发生变化时体现再界面上,反过来界面内容变化也会同步更新到数据上,使用DataBinding能轻松实现MVVM模式。MVVM模式中ViewModel(VM)表示数据,View(V)表示界面。

为什么之前很少人使用DataBinding?
答:因为它之前非常卡顿,而且项目布局越多越大,越卡

二、MVC、MVP和MVVM框架的使用场景

  • MVC: 适用于小型项目,够灵活,
    缺点:Activity不仅要做View的事情还要做控制和模型的处理,导致Activity太过臃肿,管理混乱,没有单一职责,耦合度、扩展性都不太好

  • MVP:适用于大型项目、业务很重、项目很大,必须分层清楚,因为开发的人太好
    优点:分层特别清晰
    缺点:出现接口过多、接口地狱问题

  • MVVM: 适用于界面更新很频繁,比如网易云音乐、腾讯视频
    DataBinding 是一个工具集,不属于哪个框架,在MVVM框架中用于完成View层与ViewModel层的双向绑定
    大部分情况下,MVVM框架都会使用DataBinding;小部分情况下,MVP框架也可以使用

新的开发模块:数据驱动开发(DataBinding + ViewModel + LiveData)

特别注意:MVVM模式下的ViewModel(VM)和 Jetpack的ViewModel 不是同一东西,MVVM中的ViewModel是层次结构,而Jetpack的ViewModel是组件库。

永远没有哪个模式最好,只有哪个模式适合你的项目才是最好的

三、DataBinding的使用

1. Java版本的使用

app目录下的build.gradle文件中开启DataBinding

apply plugin: 'com.android.application'

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.example.mydatabinding_java"
        minSdkVersion 26
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    // 打开DataBinding开关
    // 第一种方式
    /*dataBinding {
        enabled true
    }*/

    // 第二种方式
    dataBinding.enabled = true
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

}

User.java

package com.example.mydatabinding_java.model;

import com.example.mydatabinding_java.BR;

import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;
// Model
public class User extends BaseObservable {
    private String name;
    private String pwd;

    public User(String name, String pwd) {
        this.name = name;
        this.pwd = pwd;
    }

    @Bindable // BR里面标记生成 name数值标记
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
        notifyPropertyChanged(BR.name);  // ART注解处理器技术 生成BR文件
    }

    @Bindable // BR里面标记生成 pwd数值标记
    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
        notifyPropertyChanged(BR.pwd);   // ART注解处理器技术 生成BR文件
    }
}

activity_main.xml布局文件:




    
        
        
        
        
    

    

    


    

        
        

        

    

编译之后会生成两个布局文件

  1. app/build/intermediates/data_binding_layout_info_type_merge/debug/out/ 目录下生成 activity_main-layout.xml文件
    activity_main-layout.xml


    
        
    
    
        
            
            
        
        
            
                
                    
                    true
                    
                
            
            
        
        
            
                
                    
                    true
                    
                
            
            
        
    

  这个xml文件就相当于activity_main.xml布局文件中的这一截里面的内容。
  可以看到,这里定义了多个Target标签,这些Target的定义,其实就是定义对应的tag,将tag与activity_main.xml布局中的对应的View的id对应起来经过DataBinding变化后的布局,会多出tag。

  1. app/build/intermediates/incremental/mergeDebugResources/stripped.dir/layout/ 目录下生成 activity_main.xml文件,这个xml就相当于原activity_main.xml布局文件中的这一截里面的内容
    activity_main.xml










    
    

    


         

MainActivity.java

package com.example.mydatabinding_java;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.os.Bundle;

import com.example.mydatabinding_java.databinding.ActivityMainBinding;
import com.example.mydatabinding_java.model.User;

public class MainActivity extends AppCompatActivity {

    User user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

        user = new User("admin", "123456");
        binding.setUser(user);  // 必须建立绑定关系,否则没有任何效果,这里的setUser函数是在activity_main.xml布局文件中layout里面写了name="user"而生成的

        // Model(修改Model数据) -> View(UI的控件 DataBinding自动刷选)  一向 更新
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    try {
                        Thread.sleep(1000);
                        user.setName(user.getName() + "啊");
                        user.setPwd(user.getPwd() + "啊");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

    }
}

2、Kotlin版本的使用

StudentInfo.kt

package com.example.mydatabinding.model

import androidx.databinding.BaseObservable
import androidx.databinding.ObservableField

class StudentInfo : BaseObservable() {
    // 第一种方式 未修复 KT放弃第一种方式
    var name : String? = null
        get() {
            return field
        }
        set(value) {
            field = value
            // notifyPropertyChanged(BR.name)
        }
    var pwd : String? = null
        get() {
            return field
        }
        set(value : String?) {
            field = value
        }

    // 第二种方式 已修复 懒加载
    val nameF : ObservableField by lazy { ObservableField() }
    val pwdF : ObservableField by lazy { ObservableField()  }
}

Kotlin目前只能采用第二种方式

activity_main.xml





    
    
        
        
    

    

    
    

    

        
        

        

        
        
        


        
        
        

        
        
    

MainActivity.kt

package com.example.mydatabinding

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.util.Log
import androidx.databinding.DataBindingUtil
import com.example.mydatabinding.databinding.ActivityMainBinding
import com.example.mydatabinding.model.StudentInfo

class MainActivity : AppCompatActivity() {
    private val TAG = MainActivity::class.java.simpleName

    private val studentInfo : StudentInfo = StudentInfo()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // setContentView(R.layout.activity_main)
        // 这里必须要指定类型为ActivityMainBinding
        val binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

        // TODO 单向绑定第一种方式: Model -> View
        studentInfo.name = "admin"
        studentInfo.pwd = "123456"
        binding.studentInfo = studentInfo

        // 数据的刷选 发现界面没有效果 目前:界面不会根据Model的变化而变化
        // 未修复
        Log.d(TAG, "name:${studentInfo.name}, pwd:${studentInfo.pwd}")
        Handler().postDelayed({
            studentInfo.name = "test1"
            studentInfo.pwd = "11111"
        }, 2000)

        // 修复后: Model -> View 一向
        Log.d(TAG, "name:${studentInfo.name}, pwd:${studentInfo.pwd}")
        Handler().postDelayed({
            studentInfo.nameF.set("test2")
            studentInfo.pwdF.set("22222");
        }, 2000)


        // TODO 双向绑定 (一向:Model -> View   一向:View -> Model)
        studentInfo.nameF.set("zhangsan")
        studentInfo.pwdF.set("123456")
        binding.studentInfo = studentInfo
        Log.d(TAG, "name:${studentInfo.name}, pwd:${studentInfo.pwd}")

        Handler().postDelayed({
            Log.d(TAG, "name:${studentInfo.name}, pwd:${studentInfo.pwd}")
        }, 4000)
    }
}

四、DataBinding源码分析

1. DataBindingUtil.setContentView的原理

MainActivity.java

ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

(1)DataBindingUtil.setContentView()
  其实DataBindingUtil的setContentView()方法,主要就是调用activity的setContentView设置布局,并且绑定添加对应的View

DataBindingUtil.java

	public static  T setContentView(@NonNull Activity activity,
            int layoutId) {
        return setContentView(activity, layoutId, sDefaultComponent);
    }

    public static  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);
    }

(2)DataBindingUtil.bindToAddedViews()

DataBindingUtil.java

    private static  T bindToAddedViews(DataBindingComponent component,
            ViewGroup parent, int startChildren, int layoutId) {
        final int endChildren = parent.getChildCount();
        final int childrenAdded = endChildren - startChildren;
        // 如果childAdded ==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);
        }
    }

(3)DataBindingUtil.bind()

DataBindingUtil.java

    static  T bind(DataBindingComponent bindingComponent, View[] roots,
            int layoutId) {
        return (T) sMapper.getDataBinder(bindingComponent, roots, layoutId);
    }
    
    static  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的appMapper()方法添加的对象。

DataBinderMapperImpl.java

public class DataBinderMapperImpl extends MergedDataBinderMapper {
  DataBinderMapperImpl() {
    addMapper(new com.example.mydatabinding_java.DataBinderMapperImpl());
  }
}

  在DataBinding中有两个DataBinderMapperImpl类,一个是上面这个在androidx.databinding包下,继承了MergedDataBinderMapper的,另一个是在com.example.mydatabinding_java应用包下,直接继承DataBinderMapper。其实MergedDataBindermaper也是继承自DataBinderMapper。

(4)MergedDataBinderMapper.getDataBinder()
MergedDataBinderMapper.java

    @Override
    public ViewDataBinding getDataBinder(DataBindingComponent bindingComponent, View[] view,
            int layoutId) {
        // mMapperrs集合中的数据就是来源于androidx.databinding.DataBinderMapperImpl的构造器中调用addMapper方法传入的对象添加的
        // 所以这里的mapper就是com.exmaple.mydatabinding_java.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;
    }

(5)app/build/generated/ap_generated_sources/debug/out/目录下com.exmaple.mydatabinding_java.DataBinderMapperImpl.getDataBinder
  这里需要注意两点,就是如果是布局的顶层View,比如tag为layout/activity_main_0,那么就会new一个ActivityMainBindingImpl对象,这个tag,其实可以从前面看到的app/build/intermediates/incremental/mergeDebugResources/stripped.dir/layout/activity_main.xml布局总的LinearLayout的tag知道

  @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_ACTIVITYMAIN: {
          if ("layout/activity_main_0".equals(tag)) {
            return new ActivityMainBindingImpl(component, view);
          }
          throw new IllegalArgumentException("The tag for activity_main 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;
  }

(6)ActivityMainBindingImpl的构造器
  在new出ActivityMainBindingImpl对象后,则进行一些View的绑定操作,将通过tag取出的View与ActivityMainBindingImpl中对应的View属性进行绑定。
ActivityMainBindingImpl.java

    public ActivityMainBindingImpl(@Nullable androidx.databinding.DataBindingComponent bindingComponent, @NonNull View root) {
        this(bindingComponent, root, mapBindings(bindingComponent, root, 3, sIncludes, sViewsWithIds));
    }

  在这里,会调用一个mapBindings方法,第三个参数是一个3,表示activity_main.xml布局文件中有3个节点

mapBindings(bindingComponent, root, 3, sIncludes, sViewsWithIds)

  mapBindings就会返回一个Object[] bindings数组,保存所有控件的内存信息,这个就是第一个内存消耗过大的地方,它会参考activity_main-layout.xml和activity_main.xml布局做大量的解析工作,从而产生Object数组,保存控件。 布局越大,所耗的内存越多

  第二个耗内存的地方是ViewDataBinding类中有个静态代码块

    static {
        if (VERSION.SDK_INT < VERSION_CODES.KITKAT) {
            ROOT_REATTACHED_LISTENER = null;
        } else {
            ROOT_REATTACHED_LISTENER = new OnAttachStateChangeListener() {
                @TargetApi(VERSION_CODES.KITKAT)
                @Override
                public void onViewAttachedToWindow(View v) {
                    // execute the pending bindings.
                    final ViewDataBinding binding = getBinding(v);
                    binding.mRebindRunnable.run();
                    v.removeOnAttachStateChangeListener(this);
                }

                @Override
                public void onViewDetachedFromWindow(View v) {
                }
            };
        }
    }
  

  每次变动都会执行onAttachStateChangeListener()方法,从而执行回调方法onViewAttachedToWindow()方法来触发binding.mRebindRunnable.run()方法进行处理,这是非常耗内存的

(7)ViewDataBinding.mapBinding()
ViewDataBinding.java

    protected static Object[] mapBindings(DataBindingComponent bindingComponent, View root,
            int numBindings, IncludedLayouts includes, SparseIntArray viewsWithIds) {
        Object[] bindings = new Object[numBindings];
        mapBindings(bindingComponent, root, bindings, includes, viewsWithIds, true);
        return bindings;
    }

    private static void mapBindings(DataBindingComponent bindingComponent, View view,
            Object[] bindings, 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);
                }
            }
        }
    }

  ActivityMainBindingImpl的父类ActivityMainBinding是在app/build/generated/ap_generated_sources/debug/out/com/example/mydatabinding_java/databinding包下

ActivityMainBinding.java

// Generated by data binding compiler. Do not edit!
package com.example.mydatabinding_java.databinding;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.Bindable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import com.example.mydatabinding_java.R;
import com.example.mydatabinding_java.model.User;
import java.lang.Deprecated;
import java.lang.Object;

public abstract class ActivityMainBinding extends ViewDataBinding {
  @NonNull
  public final EditText et1;

  @NonNull
  public final EditText et2;

  @Bindable
  protected User mUser;

  protected ActivityMainBinding(Object _bindingComponent, View _root, int _localFieldCount,
      EditText et1, EditText et2) {
    super(_bindingComponent, _root, _localFieldCount);
    this.et1 = et1;
    this.et2 = et2;
  }

  public abstract void setUser(@Nullable User user);

  @Nullable
  public User getUser() {
    return mUser;
  }

  @NonNull
  public static ActivityMainBinding inflate(@NonNull LayoutInflater inflater,
      @Nullable ViewGroup root, boolean attachToRoot) {
    return inflate(inflater, root, attachToRoot, DataBindingUtil.getDefaultComponent());
  }

  /**
   * This method receives DataBindingComponent instance as type Object instead of
   * type DataBindingComponent to avoid causing too many compilation errors if
   * compilation fails for another reason.
   * https://issuetracker.google.com/issues/116541301
   * @Deprecated Use DataBindingUtil.inflate(inflater, R.layout.activity_main, root, attachToRoot, component)
   */
  @NonNull
  @Deprecated
  public static ActivityMainBinding inflate(@NonNull LayoutInflater inflater,
      @Nullable ViewGroup root, boolean attachToRoot, @Nullable Object component) {
    return ViewDataBinding.inflateInternal(inflater, R.layout.activity_main, root, attachToRoot, component);
  }

  @NonNull
  public static ActivityMainBinding inflate(@NonNull LayoutInflater inflater) {
    return inflate(inflater, DataBindingUtil.getDefaultComponent());
  }

  /**
   * This method receives DataBindingComponent instance as type Object instead of
   * type DataBindingComponent to avoid causing too many compilation errors if
   * compilation fails for another reason.
   * https://issuetracker.google.com/issues/116541301
   * @Deprecated Use DataBindingUtil.inflate(inflater, R.layout.activity_main, null, false, component)
   */
  @NonNull
  @Deprecated
  public static ActivityMainBinding inflate(@NonNull LayoutInflater inflater,
      @Nullable Object component) {
    return ViewDataBinding.inflateInternal(inflater, R.layout.activity_main, null, false, component);
  }

  public static ActivityMainBinding bind(@NonNull View view) {
    return bind(view, DataBindingUtil.getDefaultComponent());
  }

  /**
   * This method receives DataBindingComponent instance as type Object instead of
   * type DataBindingComponent to avoid causing too many compilation errors if
   * compilation fails for another reason.
   * https://issuetracker.google.com/issues/116541301
   * @Deprecated Use DataBindingUtil.bind(view, component)
   */
  @Deprecated
  public static ActivityMainBinding bind(@NonNull View view, @Nullable Object component) {
    return (ActivityMainBinding)bind(component, view, R.layout.activity_main);
  }
}

2. binding.setUser(user) 解析

BR文件
生成的路径:app/build/generated/ap_generated_sources/debug/out/com.example.mydatabinding_java/databinding/BR.java
BR的作用:就用BR中的属性值来标记不同的操作需要的监听在mLocalFieldObservers数组中的位置

// binding.setVariable(BR.name, "21341234123")
// binding.setUser(user)
// 这里的BR,其实代表的含义不同,如果是BR.user,则也是会修改整个User数据
// 如果是binding.setUser(),此时就是修改了整个DataBinding的数据
// 如果是BR.name,则是修改了name的数据

package com.example.mydatabinding_java;

public class BR {
  public static final int _all = 0;

  public static final int name = 1;

  public static final int pwd = 2;

  public static final int user = 3;
}

(1)ActivityMainBindingImpl.setUser()

app/build/generated/ap_generated_sources/debug/out/com.example.mydatabinding_java/databinding/databinding/ActivityMainBindingImpl.java

ActivityMainBinding类是抽象类,具体实现类是ActivityMainBindingImpl类

ActivityMainBindingImpl.java

    public void setUser(@Nullable com.example.mydatabinding_java.model.User User) {
       // 更新注册
        updateRegistration(0, User);
        this.mUser = User;
        synchronized(this) {
            mDirtyFlags |= 0x1L;
        }
        notifyPropertyChanged(BR.user);
        super.requestRebind();
    }

(2)ViewDataBinding.updateRegistration()

ViewDataBinding.java

    // 使用BR属性的值做index,存储每个BR的属性对应的监听器
    private WeakListener[] mLocalFieldObservers;
    
    // 创建属性的监听器
    /**
     * Method object extracted out to attach a listener to a bound Observable object.
     */
    private static final CreateWeakListener CREATE_PROPERTY_LISTENER = new CreateWeakListener() {
        @Override
        public WeakListener create(ViewDataBinding viewDataBinding, int localFieldId) {
            // 返回一个属性监听
            return new WeakPropertyListener(viewDataBinding, localFieldId).getListener();
        }
    };


    /**
     * @hide
     */
    protected boolean updateRegistration(int localFieldId, Observable observable) {
        return updateRegistration(localFieldId, observable, CREATE_PROPERTY_LISTENER);
    }

  这里的localFieldId = 0,这个id其实就是BR文件中的id,就是BR文件中对应的静态final属性的值。而第二个就是观察者对象,比如传入的ViewModel对象。

ViewDataBinding.java

    private boolean updateRegistration(int localFieldId, Object observable,
            CreateWeakListener listenerCreator) {
        if (observable == null) {
            return unregisterFrom(localFieldId);
        }
        // 根据BR的每个属性的属性值做index,存储每个BR属性对应的监听器
        WeakListener listener = mLocalFieldObservers[localFieldId];
        if (listener == null) {
            registerTo(localFieldId, observable, listenerCreator);
            return true;
        }
        if (listener.getTarget() == observable) {
            return false;//nothing to do, same object
        }
        unregisterFrom(localFieldId);
        registerTo(localFieldId, observable, listenerCreator);
        return true;
    }

	// 这个registerTo方法,其实就是将Activity这个观察者和User这个观察者统一添加到OnservableReference中
    protected void registerTo(int localFieldId, Object observable,
            CreateWeakListener listenerCreator) {
        if (observable == null) {
            return;
        }
        WeakListener listener = mLocalFieldObservers[localFieldId];
        if (listener == null) {
        	// 通过属性监听器的创建器创建一个BR属性值对应的监听器
        	// listener就是WeakPropertyListener对象的getListener返回的
        	// 就是WeakListener对象
        	// 在创建WeakPropertyListener的时候,其构造器内部就会创建
        	// WeakListener对象,并且将WeakPropertyListener对象传给WeakListener
        	// 这样WeakListener和WeakPropertyListener就相互持有
            listener = listenerCreator.create(this, localFieldId);
            mLocalFieldObservers[localFieldId] = listener;
            // 将监听器与观察者做绑定,这里的观察者其实就是Activity
            if (mLifecycleOwner != null) {
                listener.setLifecycleOwner(mLifecycleOwner);
            }
        }
        // 将该监听器与被观察者做绑定
        // 比如这里的被观察者就是User
        listener.setTarget(observable);
    }

  这里
通过WeakListener监听器中的ObservableReference对象保存观察者与被观察者,当被观察者发生改变的时候,就会找到对应的WeakListener监听器,然后通知观察者做修改。而ObservableReference方法的实现,有多个,比如:WeakPropertyListener。这里让WeakListener.setTarget()其实就是通过WeakPropertyListener给被观察者添加callback,然后当被观察者数据发生改变的时候,被观察者通过遍历其内部的PropertyChangeRegistry中的OnPropertyChangedCallback回调(其实就是WeakPropertyListener),然后通过WeakPropertyListener监听通知给ViewDataBinding以及其实现类ActivityMainBindingImpl具体进行数据的处理和设置。

(3)WeakListener.setTarget()给Observable添加回调
  这里的mTarget其实是一个泛型T对象,而这个泛型是在WeakPropertyListener初始化WeakListener的时候传入的一个Observable,这个是databinding中的Observable,其子类实现就是BaseObservable

ViewDataBinding.java

   // 这里的mObservable就是weakPropertyListener对象
   private final ObservableReference mObservable;

   public void setTarget(T object) {
       unregister();
       mTarget = object;
       if (mTarget != null) {
           mObservable.addListener(mTarget);
       }
   }

  WeakPropertyListener中的addListener方法,就会给Observable添加一个callback回调,向Observable这个观察者中添加callback的目的,就是在Observable数据发生变化的时候,遍历Observable中的mCallbacks这个callback集合,通知观察者进行修改。

ViewDataBinding.java

    private static class WeakPropertyListener extends Observable.OnPropertyChangedCallback
            implements ObservableReference {
        final WeakListener mListener;

        public WeakPropertyListener(ViewDataBinding binder, int localFieldId) {
            mListener = new WeakListener(binder, localFieldId, this);
        }

        @Override
        public WeakListener getListener() {
            return mListener;
        }

        @Override
        public void addListener(Observable target) {
            target.addOnPropertyChangedCallback(this);
        }

        @Override
        public void removeListener(Observable target) {
            target.removeOnPropertyChangedCallback(this);
        }

        @Override
        public void setLifecycleOwner(LifecycleOwner lifecycleOwner) {
        }

        @Override
        public void onPropertyChanged(Observable sender, int propertyId) {
        	// 当被观察者数据发生变化的时候,就会回调该方法说明属性发生变化
            ViewDataBinding binder = mListener.getBinder();
            if (binder == null) {
                return;
            }
            Observable obj = mListener.getTarget();
            if (obj != sender) {
                return; // notification from the wrong object?
            }
            binder.handleFieldChange(mListener.mLocalFieldId, sender, propertyId);
        }
    }

BaseObservable.java

    @Override
    public void addOnPropertyChangedCallback(@NonNull OnPropertyChangedCallback callback) {
        synchronized (this) {
            if (mCallbacks == null) {
            	// 这个PropertyChangeRegistry就是用来做属性修改的时候回调的
            	// 因为往被观察者中添加了callback
            	// 当被观察者发生修改的时候,就会遍历整个mCallbacks进行数据回调
                mCallbacks = new PropertyChangeRegistry();
            }
        }
        mCallbacks.add(callback);
    }

PropertyChangeRegistry.java

public class PropertyChangeRegistry extends
        CallbackRegistry {

    private static final CallbackRegistry.NotifierCallback NOTIFIER_CALLBACK = new CallbackRegistry.NotifierCallback() {
        @Override
        public void onNotifyCallback(Observable.OnPropertyChangedCallback callback, Observable sender,
                int arg, Void notUsed) {
            callback.onPropertyChanged(sender, arg);
        }
    };

    public PropertyChangeRegistry() {
    	// 将静态属性NOTIFIER_CALLBACK设置给其父类的mNotifier
    	// 设置这个的目的,就是在被观察者数据发生改变的时候
    	// 能通过NOTIFIER_CALLBACK对象实现中的onNotifyCallback方法
    	// 接收回调处理,将处理过程回调给WeakPropertyListener
        super(NOTIFIER_CALLBACK);
    }

    /**
     * Notifies registered callbacks that a specific property has changed.
     *
     * @param observable The Observable that has changed.
     * @param propertyId The BR id of the property that has changed or BR._all if the entire
     *                   Observable has changed.
     */
    public void notifyChange(@NonNull Observable observable, int propertyId) {
        notifyCallbacks(observable, propertyId, null);
    }
}

从这第三步可以知道:
  BaseObservable这个被观察者,会被WeakListener持有,而WeakListener会被WeakPropertyListener持有,从而BaseObservable会间接的被WeakPropertyListener持有。
  而BaseObservable会通过mCallbacks持有Observable.OnPropertyChangedCallback对象,而Observable.OnPropertyChangedCallback的子类是WeakPropertyListener,所以BaseObservable也会持有WeakPropertyListener对象
  而BaseObservable会持有一个mCallbacks对象,这是一个PropertyChangeRegistry对象,是CallbackRegistry的子类
  而WeakPropertyListener和WeakListener是相互持有的对方的引用。
  在完成监听的相互绑定关系,并且给Observable添加了回调之后,就会回到ActivityMainBindingImpl的setUser()方法继续执行notifyPropertyChanged()方法。但是这里的例子有个问题,就是监听是添加在User这个BaseObservable的子类中的,但是更新的时候,并不是通过这个User来进行通知,而是根据ActivityMainBindingImpl这个BaseObservable来通知,那么这个时候并不会通过ActivityMainBindingImpl的调用notifyPropertyChanged()最终拿到User中的PropertyChangeRegistry对象mCallbacks,所以起作用的并不是这句话,而最终ActivityMainBindingImpl在设置User起刷新作用,是因为super.requestRebind()的调用也触发了额mRebindRunnable任务的执行,其实就是没有通过PropertyChange来触发requestRebind()

(4)ActivityMainBindingImpl.notifyPropertyChanged()
  这里其实就是调用的BaseObservable的notifyPropertyChanged()方法,因为ActivityMainBindingImpl是ViewDataBinding的子类,而ViewDataBinding继承了BaseObservable类

callbackRegister.java

    public synchronized void notifyCallbacks(T sender, int arg, A arg2) {
        mNotificationLevel++;
        notifyRecurse(sender, arg, arg2);
        mNotificationLevel--;
        if (mNotificationLevel == 0) {
            if (mRemainderRemoved != null) {
                for (int i = mRemainderRemoved.length - 1; i >= 0; i--) {
                    final long removedBits = mRemainderRemoved[i];
                    if (removedBits != 0) {
                        removeRemovedCallbacks((i + 1) * Long.SIZE, removedBits);
                        mRemainderRemoved[i] = 0;
                    }
                }
            }
            if (mFirst64Removed != 0) {
                removeRemovedCallbacks(0, mFirst64Removed);
                mFirst64Removed = 0;
            }
        }
    }

    private void notifyRecurse(T sender, int arg, A arg2) {
        final int callbackCount = mCallbacks.size();
        final int remainderIndex = mRemainderRemoved == null ? -1 : mRemainderRemoved.length - 1;

        // Now we've got all callbakcs that have no mRemainderRemoved value, so notify the
        // others.
        notifyRemainder(sender, arg, arg2, remainderIndex);

        // notifyRemainder notifies all at maxIndex, so we'd normally start at maxIndex + 1
        // However, we must also keep track of those in mFirst64Removed, so we add 2 instead:
        final int startCallbackIndex = (remainderIndex + 2) * Long.SIZE;

        // The remaining have no bit set
        notifyCallbacks(sender, arg, arg2, startCallbackIndex, callbackCount, 0);
    }

    private void notifyRemainder(T sender, int arg, A arg2, int remainderIndex) {
        if (remainderIndex < 0) {
            notifyFirst64(sender, arg, arg2);
        } else {
            final long bits = mRemainderRemoved[remainderIndex];
            final int startIndex = (remainderIndex + 1) * Long.SIZE;
            final int endIndex = Math.min(mCallbacks.size(), startIndex + Long.SIZE);
            notifyRemainder(sender, arg, arg2, remainderIndex - 1);
            notifyCallbacks(sender, arg, arg2, startIndex, endIndex, bits);
        }
    }

    private void notifyFirst64(T sender, int arg, A arg2) {
        final int maxNotified = Math.min(Long.SIZE, mCallbacks.size());
        notifyCallbacks(sender, arg, arg2, 0, maxNotified, mFirst64Removed);
    }

    private void notifyCallbacks(T sender, int arg, A arg2, final int startIndex,
            final int endIndex, final long bits) {
        long bitMask = 1;
        for (int i = startIndex; i < endIndex; i++) {
            if ((bits & bitMask) == 0) {
            	// mCallbacks是PropertyChangeRegistry对象
            	// mCallbacks持有的就是WeakPropertyListener对象
            	// mNotifier是静态对象,是由PropertyChangeRegistry中创建
            	// 并且由其构造器中传给其PropertyChangeRegistry的父类
                mNotifier.onNotifyCallback(mCallbacks.get(i), sender, arg, arg2);
            }
            bitMask <<= 1;
        }
    }

  这里的mNotifier.notifyCallback其实就会调用到下面的PropertyChangeRegistry中定义的NOTIFIER_CALLBACK属性中的onNotifyCallback实现,而这里的callback其实就是WeakPropertyListener,因为WeakPropertyListener是OnPropertyChangedCallback的子类,这里其实会回调给mLocalFieldObservers数组中所有的WeakListener

PropertyChangeRegistry.java

public class PropertyChangeRegistry extends
        CallbackRegistry {

    private static final CallbackRegistry.NotifierCallback NOTIFIER_CALLBACK = new CallbackRegistry.NotifierCallback() {
        @Override
        public void onNotifyCallback(Observable.OnPropertyChangedCallback callback, Observable sender,
                int arg, Void notUsed) {
            callback.onPropertyChanged(sender, arg);
        }
    };

    public PropertyChangeRegistry() {
        super(NOTIFIER_CALLBACK);
    }

    /**
     * Notifies registered callbacks that a specific property has changed.
     *
     * @param observable The Observable that has changed.
     * @param propertyId The BR id of the property that has changed or BR._all if the entire
     *                   Observable has changed.
     */
    public void notifyChange(@NonNull Observable observable, int propertyId) {
        notifyCallbacks(observable, propertyId, null);
    }
}

ViewDataBinding.java类内部静态类的WeakPropertyListener

    @Override
    public void onPropertyChanged(Observable sender, int propertyId) {
        ViewDataBinding binder = mListener.getBinder();
        if (binder == null) {
            return;
        }
        Observable obj = mListener.getTarget();
        if (obj != sender) {
            return; // notification from the wrong object?
        }
        // 从mListenerer中取出target,而这里的mListener其实就是WeakListener,
        // 而每个被观察者,其实都是有一个对应的LocalFieldId,这个id就是BR文件中定义的,
        // 刚才的流程中,我们传入的是0,所以这里的mLocalFieldId=0
        binder.handleFieldChange(mListener.mLocalFieldId, sender, propertyId);
    }

  从mListener中取出target,而这里的mListener其实就是WeakListener,而每个被观察者,其实都是有一个对应的LocalFieldId,这个id就是BR文件中定义的,刚才的流程中,我们传入的是0,所以这里的mLocalFieldId = 0

ViewDataBinding.java

    private void handleFieldChange(int mLocalFieldId, Object object, int fieldId) {
        if (mInLiveDataRegisterObserver) {
            // We're in LiveData registration, which always results in a field change
            // that we can ignore. The value will be read immediately after anyway, so
            // there is no need to be dirty.
            return;
        }
        boolean result = onFieldChange(mLocalFieldId, object, fieldId);
        if (result) {
            requestRebind();
        }
    }

这里的onFieldChange的方法的实现,就是在ActivityMainBindingImpl.java中

ActivityMainBindingImpl.java

    @Override
    protected boolean onFieldChange(int localFieldId, Object object, int fieldId) {
        switch (localFieldId) {
            case 0 :
                return onChangeUser((com.example.mydatabinding_java.model.User) object, fieldId);
        }
        return false;
    }
    private boolean onChangeUser(com.example.mydatabinding_java.model.User User, int fieldId) {
    	// mDirtyFlags的初始值0xffffffffffffffffL
        if (fieldId == BR._all) {
            synchronized(this) {
                    mDirtyFlags |= 0x1L;
            }
            return true;
        }
        else if (fieldId == BR.name) {
            synchronized(this) {
                    mDirtyFlags |= 0x2L;
            }
            return true;
        }
        else if (fieldId == BR.pwd) {
            synchronized(this) {
                    mDirtyFlags |= 0x4L;
            }
            return true;
        }
        return false;
    }

  这里因为fieldId = 0,所以会进入第一个if条件(fieldId == BR._all),所以会返回true,所以就会返回到ViewDataBinding.java中的handleFieldChange方法中,继续执行requestRebind()

ViewDataBinding.java

    protected void requestRebind() {
        if (mContainingBinding != null) {
            mContainingBinding.requestRebind();
        } else {
            final LifecycleOwner owner = this.mLifecycleOwner;
            if (owner != null) {
                Lifecycle.State state = owner.getLifecycle().getCurrentState();
                if (!state.isAtLeast(Lifecycle.State.STARTED)) {
                    return; // wait until lifecycle owner is started
                }
            }
            synchronized (this) {
                if (mPendingRebind) {
                    return;
                }
                mPendingRebind = true;
            }
            if (USE_CHOREOGRAPHER) {
                mChoreographer.postFrameCallback(mFrameCallback);
            } else {
                mUIThreadHandler.post(mRebindRunnable);
            }
        }
    }

  这里最终都会执行到mRebindRunnable的run()方法。只不过SDK版本大于等于16的时候,会采用Choreographer编舞者来处理,而之前的版本则是采用Handler来执行

ViewDataBinding.java

    private final Runnable mRebindRunnable = new Runnable() {
        @Override
        public void run() {
            synchronized (this) {
                mPendingRebind = false;
            }
            processReferenceQueue();

            if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
                // Nested so that we don't get a lint warning in IntelliJ
                if (!mRoot.isAttachedToWindow()) {
                    // Don't execute the pending bindings until the View
                    // is attached again.
                    mRoot.removeOnAttachStateChangeListener(ROOT_REATTACHED_LISTENER);
                    mRoot.addOnAttachStateChangeListener(ROOT_REATTACHED_LISTENER);
                    return;
                }
            }
            executePendingBindings();
        }
    };


    public void executePendingBindings() {
        if (mContainingBinding == null) {
            executeBindingsInternal();
        } else {
            mContainingBinding.executePendingBindings();
        }
    }

    private void executeBindingsInternal() {
        if (mIsExecutingPendingBindings) {
            requestRebind();
            return;
        }
        if (!hasPendingBindings()) {
            return;
        }
        mIsExecutingPendingBindings = true;
        mRebindHalted = false;
        if (mRebindCallbacks != null) {
            mRebindCallbacks.notifyCallbacks(this, REBIND, null);

            // The onRebindListeners will change mPendingHalted
            if (mRebindHalted) {
                mRebindCallbacks.notifyCallbacks(this, HALTED, null);
            }
        }
        if (!mRebindHalted) {
            executeBindings();
            if (mRebindCallbacks != null) {
                mRebindCallbacks.notifyCallbacks(this, REBOUND, null);
            }
        }
        mIsExecutingPendingBindings = false;
    }

  在这里最终就会执行到executeBindings()方法,而该方法的实现,又是在ActivityMainBindingImpl.java中

ActivityMainBindingImpl.java

    @Override
    protected void executeBindings() {
        long dirtyFlags = 0;
        synchronized(this) {
        	// mDirtyFlags在之前的onChangeUser方法中
        	// 因为fieldId = 0,所以会执行mDirtyFlags |=0x1L
        	// 而初始值为private  long mDirtyFlags = 0xffffffffffffffffL;
            dirtyFlags = mDirtyFlags;
            mDirtyFlags = 0;
        }
        java.lang.String userName = null;
        com.example.mydatabinding_java.model.User user = mUser;
        java.lang.String userPwd = null;

		// 因为mDirtyFlags或了一个0x1L,此时再执行一个与操作
		// 不为0
        if ((dirtyFlags & 0xfL) != 0) {

			// 0xbL为1011,与上0001,则不会为0
            if ((dirtyFlags & 0xbL) != 0) {

                    if (user != null) {
                        // read user.name
                        userName = user.getName();
                    }
            }
            // 0xdL=1101,与上0001,也不会为0
            if ((dirtyFlags & 0xdL) != 0) {

                    if (user != null) {
                        // read user.pwd
                        userPwd = user.getPwd();
                    }
            }
        }
        // batch finished
        // 这里最终再执行setText的操作,其实就是调用了页面布局的View的setText
        if ((dirtyFlags & 0xbL) != 0) {
            // api target 1
			// 最后还是执行setText()方法
            androidx.databinding.adapters.TextViewBindingAdapter.setText(this.et1, userName);
        }
        if ((dirtyFlags & 0x8L) != 0) {
            // api target 1

            androidx.databinding.adapters.TextViewBindingAdapter.setTextWatcher(this.et1, (androidx.databinding.adapters.TextViewBindingAdapter.BeforeTextChanged)null, (androidx.databinding.adapters.TextViewBindingAdapter.OnTextChanged)null, (androidx.databinding.adapters.TextViewBindingAdapter.AfterTextChanged)null, et1androidTextAttrChanged);
            androidx.databinding.adapters.TextViewBindingAdapter.setTextWatcher(this.et2, (androidx.databinding.adapters.TextViewBindingAdapter.BeforeTextChanged)null, (androidx.databinding.adapters.TextViewBindingAdapter.OnTextChanged)null, (androidx.databinding.adapters.TextViewBindingAdapter.AfterTextChanged)null, et2androidTextAttrChanged);
        }
        if ((dirtyFlags & 0xdL) != 0) {
            // api target 1
			// 最后还是执行setText()方法
            androidx.databinding.adapters.TextViewBindingAdapter.setText(this.et2, userPwd);
        }
    }

你可能感兴趣的:(Kotlin,DataBinding)