Lets assume that we have a following foo.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/zzz"
android:background="#f0f"
...
>
and a extended LinearLayout called SuperLinearLayout.kt
class SuperLinearLayout: LinearLayout {
...
val view = View.inflate(context, R.layout.foo, this)
binding = FooBinding.bind(view)
...
}
then somewhere in the SuperLinearLayout.kt
val a1 = findViewById<LinearLayout>(R.id.zzz)
val a2 = binding.zzz
val a3 = binding.root
output:
a1 -> android.widget.LinearLayout{eabfd59...
a1.background -> ColorDrawable@32089ff
a2 -> test.SuperLinearLayout{234fb46...
a2.background -> null
a3 -> test.SuperLinearLayout{234fb46...
a3.background -> null
why does the a1 reference the correct LinearLayout View with correct background ColorDrawable when a2 references the same object as a3 with both showing null background (a2 & a3 both reference the SuperLinearLayout)?
I've checked the generated binding and I've found that:
@NonNull
public static FooBinding bind(@NonNull View rootView) {
...
LinearLayout zzz = (LinearLayout) rootView;
...
}
Why am I loosing access to the correct View in my Layout hierarchy when using ViewBinding?