網站首頁 編程語言 正文
上一章說明了DataBinding生存的類之間關系,現在這里來看看布局是如何加載的以及view是如何映射的。
一、布局加載
這里把之前的代碼重新貼下方便說明,代碼如下:
class MainActivity : AppCompatActivity() {
private val viewModel: SimpleViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding: ActivityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)
binding.lifecycleOwner = this
binding.viewModel = viewModel
}
}
其中布局加載就這一行:DataBindingUtil.setContentView(this, R.layout.activity_main),所以進入到DataBindingUtil中,代碼如下:
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(layoutId);
View decorView = activity.getWindow().getDecorView();
ViewGroup contentView = (ViewGroup) decorView.findViewById(android.R.id.content);
return bindToAddedViews(bindingComponent, contentView, 0, layoutId);
}
activity.setContentView(layoutId),這和我們不用DataBinding寫的一樣啊,所以Databinding在這里就幫我們加載了布局。
接下來,看DataBinding是如何實現view映射的。
二、view映射
然后拿到decorView 并找到contentView ,最后調用bindToAddedViews,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;
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);
}
}
在我們的場景里面,endChildren 應該為1,childrenAdded 也為1,所以走了第一個分支,繼續調用bind函數,如下:
private static DataBinderMapper sMapper = new DataBinderMapperImpl();
static <T extends ViewDataBinding> T bind(DataBindingComponent bindingComponent, View root,
int layoutId) {
return (T) sMapper.getDataBinder(bindingComponent, root, layoutId);
}
調用了sMapper的getDataBinder函數,這里的sMapper類型為DataBinderMapperImpl,還記得上一章說過有兩個DataBinderMapperImpl嗎?為了便于說明,這里再把之前的類圖貼下:
額,這就尷尬了,所以這里的Mapper到底是哪個呢?之前說過左邊的是android提供的,右邊的是我們自己包下面的;其實這里的sMapper屬于左邊這個行列,也就是androidx這個包下面的。那他們有什么區別呢?你可以認為左邊的提供了一個簡單的代理功能,其實它就是簡單對右邊的Mapper類進行包裝而已。
這里需要說明下sMapper對象的初始化過程,我們知道類加載會觸發類變量(靜態變量)的初始化,這個時候sMapper就會被初始化,這個時候會調用DataBinderMapperImpl(左邊那個mapper)的構建函數,代碼如下:
package androidx.databinding;//位于androidx包下面
public class DataBinderMapperImpl extends MergedDataBinderMapper {
DataBinderMapperImpl() {
//這個DataBinderMapperImpl就是我們自己包下面的了
addMapper(new com.zfang.databindingstudy.DataBinderMapperImpl());
}
}
正如前面所說,androidx下面的mapper類包裝了項目中的mapper類,addMapper代碼如下:
public void addMapper(DataBinderMapper mapper) {
Class<? extends DataBinderMapper> mapperClass = mapper.getClass();
if (mExistingMappers.add(mapperClass)) {
mMappers.add(mapper);
final List<DataBinderMapper> dependencies = mapper.collectDependencies();
for(DataBinderMapper dependency : dependencies) {
addMapper(dependency);
}
}
}
這里會把項目中的mapper(即DataBinderMapperImpl)加入到mMappers這個CopyOnWriteArrayList中,后面會用到。
此時可以繼續看看getDataBinder的實現了(其實現位于MergedDataBinderMapper中),代碼如下:
@Override
public ViewDataBinding getDataBinder(DataBindingComponent bindingComponent, View view,
int layoutId) {
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;
}
這里就是從mMappers中把mapper拿出來,再根據傳遞進來的參數view、layoutId找到相應的ViewDataBinding對象;這里的mMappers就是剛剛提到的那個CopyOnWriteArrayList,所以會調用到我們的DataBinderMapperImpl,其中的getDataBinder實現如下:
private static final int LAYOUT_ACTIVITYMAIN = 1;
private static final SparseIntArray INTERNAL_LAYOUT_ID_LOOKUP = new SparseIntArray(1);
static {
INTERNAL_LAYOUT_ID_LOOKUP.put(com.zfang.databindingstudy.R.layout.activity_main, LAYOUT_ACTIVITYMAIN);
}
@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;
}
這里有個SparseIntArray ,它定義了我們的布局與一個整數的映射關系,上面的代碼首先拿到view的tag,這里返回的tag為layout/activity_main_0(回憶下:上一章說過DataBinding會生存兩個xml,其中一個加了tag,那里說的tag正是和這里對應上了,其作用就體現在這里),所以會返回ActivityMainBindingImpl,這正是需要的ViewDataBinding類。
繼續進入ActivityMainBindingImpl的構建函數中,代碼如下:
public ActivityMainBindingImpl(@Nullable androidx.databinding.DataBindingComponent bindingComponent, @NonNull View root) {
this(bindingComponent, root, mapBindings(bindingComponent, root, 3, sIncludes, sViewsWithIds));
}
private ActivityMainBindingImpl(androidx.databinding.DataBindingComponent bindingComponent, View root, Object[] bindings) {
super(bindingComponent, root, 2
, (android.widget.TextView) bindings[1]
, (android.widget.TextView) bindings[2]
);
this.first.setTag(null);
this.mboundView0 = (androidx.constraintlayout.widget.ConstraintLayout) bindings[0];
this.mboundView0.setTag(null);
this.second.setTag(null);
setRootTag(root);
// listeners
invalidateAll();
}
先調用了第一個構造函數,然后進入第二個。第二個構造函數又調用了父類的相應構造函數,代碼如下:
protected ActivityMainBinding(Object _bindingComponent, View _root, int _localFieldCount,
TextView first, TextView second) {
super(_bindingComponent, _root, _localFieldCount);
this.first = first;
this.second = second;
}
沒錯,上面的bindings數組中的bindings[1]、bindings[2]正是對應到了我們這個場景中的first和second兩個view。現在的問題是bindings數組中的值是怎么來的呢?
我們繼續看看ActivityMainBindingImpl類中第一個構建數據中調用的函數mapBindings,看來在mapBindings中會填充bindings數組,mapBindings代碼如下:
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;
}
這里 根據numBindings新建了一個數組,繼續:
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;
//第一次進來isRoot為true,tag為根據布局所以是以layout開頭,因此這進入第一個if
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;//放入bindings數組,這里的view代表根布局
}
//處理包含布局中有include標簽的情況
indexInIncludes = includes == null ? -1 : index;
isBound = true;
} else {
indexInIncludes = -1;
}
} else if (tag != null && tag.startsWith(BINDING_TAG_PREFIX)) {
//如何不是根布局,對應到我們的場景則會走到這里,我們的兩個TextView的
//tag剛是以binding開頭的,其實只要寫了綁定表達式就會到這里。
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;
}
}
}
//如果是ViewGroup則遞歸處理找到相應的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;
//處理include標簽
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;
}
}
}
}
//非include
if (!isInclude) {
mapBindings(bindingComponent, child, bindings, includes, viewsWithIds, false);
}
}
}
}
這里就是實現view數組映射的關鍵,主要功能就是填充了bindings數組,思路就是找到包含綁定表達式的控件,然后把它們記錄下來放到一個數組中,方便在相應控件的數據變化的時候能夠通知到控件, 這里其實就是找到如下布局中的兩個TextView然后加入到bindings中。
<?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_main_0" tools:context=".MainActivity"> <TextView android:id="@+id/first" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginVertical="12dp" android:tag="binding_1" android:textColor="#333333" android:textSize="18sp" app:layout_constraintBottom_toTopOf="@id/second" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_chainStyle="packed" /> <TextView android:id="@+id/second" android:layout_width="wrap_content" android:layout_height="wrap_content" android:tag="binding_2" android:textColor="#999" android:textSize="14sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/first" /> </androidx.constraintlayout.widget.ConstraintLayout>
上面那段代碼的邏輯就是找到ConstraintLayout(android:tag="layout/activity_main_0") 以及兩個TextView(tag分別為binding_1和binding_2),總共三個控件。ConstraintLayout就是根布局,兩個TextView就是我們需要操作的View。
好了,布局view映射完成,簡單總結下:首先就是DataBinding會幫我們調用setContentView,所以我們不用調用這個方法;其次DataBinding會幫我們找到包含有數據綁定表達式的View其后幫我們存起來,方便在數據變化的時候操作我們的View。
下一章繼續分析數據是如何與控件進行綁定的。
如果你對DataBinding生存的類關系有疑問,可以返回上一章DataBinding原理----類關系進行參考。
原文鏈接:https://blog.csdn.net/www586089/article/details/127779260
相關推薦
- 2022-09-24 C#中參數的傳遞方式詳解_C#教程
- 2022-01-15 面試官:[‘1‘, ‘2‘, ‘3‘].map(parseInt)的結果是什么?為甚?我:[1, 2
- 2022-06-26 C#實現數組元素的數據類型轉換方法詳解_C#教程
- 2022-03-28 深入理解numpy中argmax的具體使用_python
- 2023-01-26 C#實現批量Word轉換Html的示例代碼_C#教程
- 2022-07-21 Eslint代碼保存自動格式化
- 2022-07-20 python密碼學列置換密碼學習_python
- 2022-12-25 使用Python可設置抽獎者權重的抽獎腳本代碼_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支