Explanation of the inflate parameter

There are four combinations of root and attachtoroot, but one of them is meaningless.
I feel that this function is too wasteful. It's very obvious that boolean attachToRoot, this parameter is put in the front, and root is better understood later.
Do you want to attach the root node? After that, check whether the following node is empty. Is it the grammar of English? i go first!
attachToRoot
root  
true null Meaningless
true not null xml and a viewgroup. In the end, there should be two same viewgroups. In special cases
false null Return xml directly. In this case, the width and height attribute of the root point in xml will become the default value of warp [u context]. Generally do not use
false not null Only the outer layer of xml's parent group layout type is needed, which is generally used. This is the general idea when designing. xml and outer layer are the same layout type. This is a good connection.










Just seal it yourself. Save the memory of useless knowledge points. These set the knowledge points of the class. Too much change. Remember that there is nothing to use.
//activity helper
    public static class LS_Activity
    {
        
        public static View inflast_Normal(Context context,int rid,ViewGroup viewGroup)
        {
            return LayoutInflater.from(context).inflate(rid, viewGroup,false);
        }

        public static View inflast_WrapFather(Context context,int rid,ViewGroup viewGroup)
        {
            return LayoutInflater.from(context).inflate(rid, viewGroup,true);
        }

        public static View inflast_ClearArrer(Context context,int rid)
        {
            return LayoutInflater.from(context).inflate(rid, null,false);
        }
    }

 




Source code reduction.
public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) 
       {
            final Context inflaterContext = mContext;
            Context lastContext = (Context) mConstructorArgs[0];
            mConstructorArgs[0] = inflaterContext;
            
            final AttributeSet attrs = Xml.asAttributeSet(parser);
            View result = root;

            int type=getXmlPullParser.START_TAG;// Look for the root node.
            final String name = parser.getName();
            if (TAG_MERGE.equals(name) && root != null && attachToRoot==true) //If xml The root node of is merge.
            {
                rInflate(parser, root, inflaterContext, attrs, false);
            } 
            else 
            {
                final View temp = createViewFromTag(root, name, inflaterContext, attrs);
                ViewGroup.LayoutParams params = null;

                if (root != null && attachToRoot==false)//use xml Of as the root node, with additional parameters.
                {
                    params = root.generateLayoutParams(attrs);
                    temp.setLayoutParams(params);
                    rInflateChildren(parser, temp, attrs, true);
                }
                else if(root != null && attachToRoot==true)//use xml The upper layer is the root node and uses the layout type of the upper layer.
                {
                    root.addView(temp, params);
                }
                else if(root == null || attachToRoot==false)//use xml As root node of
                {
                    result = temp;
                }
            }

            
            return result;
    }

Tags: Java xml Attribute

Posted on Sat, 02 Nov 2019 04:06:27 -0700 by Evanthes