美文网首页Android开发经验谈
Jetpack使用(三)DataBinding核心原理

Jetpack使用(三)DataBinding核心原理

作者: 程序员三千_ | 来源:发表于2020-03-31 12:37 被阅读0次

    Jetpack使用(一)Lifecycles核心原理
    Jetpack使用(二)LiveData核心原理
    Jetpack使用(三)DataBinding核心原理
    Jetpack使用(四)ViewModel核心原理
    Jetpack使用(五)Navigation核心原理

    DataBinding

    是 Google 在 Jetpack 中推出的一款数据绑定的支持库,利用该库可以实现在页面组件和数据的双向绑定,类似与MVVM。

    具体使用

    • 在build.gradle里配置
     dataBinding{
                enabled true
     }
    
    • 新建一个实体类继承BaseObservable,并且在对应的get方法上加上注解 @Bindable,在set方法里加 notifyPropertyChanged(BR.name);
    public class User extends BaseObservable {
        @Bindable
        private String name;
        @Bindable
        private String password;
    
        public User(String name, String password) {
            this.name = name;
            this.password = password;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    }
    
    • 在布局文件里导入要用的实体类
      <?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="user"
                type="com.example.databinding.User" />
        </data>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            tools:context=".MainActivity">
    
            <TextView
                android:id="@+id/tv1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@{user.name}"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
            <TextView
                android:id="@+id/tv2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@{user.password}"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
    
        </LinearLayout>
    </layout>
    
    • MainActivity里具体去使用
    public class MainActivity extends AppCompatActivity {
    
        ActivityMainBinding binding;
    
        User user;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
            user = new User("小三爷", "123");
    
            user.setName(user.getName() + "1");
            binding.setVariable(BR.user, user);
    
        }
    }
    

    在你想要更新的地方,调用binding.setVariable(BR.user, user)就ok。

    核心原理分析

    当我们的代码在编译的时候,系统会给我们的xml文件分离成两个文件
    1、app/build/imtermediates/data_binding_layout_info_type_merge/
    debug/activity_main-layout.xml,相当于把我们的xml文件用另一种xml方式翻译了一下

    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
    <Layout directory="layout" filePath="/Users/wudengyao/android-projects/DataBinding_Demo_20200329/app/src/main/res/layout/activity_main.xml"
        isMerge="false"
        layout="activity_main" modulePackage="com.example.databinding_demo_20200329">
        <Variables name="user" declared="true" type="com.example.databinding_demo_20200329.User">
            <location endLine="7" endOffset="63" startLine="5" startOffset="8" />
        </Variables>
        <Targets>
            <Target tag="layout/activity_main_0" view="LinearLayout">
                <Expressions />
                <location endLine="35" endOffset="18" startLine="10" startOffset="4" />
            </Target>
            <Target id="@+id/tv1" tag="binding_1" view="TextView">
                <Expressions>
                    <Expression attribute="android:text" text="user.name">
                        <Location endLine="20" endOffset="38" startLine="20" startOffset="12" />
                        <TwoWay>false</TwoWay>
                        <ValueLocation endLine="20" endOffset="36" startLine="20" startOffset="28" />
                    </Expression>
                </Expressions>
                <location endLine="24" endOffset="55" startLine="16" startOffset="8" />
            </Target>
            <Target id="@+id/tv2" tag="binding_2" view="TextView">
                <Expressions>
                    <Expression attribute="android:text" text="user.password">
                        <Location endLine="29" endOffset="42" startLine="29" startOffset="12" />
                        <TwoWay>false</TwoWay>
                        <ValueLocation endLine="29" endOffset="40" startLine="29" startOffset="28" />
                    </Expression>
                </Expressions>
                <location endLine="33" endOffset="55" startLine="25" startOffset="8" />
            </Target>
        </Targets>
    </Layout>
    

    2、app/build/imtermediates/incremental/mergeDebugResources/stripped.dir/
    layout/activity_main.xml,把我们的xml去掉了<data></data>包含的代码块

    <?xml version="1.0" encoding="utf-8"?>
    
                                                           
                                                       
        
                     
                           
                                                                    
               
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            tools:context=".MainActivity" android:tag="layout/activity_main_0" 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">
    
            <TextView
                android:id="@+id/tv1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:tag="binding_1"    
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
            <TextView
                android:id="@+id/tv2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:tag="binding_2"        
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
    
        </LinearLayout>
             
    

    还有会通过apt生成三个类文件ActivityMainBindingImpl、BR、DataBinderMapperImpl
    BR就相当于R文件,里面存放xml布局里控件的id

    public class BR {
      public static final int _all = 0;
    
      public static final int password = 1;
    
      public static final int name = 2;
    
      public static final int user = 3;
    }
    
    image.png

    知道了这些我们现在真正进入源码分析阶段

    流程一、binding.setVariable(设置数据)

    我们在在MainActicty里调用setVariable来设置数据的时候,会进入到ViewDataBinding类

    public abstract boolean setVariable(int variableId, @Nullable Object value);
    

    这是一个抽象类,具体实现会在ActivityMainBingdingImpl里,

        @Override
        public boolean setVariable(int variableId, @Nullable Object variable)  {
            boolean variableSet = true;
            if (BR.user == variableId) {
                setUser((com.example.databinding_demo.User) variable);
            }
            else {
                variableSet = false;
            }
                return variableSet;
        }
    

    setVariable里会调用到ActivityMainBingdingImpl里的另一个方法setUser,我们再点进去

     public void setUser(@Nullable com.example.databinding_demo.User User) {
            updateRegistration(0, User);
            this.mUser = User;
            synchronized(this) {
                mDirtyFlags |= 0x1L;
            }
            notifyPropertyChanged(BR.user);
            super.requestRebind();
        }
    

    setUser里先调用注册updateRegistration方法,然后把User设置给成员变量mUser
    初始化了mDirtyFlags标志,最后是调用notifyPropertyChanged通知更新。
    我们先进入注册updateRegistration方法

     protected boolean updateRegistration(int localFieldId, Observable observable) {
            return updateRegistration(localFieldId, observable, CREATE_PROPERTY_LISTENER);
        }
    

    CREATE_PROPERTY_LISTENER实际是返回了WeakPropertyListener对象

        private boolean updateRegistration(int localFieldId, Object observable,
                CreateWeakListener listenerCreator) {
            if (observable == null) {
                return unregisterFrom(localFieldId);
            }
            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方法

       protected void registerTo(int localFieldId, Object observable,
                CreateWeakListener listenerCreator) {
            if (observable == null) {
                return;
            }
            WeakListener listener = mLocalFieldObservers[localFieldId];
            if (listener == null) {
                listener = listenerCreator.create(this, localFieldId);
                mLocalFieldObservers[localFieldId] = listener;
                if (mLifecycleOwner != null) {
                    listener.setLifecycleOwner(mLifecycleOwner);
                }
            }
            listener.setTarget(observable);
        }
    

    这个方法实会判断mLocalFieldObservers里是否存在WeakListener listener,如果不存在就创建一个存进去,mLocalFieldObservers里面保存的就是我们控件在BR文件里的id,每个id就对应一个监听器,然后通过listener.setLifecycleOwner(mLifecycleOwner);把lifecycle关联起来,再通过listener.setTarget(observable);把User对象关联起来,所以这个注册方法简单的说就是:把我们的实体对象User(观察者)、DataBinding、Lifecycle(被观察者这里就是activity)建立绑定关系,还有把控件的BR文件里的id放到一个数组里。

    流程二、setContentView(读取XML信息)

    我们在MainActivty里调用DataBindingUtil.setContentView(this,R.layout.activity_main)实际会调用到
    DataBinderMapperImpl的getDataBinder方法,

      @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;
      }
    

    ActivityMainBindingImpl再点进去,最后会调到ViewDataBinding的mapBindings的方法

    private static void mapBindings(DataBindingComponent bindingComponent, View view,
                Object[] bindings, IncludedLayouts includes, SparseIntArray viewsWithIds,
                boolean isRoot) {
            final int indexInIncludes;
            final ViewDataBinding existingBinding = getBinding(view);
            if (existingBinding != null) {
                return;
            }
            Object objTag = view.getTag();
            final String tag = (objTag instanceof String) ? (String) objTag : null;
            boolean isBound = false;
            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);
                    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);
                    }
                }
            }
        }
    
    

    mapBindings里就是解析xml文件信息,并存入bingdings数组,

    流程三、notifyPropertyChanged(更新数据)

    我们知道在流程一setVariable方法之后会调用notifyPropertyChanged去更新数据,我们点进去最终会跑到ViewDataBingding的 onPropertyChanged里

    
    @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);
            }
    
    

    一层层点进去发现是一个抽象方法

        protected abstract boolean onFieldChange(int localFieldId, Object object, int fieldId);
    
    

    最终它的实现类在ActivityMainBingdingImpl的onFieldChange里

    
     @Override
        protected boolean onFieldChange(int localFieldId, Object object, int fieldId) {
            switch (localFieldId) {
                case 0 :
                    return onChangeUser((com.example.databinding_demo_20200329.User) object, fieldId);
            }
            return false;
        }
    
      private boolean onChangeUser(com.example.databinding_demo_20200329.User User, int fieldId) {
            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.password) {
                synchronized(this) {
                        mDirtyFlags |= 0x4L;
                }
                return true;
            }
            return false;
        }
    
    

    这个方法去通过位或操作设置mDirtyFlags的初始值的,我们再看到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) {
                    }
                };
            }
        }
    

    这里有个监听器,在监听executeBindings,这个方法最终的实现类在ActivityMainBindingImpl里

     @Override
        protected void executeBindings() {
            long dirtyFlags = 0;
            synchronized(this) {
                dirtyFlags = mDirtyFlags;
                mDirtyFlags = 0;
            }
            java.lang.String userName = null;
            com.example.databinding_demo_20200329.User user = mUser;
            java.lang.String userPassword = null;
    
            if ((dirtyFlags & 0xfL) != 0) {
    
    
                if ((dirtyFlags & 0xbL) != 0) {
    
                        if (user != null) {
                            // read user.name
                            userName = user.getName();
                        }
                }
                if ((dirtyFlags & 0xdL) != 0) {
    
                        if (user != null) {
                            // read user.password
                            userPassword = user.getPassword();
                        }
                }
            }
            // batch finished
            if ((dirtyFlags & 0xbL) != 0) {
                // api target 1
    
                androidx.databinding.adapters.TextViewBindingAdapter.setText(this.tv1, userName);
            }
            if ((dirtyFlags & 0xdL) != 0) {
                // api target 1
    
                androidx.databinding.adapters.TextViewBindingAdapter.setText(this.tv2, userPassword);
            }
        }
    

    我们前面通过onFieldChange设置的标志就是在这里通过位与操作来进行控件的更新的。到此,DataBingding核心原理就一目了然了

    总结:1、我们一开始使用DataBinding的时候,在布局文件里将实体对象类通过data标签写在layout布局文件里了。然后build.gradle里开启DataBinding的使用权限,所以在编译的系统会默认给我们生成两个layout文件,一个是把data标签拿掉的layout,layout里存的是通过另一种xml方式翻译了下我们的layout,里面就是一些什么target标签把我们的textview和LinearLayout这些控件重新包装了下,然后还有一个BR文件来存储我们控件的id(和R文件类似),

    2、所以我们在通过DataBindingUtil初始化DataBinding的时候,就会把我们的前面生成的xml文件读取到一个数组里,

    3、DataBinding在set数据的时候,就是通过registerTo注册方法把我们的实体对象(观察者)、DataBinding、Lifecycle(被观察者这里就是activity)建立绑定关系,还有把控件的BR文件里的id放到一个数组里,

    4、再调用更新数据的notifyPropertyChanged方法,获取前面存放控件数组里当前这个控件所对应的元素,去更新数据。它这里更新数据是先通过位或操作,初始化更新的标志位,再通过位与具体去判断这个控件是否需要更新

    相关文章

      网友评论

        本文标题:Jetpack使用(三)DataBinding核心原理

        本文链接:https://www.haomeiwen.com/subject/xebbuhtx.html