View.AttachInfo的前世今生

简介: View.AttachInfo的前世今生

View所持有的AttachInfo来自于根布局的ViewRootImpl。

同一个window下的view,持有的AttachInfo都是同一份。

1、View#AttachInfo:当view 被跟父window关联起来时,被设置给view的一系列信息。


A set of information given to a view when it is attached to its parent window.

@UnsupportedAppUsage

final IWindowSession mSession;

@UnsupportedAppUsage

final IWindow mWindow;

final IBinder mWindowToken;

Display mDisplay;

final Callbacks mRootCallbacks;

IWindowId mIWindowId;

WindowId mWindowId;

/**

* The top view of the hierarchy.

*/

View mRootView;

IBinder mPanelParentWindowToken;

boolean mHardwareAccelerated;

boolean mHardwareAccelerationRequested;

ThreadedRenderer mThreadedRenderer;

List<RenderNode> mPendingAnimatingRenderNodes;

...

2、何时、哪里生成的:ViewRootImpl


ViewRootImpl的构造函数中:

ViewRootImpl(Context context, Display display, IWindowSession session, boolean useSfChoreographer){

mAttachInfo = new View.AttachInfo(mWindowSession, mWindow, display, this, mHandler, this, context);

}

3、何时设置给View?

①activity启动时,渲染布局:


--> ViewRootImpl#doTraversal()

--> ViewRootImpl#performTraversals()

--> Decorview#dispatchAttachedToWindow(AttachInfo info, int visibility)

--> ViewGroup#dispatchAttachedToWindow(AttachInfo info, int visibility)

--> View#dispatchAttachedToWindow(AttachInfo info, int visibility): the android.view.View.AttachInfo to associated with this view

②动态添加View:比如点击button时,给root添加子View。

示例代码:


LinearLayout root = findViewById(R.id.root);

Button btn = findViewById(R.id.btn_add);

btn.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

View view = new View(DispatchAttachInfoTestActivity.this);

view.setBackgroundColor(getResources().getColor(R.color.background_info));

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(300, 300);

root.addView(view, lp);

}

});

调用栈:


--> View.OnClickListener#onClick(View v)

--> ViewGroup#addView(View child, LayoutParams params)

--> ViewGroup#addView(View child, int index, LayoutParams params)

--> ViewGroup#addViewInner(View child, int index, LayoutParams params, boolean preventRequestLayout)

--> View#dispatchAttachedToWindow(AttachInfo info, int visibility)

总结

①View#AttachInfo由ViewRootImpl生成,mAttachInfo实例维护在ViewRootImpl中。

②最先设置给根root。

③当view树中添加新的View时,会由parent将其mAttachInfo设置给child。

最终保证了整个View数中的View#AttachInfo是同一个对象。

相关文章
|
6月前
|
Android开发 开发者
Error:Could not find com.android.support:appcompat-v7:27.0.2.
Error:Could not find com.android.support:appcompat-v7:27.0.2.
132 0
|
6月前
|
XML 前端开发 Java
Model-View-Controller
“【5月更文挑战第28天】”
41 4
|
Java
viewpage 添加fragment 报错 viewpage demo LayoutInflater 自定义控件轮播图demo
viewpage 添加fragment 报错 viewpage demo LayoutInflater 自定义控件轮播图demo
113 0
viewpage 添加fragment 报错 viewpage demo LayoutInflater 自定义控件轮播图demo
|
缓存
ViewPager2 详细使用
ViewPager2 详细使用
1058 0
Could not find com.android.support:appcompat-v7:25.3.1.
Could not find com.android.support:appcompat-v7:25.3.1.
82 0
view.setTag()的正确使用姿势
开发中,我们经常要进行数据的传递,会使用到view.setTag()和view.getTag()方法,主要用在view的点击事件中,可以让数据跟着view走,这种方法很方便。一般情况下给view设置一个tag就够用了,某些情况下我们需要给一个view设置多个tag,在需要的时候再分别取出来,这就需要用到view.setTag()的一个重载方法view.setTag(int key,final Object Tag)了。
|
Java
Could not find class 'android.support.v4.view.ViewPager', referenced from method***
Could not find class 'android.support.v4.view.ViewPager', referenced from method***
156 0
Could not find class 'android.support.v4.view.ViewPager', referenced from method***
|
Windows
List View控件总结
List View控件总结
138 0
|
前端开发 Java Spring
|
前端开发 Java Spring