Android源码解析之LayoutInflater原理

看了郭神的源码解析之后,感觉需要为他提供的源码加上一步一步的中文注释,才会更好的理解代码的逻辑,不然我看完给我的感觉是源码依旧博大精深!献上郭神原博客地址:http://blog.csdn.net/guolin_blog/article/details/12921889,方便大家参照着看。

直接show codes了。

//传入3个参数:已解析布局文件的解析者,根布局,是否将布局放到根布局上
    public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot) {
        synchronized (mConstructorArgs) {
            //用来跟踪该inflate方法
            Trace.traceBegin(Trace.TRACE_TAG_VIEW, "inflate");
            //得到布局文件的所有属性值
            final AttributeSet attrs = Xml.asAttributeSet(parser);
            Context lastContext = (Context)mConstructorArgs[0];
            mConstructorArgs[0] = mContext;
            View result = root;

            try {
                // Look for the root node.寻找根节点
                int type;
                //当解析事件等于开始标记或者等于文档结束标记时跳出循环
                while ((type = parser.next()) != XmlPullParser.START_TAG &&
                        type != XmlPullParser.END_DOCUMENT) {
                    // Empty
                }
                //当解析事件不等于开始标记时抛出填充异常:未找到开始标记
                if (type != XmlPullParser.START_TAG) {
                    throw new InflateException(parser.getPositionDescription()
                            + ": No start tag found!");
                }
                //得到节点名字
                final String name = parser.getName();

                if (DEBUG) {
                    System.out.println("**************************");
                    System.out.println("Creating root view: "
                            + name);
                    System.out.println("**************************");
                }
                //节点为合并标签
                if (TAG_MERGE.equals(name)) {
                    //如果根布局为空或没有放到根布局上就抛出异常:合并标签只能在一个有效的根布局和放到根布局上时才能使用
                    if (root == null || !attachToRoot) {
                        throw new InflateException("<merge /> can be used only with a valid "
                                + "ViewGroup root and attachToRoot=true");
                    }
                    //循环遍历子view
                    rInflate(parser, root, attrs, false);
                } else {
                    // Temp is the root view that was found in the xml
                    //temp是布局文件xml中找到的根布局
                    View temp;
                    //?????否则创建temp view的实例
                    if (TAG_1995.equals(name)) {
                        temp = new BlinkLayout(mContext, attrs);
                    } else {
                        temp = createViewFromTag(root, name, attrs);
                    }

                    ViewGroup.LayoutParams params = null;
                    //根布局不为空时,该布局参数生成由根布局和xml中设置的属性决定。
                    if (root != null) {
                        if (DEBUG) {
                            System.out.println("Creating params from root: " +
                                    root);
                        }
                        // Create layout params that match root, if supplied
                        params = root.generateLayoutParams(attrs);
                        //如果xml不放到根布局中,则将这个布局参数设置给xml布局
                        if (!attachToRoot) {
                            // Set the layout params for temp if we are not
                            // attaching. (If we are, we use addView, below)
                            temp.setLayoutParams(params);
                        }
                    }

                    if (DEBUG) {
                        System.out.println("-----> start inflating children");
                    }
                    // Inflate all children under temp
                    //填充temp布局下的所有子view
                    rInflate(parser, temp, attrs, true);
                    if (DEBUG) {
                        System.out.println("-----> done inflating children");
                    }

                    // We are supposed to attach all the views we found (int temp)
                    // to root. Do that now.
                    //将所有视图放到根布局中
                    if (root != null && attachToRoot) {
                        root.addView(temp, params);
                    }

                    // Decide whether to return the root that was passed in or the
                    // top view found in xml.
                    //是否将根布局一起返回
                    if (root == null || !attachToRoot) {
                        result = temp;
                    }
                }

            } catch (XmlPullParserException e) {
                InflateException ex = new InflateException(e.getMessage());
                ex.initCause(e);
                throw ex;
            } catch (IOException e) {
                InflateException ex = new InflateException(
                        parser.getPositionDescription()
                        + ": " + e.getMessage());
                ex.initCause(e);
                throw ex;
            } finally {
                // Don't retain static reference on context.
                //不要保留对上下文的静态引用
                mConstructorArgs[0] = lastContext;
                mConstructorArgs[1] = null;
            }
            //追踪方法结束
            Trace.traceEnd(Trace.TRACE_TAG_VIEW);

            return result;
        }
    }

rInflate()方法

/**
     * Recursive method used to descend down the xml hierarchy and instantiate
     * views, instantiate their children, and then call onFinishInflate().
     * 这个递归方法被用来确定xml布局层次并且实例化视图和他们的子视图,然后调用onFinishInflate方法
     */
    void rInflate(XmlPullParser parser, View parent, final AttributeSet attrs,
            boolean finishInflate) throws XmlPullParserException, IOException {
        //得到xml文档层次数
        final int depth = parser.getDepth();
        int type;
        //当解析事件不等于结束标记或者当前解析层次>循环前已解析的层次   且解析事件不等于文档结束
        while (((type = parser.next()) != XmlPullParser.END_TAG ||
                parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
            //如果解析事件不等于开始标记,开始新的循环
            if (type != XmlPullParser.START_TAG) {
                continue;
            }
            //得到解析器当前指向标签名称
            final String name = parser.getName();
            //如果该标签为请求焦点,则??
            if (TAG_REQUEST_FOCUS.equals(name)) {
                parseRequestFocus(parser, parent);
            } else if (TAG_INCLUDE.equals(name)) {//否则如果该标签为include标签
                //如果当前解析层次为0层,抛出填充异常:<include />不能作为根元素
                if (parser.getDepth() == 0) {
                    throw new InflateException("<include /> cannot be the root element");
                }
                //解析include标签的情况
                parseInclude(parser, parent, attrs);
            } else if (TAG_MERGE.equals(name)) {//否则如果该标签为marge标签,抛出异常:**标签必须作为根元素
                throw new InflateException("<merge /> must be the root element");
            } else if (TAG_1995.equals(name)) {//否则如果该标签为blink标签
                //???
                final View view = new BlinkLayout(mContext, attrs);
                //生成该视图的布局参数,先递归填充该视图中的子视图,最后增加到父布局中
                final ViewGroup viewGroup = (ViewGroup) parent;
                final ViewGroup.LayoutParams params = viewGroup.generateLayoutParams(attrs);
                rInflate(parser, view, attrs, true);
                viewGroup.addView(view, params);                
            } else {
                //否则直接实例化该视图,生成该视图的布局参数,然后先递归填充该视图中的子视图,最后增加到父布局中
                final View view = createViewFromTag(parent, name, attrs);
                final ViewGroup viewGroup = (ViewGroup) parent;
                final ViewGroup.LayoutParams params = viewGroup.generateLayoutParams(attrs);
                rInflate(parser, view, attrs, true);
                viewGroup.addView(view, params);
            }
        }
        //如果填充结束,调用父布局的onFinishInflate方法
        if (finishInflate) parent.onFinishInflate();
    }

打??的地方我还需要去学习才能理解,如有理解的朋友欢迎解答我的疑问,感激不尽!欢迎大家一起学习讨论。先到此,后续会接着写另一个方法…

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值