当前分析的ReactNative版本为0.61.5:
看这边文章前最好先了解UIManagerModule、UIImplementation、UIViewOperationQueue类;
1.ReactNative源码分析之UIManagerModule.
2.ReactNative源码分析之UIViewOperationQueue.
3.ReactNative源码分析之UIImplementation.
我们看NativeViewHierarchyManager的名字就知道,它其实是一个管理类,管理所有的View。
先来看下它的私有字段:
private final SparseArray<View> mTagsToViews;
private final SparseArray<ViewManager> mTagsToViewManagers;
private final ViewManagerRegistry mViewManagers;
从这几个字段我们就能猜测,它管理着所有的ViewManager和对应的View,映射关系是通过int值关联,这个int值其实就是ViewTag。
我们找几个方法看看具体实现:
public final synchronized View resolveView(int tag) {
View view = mTagsToViews.get(tag);
if (view == null) {
throw new IllegalViewOperationException(
"Trying to resolve view with tag " + tag + " which doesn't exist");
}
return view;
}
这个是根据ViewTag,查找对应的View。
public final synchronized ViewManager resolveViewManager(int tag) {
ViewManager viewManager = mTagsToViewManagers.get(tag);
if (viewManager == null) {
boolean alreadyDropped = Arrays.asList(mDroppedViewArray).contains(tag);
throw new IllegalViewOperationException(
"ViewManager for tag "
+ tag
+ " could not be found.\n View already dropped? "
+ alreadyDropped
+ ".\nLast index "
+ mDroppedViewIndex
+ " in last 100 views"
+ mDroppedViewArray.toString());
}
return viewManager;
}
这个是通过ViewTag,查找对应的ViewManager。
在之前的文章提到UIViewOperationQueue类,所有对View的UI操作本质上是由NativeViewHierarchyManager代理执行,想操作哪个View&