Lion
Lion

Reputation: 1035

Is there any difference between the two methods of inflating a child view?

The first method:

LinearLayout parent = ...;
View child = LayoutInflator.inflate(context, parent, true);

The second method:

LinearLayout parent = ...;
View child = LayoutInflator.inflate(context, null, false);
parent.addView(child);

Is there any difference?

Upvotes: 4

Views: 2052

Answers (6)

DynamicMind
DynamicMind

Reputation: 4258

Yes, there is a difference.

if True : Whether the inflated hierarchy should be attached to the root parameter?

if False: If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.

This parameter generally use when we don't want to use Adapter class for adding row in Listview we want to make row by view or layout they have child view in that case we use it.

Upvotes: 0

kvh
kvh

Reputation: 2158

According to Android Developer documentation:

View child = LayoutInflator.inflate(context, parent, true);

Will add the child to parent,

View child = LayoutInflator.inflate(context, null, false);

Will not.

you can check out the reference: android.view.ViewGroup.inflate

Upvotes: 1

Gustek
Gustek

Reputation: 3760

If You check source of inflate method You will find:

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);
     }
}

/* ... */

// 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);
}

So in Your example there is no difference.

There would be a difference in this scenario

View child = LayoutInflator.inflate(context, parent, false);

A child would have same LayoutParams as parent but it wouldn't be attached so it would be just separate view.

Upvotes: 3

Ivan
Ivan

Reputation: 713

From the source code as below:

471                    if (root != null) {
472                        if (DEBUG) {
473                            System.out.println("Creating params from root: " +
474                                    root);
475                        }
476                        // Create layout params that match root, if supplied
477                        params = root.generateLayoutParams(attrs);
478                        if (!attachToRoot) {
479                            // Set the layout params for temp if we are not
480                            // attaching. (If we are, we use addView, below)
481                            temp.setLayoutParams(params);
482                        }
483                    }
484
485                    if (DEBUG) {
486                        System.out.println("-----> start inflating children");
487                    }
488                    // Inflate all children under temp
489                    rInflate(parser, temp, attrs, true);
490                    if (DEBUG) {
491                        System.out.println("-----> done inflating children");
492                    }
493
494                    // We are supposed to attach all the views we found (int temp)
495                    // to root. Do that now.
496                    if (root != null && attachToRoot) {
497                        root.addView(temp, params);
498                    }
499
500                    // Decide whether to return the root that was passed in or the
501                    // top view found in xml.
502                    if (root == null || !attachToRoot) {
503                        result = temp;
504                    }

From the line 471 ~ 482, it will set layout params of child view a new created params which match parent view.

From line 496~498, the parent add the child view with the layout params.

So the difference is whether set a layout params to child view

Upvotes: 0

sandrstar
sandrstar

Reputation: 12643

Assume You've called context the resource id to inflate, which is quite confusing. There's the following difference:

Usage of layout parameters from layout resource top level, becase on parent layout parameters. In second case these layout parameters won't be applied;

Upvotes: 0

Raghav Sood
Raghav Sood

Reputation: 82563

The difference is your second argument, which basically tells Android which View is the parent View of the one you're inflating.

In the first case, the View will be inflated into the ViewGroup which which is the 'parent' instance. In the second case, the newly created View has no parent, and will be inflated as is.

Upvotes: 0

Related Questions