Difference between View.inflate and layoutinflate.inflate methods and source code

Keywords: Android

View.inflate and LayoutInflater.inflate are commonly used methods. They are always used back and forth without paying attention to the difference between them. Make a list to add color to the items. View.inflate can never be used, so we find the next source code.

Let's first look at the call of View.inflate:

public static View inflate(Context context, @LayoutRes int resource, ViewGroup root) {
        LayoutInflater factory = LayoutInflater.from(context);
        return factory.inflate(resource, root);
    }
  public View inflate(@LayoutRes int resource, @Nullable ViewGroup root){
        /**
        Please take a closer look at the location, root! = null, that is to say, when we have the third parameter in View.inflate
        Given null, attachToRoot has specified false
        */
        return inflate(resource, root, root != null);
    }

Here is the core:

             
            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 has an assignment only if root is not empty and attachToRoot is not empty
                            temp.setLayoutParams(params);
                        }
                    }

                    if (DEBUG) {
                        System.out.println("-----> start inflating children");
                    }

                    // Inflate all children under temp against its context.
                    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.
                    if (root != null && attachToRoot) {
                        root.addView(temp, params);
                    }

From the above we can see that only when root and attachToRoot are not empty, temp will set layoutParams, that is to say, if the parent control of the list entry needs to be processed, then root must not be empty, but in the list, we can't set root for View.inflate, because whether listview or recycleView is used, getVIew is already set when we call getVIew. "You must call removeview() on the child's parent first

So the solution:

1. Use layout inflate. From (ViewGroup. Getcontext()). Inflate (Android. R.layout. AAA, ViewGroup, false));

2. Apply one more layer to your layout. Don't you say that the outermost layer setting doesn't work? You can only draw the added view. Then I will set another layer outside my original parent control. When using, I will set the layout of the lower level of the parent control. Of course, this will cause excessive redrawing of the layout, which is not recommended.

Posted by iambradn on Wed, 30 Oct 2019 21:51:02 -0700