安卓addView源码分析
来源:互联网 发布:如何更改淘宝店铺行业 编辑:程序博客网 时间:2024/06/07 15:20
Android把视图显示到屏幕上,从代码角度最终都是调用ViewGroup.addView方法,可以分为两类,一是初始化时添加视图,二是动态添加视图。
1. 从源码角度分析添加视图的流程,以下列出其中4个addView方法
public void addView(View child) { addView(child, -1); } public void addView(View child, int index) { LayoutParams params = child.getLayoutParams(); if (params == null) { params = generateDefaultLayoutParams(); if (params == null) { throw new IllegalArgumentException("generateDefaultLayoutParams() cannot return null"); } } addView(child, index, params); } public void addView(View child, int width, int height) { final LayoutParams params = generateDefaultLayoutParams(); params.width = width; params.height = height; addView(child, -1, params); } public void addView(View child, LayoutParams params) { addView(child, -1, params); }
2. 以上4个addView方法其最终都会调用以下方法,实现具体操作
/** * Adds a child view with the specified layout parameters. * * @param child the child view to add * @param index the position at which to add the child * @param params the layout parameters to set on the child */ public void addView(View child, int index, LayoutParams params) { if (DBG ) { System. out.println(this + " addView"); } // addViewInner() will call child.requestLayout() when setting the new LayoutParams // therefore, we call requestLayout() on ourselves before, so that the child's request // will be blocked at our level // 为什么调用invalidate还调用requestLayout? requestLayout(); invalidate(); // 为什么先requestLayout, invalidate之后才向ViewGroup中添加View? addViewInner(child, index, params, false); }
3. 调用ViewGroup.addViewInner
private void addViewInner(View child, int index, LayoutParams params, boolean preventRequestLayout) { // 此View是否已经添加 if (child.getParent() != null) { throw new IllegalStateException("The specified child already has a parent. " + "You must call removeView() on the child's parent first."); } if (!checkLayoutParams(params)) { params = generateLayoutParams(params); } if (preventRequestLayout) { child.mLayoutParams = params; } else { // 前面注释提到的,请求requestLayout child.setLayoutParams(params); } // 如果传入-1,直接添加到最后 if (index < 0) { index = mChildrenCount; } // 添加到ViewGroup addInArray(child, index); // tell our children if (preventRequestLayout) { child.assignParent( this); } else { // 指定添加view的父视图 child.mParent = this; } if (child.hasFocus()) { requestChildFocus(child, child.findFocus()); } AttachInfo ai = mAttachInfo; if (ai != null) { boolean lastKeepOn = ai.mKeepScreenOn; ai.mKeepScreenOn = false; // AttachInfo 与 View建立联系 child.dispatchAttachedToWindow(mAttachInfo, (mViewFlags&VISIBILITY_MASK)); if (ai.mKeepScreenOn) { needGlobalAttributesUpdate( true); } ai.mKeepScreenOn = lastKeepOn; } // 可以通过此监听器获得是否有新View被添加 if (mOnHierarchyChangeListener != null) { mOnHierarchyChangeListener .onChildViewAdded(this, child); } if ((child.mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE) { mGroupFlags |= FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE ; } }
疑问:
1. 为什么调用invalidate还调用requestLayout?
2. 为什么先requestLayout, invalidate之后才向ViewGroup中添加View?
0 1
- 安卓addView源码分析
- 天天记录 - Android addView源码分析
- 安卓源码分析(一)Andriod.mk
- 安卓 Drawable源码分析简述
- 安卓联系人源码 之AsyncTaskExecutors分析
- addview
- addview
- addView
- 安卓图表引擎AChartEngine - 示例源码概述和分析
- 【安卓笔记】Handler,Looper,MessageQueue,Message源码分析
- Androidpn 消息推送安卓客户端源码分析
- 安卓照相机源码分析2——PreviewFrameLayout类
- 【安卓】从源码的角度深入分析Scroller
- 【安卓】从源码的角度深入分析Scroller
- 安卓 属性动画 ValueAnimator ObjectAnimator 源码分析 关键处
- 安卓Swipemenulistview侧滑删除 源码分析
- 安卓系统源码之目录结构分析
- 利用infer工具分析安卓android源码
- RelativeLayout用到的一些重要的属性
- 与 Visual Studio 共舞
- linux下搭建android开发环境
- 自起的布局文件类型与列表类型
- hdfs 要注意的几个问题
- 安卓addView源码分析
- [ZOJ 3471] Most Powerful · 状压DP
- hdfs命令
- struts (一)
- 连续整数固定和(C百例 & 51nod 1138)
- Lowest Common Ancestor of a Binary Search Tree
- PAT乙级 1009. 说反话
- Linux-6.2-网络编程基础
- 生活一二事谈风险预防的重要性