Jetpack使用(三)DataBinding核心原理

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;
    }
}
  • 在布局文件里导入要用的实体类
  

    
        
    

    

        
        

    

  • 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方式翻译了一下



    
        
    
    
        
            
            
        
        
            
                
                    
                    false
                    
                
            
            
        
        
            
                
                    
                    false
                    
                
            
            
        
    

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



                                                       
                                                   
    
                 
                       
                                                                
           

    

        
        

    
         

还有会通过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方法,获取前面存放控件数组里当前这个控件所对应的元素,去更新数据。它这里更新数据是先通过位或操作,初始化更新的标志位,再通过位与具体去判断这个控件是否需要更新

阅读全文
下载说明:
1、本站所有资源均从互联网上收集整理而来,仅供学习交流之用,因此不包含技术服务请大家谅解!
2、本站不提供任何实质性的付费和支付资源,所有需要积分下载的资源均为网站运营赞助费用或者线下劳务费用!
3、本站所有资源仅用于学习及研究使用,您必须在下载后的24小时内删除所下载资源,切勿用于商业用途,否则由此引发的法律纠纷及连带责任本站和发布者概不承担!
4、本站站内提供的所有可下载资源,本站保证未做任何负面改动(不包含修复bug和完善功能等正面优化或二次开发),但本站不保证资源的准确性、安全性和完整性,用户下载后自行斟酌,我们以交流学习为目的,并不是所有的源码都100%无错或无bug!如有链接无法下载、失效或广告,请联系客服处理!
5、本站资源除标明原创外均来自网络整理,版权归原作者或本站特约原创作者所有,如侵犯到您的合法权益,请立即告知本站,本站将及时予与删除并致以最深的歉意!
6、如果您也有好的资源或教程,您可以投稿发布,成功分享后有站币奖励和额外收入!
7、如果您喜欢该资源,请支持官方正版资源,以得到更好的正版服务!
8、请您认真阅读上述内容,注册本站用户或下载本站资源即您同意上述内容!
原文链接:https://www.dandroid.cn/19466,转载请注明出处。
0

评论0

显示验证码
没有账号?注册  忘记密码?