Closed chet-chen closed 4 years ago
Like ProfileFragment
in samples, ProfileActivity
can be simpler.
class ProfileFragment : Fragment(R.layout.profile) { private val viewBinding: ProfileBinding by viewBinding() }
class ProfileActivity : AppCompatActivity(R.layout.profile) { private val viewBinding: ProfileBinding by viewBinding(R.id.container) }
With new API:
class ProfileActivity : AppCompatActivity(R.layout.profile) {
private val viewBinding: ProfileBinding by viewBinding()
}
I checked such kind of solution before 1.0. Root of a ViewBinding
must be specified in activity, because it's not obvious what to take. WIth Fragment
ut's obvious what to take. I don't want to make confusion for activity.
@kirich1409 I think we can provide the first view inside the android.R.id.content
ViewGroup as the default view, in the "without reflection" approach like this:
public inline fun <A : ComponentActivity, T : ViewBinding> ComponentActivity.viewBinding(
crossinline vbFactory: (View) -> T, crossinline viewProvider: (A) -> View = { window.findViewById<ViewGroup>(android.R.id.content).getChildAt(0) }
): ViewBindingProperty<A, T> {
return viewBinding { activity: A -> vbFactory(viewProvider(activity)) }
}
And use it like this:
private val binding by viewBinding(ProfileBinding::bind)
This is working for Activities with the layoutRes ID passed as parameter.
This will come in handy in those cases where there aren't any view IDs associated with the root layout of the Activity.
I am migrating a big project from kotlin synthetic properties to view binding, and I don't have a root layout ID in almost all my Activities, so instead of adding them in all places, this default value can save me a lot of time
@premacck , good idea. I'll try to implement it in 1.4.0
@kirich1409 Any ETA on the 1.4.0 release?
I will work on it during the next week
Okay, in the meanwhile, may I offer a PR for the above change? :)
Yes. It will be good to check another point of view
Yes. It will be good to check another point of view
@kirich1409 Opened #32
Hi. What is the use case for that API?