View的绘制流程 一、LayoutInflater.inflate详解

今天的主题是LayoutInflate是怎样获取布局并将其添加到父控件中去的,闲话不多说直接切入正题。

一、LayoutInflate的创建

一般我们添加布局时调用的方法有这么几种:

1、View.inflate(parent.getContext(), R.layout.item_layout, null);
2、LayoutInflater.from(context).inflate(R.layout.item_layout, parent);
3、LayoutInflater.from(context).inflate(R.layout.item_layout, parent,false);
4、getLayoutInflater().inflate(R.layout.item_layout, parent);
5、getLayoutInflater().inflate(R.layout.item_layout, parent,false);

那么上面这些方法又有哪些区别呢,带着在这个疑问我们来阅读源码吧。
首先是View.inflate(parent.getContext(), R.layout.item_layout, null);

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

View.inflate(parent.getContext(), R.layout.item_layout, null);方法最终会调用LayoutInflater.from(context)方法来获得LayoutInflater对象。那我们来看看LayoutInflater.from(context);中做了哪些处理

public static LayoutInflater from(Context context) {
    LayoutInflater LayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (LayoutInflater == null) {
        throw new AssertionError("LayoutInflater not found.");
    }
    return LayoutInflater;
}

这里我们看到LayoutInflater最终是通过context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);来创建的。我们再看看Activity中的getLayoutInflater()对象是怎样获取LayoutInflater

@NonNull
public LayoutInflater getLayoutInflater() {
    return getWindow().getLayoutInflater();
}
@Override
public LayoutInflater getLayoutInflater() {
    return mLayoutInflater;
}

public PhoneWindow(Context context) {
    super(context);
    mLayoutInflater = LayoutInflater.from(context);
}

通过上面的代码我们可以很清晰的看到在Activity中调用getLayoutInflate()方法会获取PhoneWindow中的mLayoutInflater对象而mLayoutInflater对象实在PhoneWindow被创建的时候调用LayoutInflater.from(context);来赋值的。
根据之前的代码我们看到不论通过哪种形式的调用最终是通过context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);方法来创建LayoutInflater对象的。

二、inflate()

我们经常用的inflate方法主要是有两个,一个是两个参数的另外一个是三个参数的,我们分别来看看他们的区别吧

public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {
    return inflate(resource, root, root != null);
}

在上面源码中我们看到两个参数的inflate是调用三个参数的inflate方法,而第三个参数则传入为父容器是否为空的布尔型参数下面我们通过源码来分析一下这三个参数的作用

public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {
    final Resources res = getContext().getResources();
    if (DEBUG) {
        Log.d(TAG, "INFLATING from resource: \"" + res.getResourceName(resource) + "\" ("
                + Integer.toHexString(resource) + ")");
    }

    final XmlResourceParser parser = res.getLayout(resource);
    try {
        return inflate(parser, root, attachToRoot);
    } finally {
        parser.close();
    }
}

这个方法很简单就是获取资源布局使用XmlResourceParser进行保存。并调用inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot)方法。

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

        final Context inflaterContext = mContext;
        final AttributeSet attrs = Xml.asAttributeSet(parser);
        Context lastContext = (Context) mConstructorArgs[0];
        mConstructorArgs[0] = inflaterContext;
        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("**************************");
            }

            //这里判断根节点是否为merge标签如果是merge标签并且没有将资源文件往父容器添加的意图就会跑出异常
            //merge标签是不能单独存在的
            if (TAG_MERGE.equals(name)) {
                if (root == null || !attachToRoot) {
                    throw new InflateException(" can be used only with a valid "
                            + "ViewGroup root and attachToRoot=true");
                }
                //遍历子节点填充到父容器中,直接返回父容器
                rInflate(parser, root, inflaterContext, attrs, false);
            } else {
                // Temp is the root view that was found in the xml
                final View temp = createViewFromTag(root, name, inflaterContext, attrs);

                ViewGroup.LayoutParams params = null;

                //如果存在父容器我们就根据attrs来设置View的参数
                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);
                    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 against its context.
                //遍历子节点将其填充到跟View中
                rInflateChildren(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.
                //如果满足条件则将跟View添加到父容器中并返回父容器否则返回跟View
                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) {
            final InflateException ie = new InflateException(e.getMessage(), e);
            ie.setStackTrace(EMPTY_STACK_TRACE);
            throw ie;
        } catch (Exception e) {
            final InflateException ie = new InflateException(parser.getPositionDescription()
                    + ": " + e.getMessage(), e);
            ie.setStackTrace(EMPTY_STACK_TRACE);
            throw ie;
        } finally {
            // Don't retain static reference on context.
            mConstructorArgs[0] = lastContext;
            mConstructorArgs[1] = null;

            Trace.traceEnd(Trace.TRACE_TAG_VIEW);
        }
        return result;
    }
}

这一段代码就比较有意思了首先告诉我们merge标签不能独立于父容器存在的,其次告诉我们如果不传入父容器则不会给根View设置宽高属性,也就是说我们在xml文件中设置的layout_widthlayout_height都失去了作用。
我们再来看看rInflate:

 void rInflate(XmlPullParser parser, View parent, Context context,
              AttributeSet attrs, boolean finishInflate) throws XmlPullParserException, IOException {

    final int depth = parser.getDepth();
    int type;

    //遍历子节点并创建Viwe添加到父容器中
    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_TAG.equals(name)) {
            parseViewTag(parser, parent, attrs);
        } else if (TAG_INCLUDE.equals(name)) {
            if (parser.getDepth() == 0) {
                throw new InflateException(" cannot be the root element");
            }
            parseInclude(parser, context, parent, attrs);
        } else if (TAG_MERGE.equals(name)) {
            throw new InflateException(" must be the root element");
        } else {
            final View view = createViewFromTag(parent, name, context, attrs);
            final ViewGroup viewGroup = (ViewGroup) parent;
            final ViewGroup.LayoutParams params = viewGroup.generateLayoutParams(attrs);
            rInflateChildren(parser, view, attrs, true);
            viewGroup.addView(view, params);
        }
    }

    if (finishInflate) {
        parent.onFinishInflate();
    }
}

这段代码很简单主要是遍历子节点创建对应的View添加到parent中,其中对几个特殊的标签做了处理。其中merge标签不能在非根节点而include标签不能在根节点。到此我们的代码就分析完了。


总结:

  • LayoutInflate的创建最终是通过context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);来创建的。
  • inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)方法的三个参数分别代表着子布局,父容器,是否将子布局添加到父容器中。
  • 如果父容器不为空并且attachToRoot的值为true的时候,inflate方法会将子布局添加到父容器中并返回父容器的对象,否则返回子布局的View对象。
  • merge标签只能在根节点使用而include标签只能在非根节点使用。

你可能感兴趣的:(View的绘制流程 一、LayoutInflater.inflate详解)