LayoutInflater.inflate()源码分析

在实际使用中,我们用inflate去初始化view的时候有以下三种写法:

    View.inflate(context, R.layout.track_top_item, null);

    getLayoutInflater().inflate(R.layout.track_top_item, null);

    LayoutInflater.from(context).inflate(R.layout.track_top_item, null, true);

这三种写法有什么区别呢? 让我们来从源码角度分析一下
View.inflate(context, R.layout.track_top_item, null)的源码:

public static View inflate(Context context, @LayoutRes int resource, ViewGroup root) {
    LayoutInflater factory = LayoutInflater.from(context);
    return factory.inflate(resource, root);
}

getLayoutInflater().inflate(R.layout.track_top_item, null)的源码:

public View inflate(XmlPullParser parser, @Nullable ViewGroup root) {
    return inflate(parser, root, root != null);
}

LayoutInflater.from(context).inflate(R.layout.track_top_item, null, true)的源码:

public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {
    final Resources res = getContext().getResources();

    // 根据layout resource id,获取该布局的XmlResourceParser对象
    final XmlResourceParser parser = res.getLayout(resource);
    try {
        return inflate(parser, root, attachToRoot);
    } finally {
        parser.close();
    }
}

从上面三个方法我们不难看出,前两个方法最后都会调用到第三个方法,而第三个方法最终调用下面这个方法,

public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
    synchronized (mConstructorArgs) {
        Trace.traceBegin(Trace.TRACE_TAG_VIEW, "inflate");

        final Context inflaterContext = mContext;
        // attrs是传入的布局layout在xml文件里面设置的属性集合
        final AttributeSet attrs = Xml.asAttributeSet(parser);
        Context lastContext = (Context) mConstructorArgs[0];
        mConstructorArgs[0] = inflaterContext;
        // 将传进来的父view赋值给result
        View result = root;

        try {
            // ...
            final String name = parser.getName();
            // 如果resource的根布局是merge标签
            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");
                }

                rInflate(parser, root, inflaterContext, attrs, false);
            } else {
                // temp是传入的参数resource的根布局View
                final View temp = createViewFromTag(root, name, inflaterContext, attrs);

                ViewGroup.LayoutParams params = null;

                // 如果传入的父view不为null
                if (root != null) {
                    // ...
                    // 根据attrs属性集,创建布局参数params
                    params = root.generateLayoutParams(attrs);
                    // 传入的attachToRoot为false,表示不添加到父类中
                    // 设置tempview的params属性
                    if (!attachToRoot) {
                        temp.setLayoutParams(params);
                    }
                }

                // 实例化temp视图内的所有子视图
                rInflateChildren(parser, temp, attrs, true);

                // 父view不为null 并且attachToRoot = true
                // 将temp添加到root中,并使用布局参数params
                if (root != null && attachToRoot) {
                    root.addView(temp, params);
                }

                // 父view为null 或者 attachToRoot = false
                if (root == null || !attachToRoot) {
                    // 将temp赋值给result(在此之前,result==root)
                    result = temp;
                }
            }

        } catch (XmlPullParserException e) {
            // ...
        }

        return result;
    }
}

大体流程解析:
1、定义方法的返回值result,将参数root赋值给result
2、实例化参数resource赋值给temp,这时temp是传入的参数resource的根布局View
3、当root != null时,创建布局参数params
如果 !attachToRoot,为temp设置布局参数params。
如果 attachToRoot,将temp添加到root中,并使用布局参数params。
4、当root == null || !attachToRoot,将temp赋值给result。
5、将result返回。
进一步简化:
只要root == null,无视attachToRoot的值,创建temp对象,返回temp。
当root != null时,分两种情况。
1、attachToRoot == true,将temp添加到root中,并使用布局参数params,返回root。
2、attachToRoot == false,为temp设置布局参数params,返回temp。
听说程序员看代码更能理解逻辑:

public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
    View result = null;
    final View temp = createViewFromTag(root, name, inflaterContext, attrs);
    if(root == null){
        result = temp;
    }else{
        ViewGroup.LayoutParams params = root.generateLayoutParams(attrs);
        if (attachToRoot) {
            root.addView(temp, params);
            result = root;
        } else {
            temp.setLayoutParams(params);
            result = temp;
        }
    }
    return result;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容