Ehsan
Ehsan

Reputation: 197

The Function of "parent, true" or "null, true"

I'm learning how to create ListView in Android Studio. I've looked at a lot of ListView examples and there is a part in the examples which I don't understand why is used. The following lines of code include the part I mean:

View rowView = inflater.inflate(R.layout.second_layout, parent, false//the function of "parent, false"?);

View itemView = inflater.inflate(R.layout.listview_item, null, true//the function of "null, true"?);

What are "parent, true" and "null, true" used for?

Any help is appreciated

Upvotes: 0

Views: 64

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007624

The second parameter (parent or null) in your samples indicates the eventual parent of the root view being inflated from the layout file. Mostly, this is to help RelativeLayout interpret its layout rules properly.

The third parameter (true or false) indicates whether the inflated view should be added as a child of the designated parent immediately (true) or not (false).

Your second example should never be used, as there is no parent to add the inflated view to, and so true makes no sense here.

If you are using inflate(), the first form is the one you will use almost all of the time. If you know the parent, provide it. Usually, you will pass false for the third parameter, as something else (ListView, RecyclerView, FragmentManager, etc.) will determine when the inflated child is attached to its parent.

Upvotes: 2

Related Questions