-
Notifications
You must be signed in to change notification settings - Fork 561
Add ViewGroup.inflate extension #162
base: master
Are you sure you want to change the base?
Conversation
| } | ||
|
|
||
| /** | ||
| * Inflates the given layoutId into the ViewGroup. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you be more specific and explain that the inflated view is added as a child to this ViewGroup?
| --> | ||
|
|
||
| <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
| android:id="@+id/test_container" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Incorrect indentation. See https://github.com/android/android-ktx/blob/master/src/androidTest/res/layout/test_attrs.xml for example
| * | ||
| * @see LayoutInflater.inflate | ||
| */ | ||
| fun ViewGroup.inflate(@LayoutRes layoutId: Int, attachToRoot: Boolean = true): View { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like attachToRoot being true by default. I would however rename it here to attachToSelf or attachToThis instead? (or any other better idea you might have)
00679e4 to
b192e60
Compare
|
Just for consideration. Methods that take a boolean as default are a bit confusing, because one never knows what's the default. This one is specially confusing, as the gut feeling is thinking that the default value is What do you think about having two separate methods? viewGroup.inflate(R.layout.item)
viewGroup.inflateAndAttach(R.layout.item)The method name says what it's doing without having to go to the code / reference and check what's the default. |
|
I don't see why a default value for a boolean is any different than for other types? The fact that the default matches Android's existing methods makes sense for consistency (and the default value should be documented). |
|
Booleans tend to be confusing for method arguments in general, that was point of my suggestion. There's quite some literature about it: https://martinfowler.com/bliki/FlagArgument.html But I also see your point, people used to the SDK methods will easily understand this too, and for this library consistency may be better. |
|
I agree that booleans tend to be confusing but this particular API has been around since 1.0 so I think consistency wins here. |
b192e60 to
a0e6d43
Compare
|
I agree that the |
|
Agreed, was a random idea, but true that consistency wins in this case. |
a0e6d43 to
32e2c11
Compare
|
I think |
|
I really don't like having inflate methods use different default behaviors. |
|
I Agree that we should keep constancy with the platform, attachToRoot is by default true if the viewGroup is not null. In the mean time we need to be able to override this behavior (especially for adapter). Actually, We should go deeper, we only need a context to inflate a view, so we should have three extensions like: |
Must say that I use it a lot too in custom views, where the value is usually |
|
An alternative is to automatically cast to the receiver type: @Suppress("UNCHECKED_CAST")
fun <T : View> ViewGroup.inflate(@LayoutRes layoutRes: Int, attachToRoot: Boolean = true): T =
LayoutInflater.from(context).inflate(layoutRes, this, attachToRoot) as T |
|
@tetedoie That sounds good! Could allow handling every case, one of the problems I talked about in my comment. But maybe in different PRs so that it can be discussed more easily? @clhols Not sure if that's very useful, but I don't really see why not. @romainguy How does it look for you? |
In my experience, it's helpful in adapters, where you need to use the result of the inflation (to add it to a VH for instance), but not so much in custom views. Kotlin forces you to specify the type, so if you're not assigning the result to a variabe, you need to make it explicit. It would look like this on a custom view: inflate<View>(R.layout.view_custom) |
|
It would be consistent with what was done for findViewById: |
5143977 to
53eaae0
Compare
|
@clhols Agreed, it makes sense. @romainguy Still looks good to you? |
|
@Bhullnatik can you update with the latest |
4f37386 to
a4c94f0
Compare
02f13aa to
2c5a194
Compare
Sorry for opening a PR straight away, but it was so simple I just wrote it.
This is a method I add to every one of my Kotlin projects. The easiest/simplest way is to use it in
RecyclerView.Adapter.createViewHolderlike so:but can be used anywhere.
The points I am not sure about:
ViewGroupcan be null as it would require passing anotherContextas a parameter since we can't use the one from theViewGroup. But that's out of the scope-ish since it's an extension method forViewGroup?attachToRoottotrue. Mainly to mirror the Android API but as I said the main use case is to use inside aRecyclerView.Adapterin which case it should befalse, but I don't think it's a good enough reason/too confusing.Anyway thanks for the library, very cool. 👍🏾