Can anyone who uses android databinding library (com.android.databinding:dataBinder) comment about this beta library? In android developer site says "It might contain bugs, and it might not work for your use case, so use it at your own risk. " , so any issues or significant bug or bugs about it ?
I've been playing around with the databinding library for the past couple of weeks and it is surprisingly robust considering it is the very first version.
The only bug I have found so far has a workaround. I'll explain it below.
When using an <include>
tag in your databinding layout file (a layout file which now uses the <layout>
tag as the root), the generated code creates a binding for the <include>
tag's parent ViewGroup.
When inflating the view using the DataBindingUtil, the application will crash with a when it tries to resolve the ViewGroup. There appears to be a different behaviour between the code generator and the runtime binding logic.
Problem Example
Here is an example layout with the problem mentioned above.
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
</data>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- This include causes no issues -->
<include
layout="@layout/view_content"/>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- This include however causes the data binding to crash on the ScrollView -->
<include
layout="@layout/view_content"/>
</ScrollView>
</RelativeLayout>
</layout>
And here is the included layout.
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
</data>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Test"/>
</layout>
When attempting to use DataBindingUtil.setContentView the following crash occurs.
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ScrollView.setTag(java.lang.Object)' on a null object reference
Solution (Workaround)
The temporary workaround I found is to bind a dummy value to the <include>
tag's parent ViewGroup. This allows the databinder to find the ViewGroup at runtime, avoiding the crash.
Here is an example of the fix in action:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bind="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewModel"
type="com.example.ViewModel"/>
</data>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- This include causes no issues -->
<include
layout="@layout/view_content"/>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
bind:visibility="@{viewModel.dummyVisibility}">
<!-- This include will not cause a problem now that the ScrollView has a value being bound -->
<include
layout="@layout/view_content"/>
</ScrollView>
</RelativeLayout>
</layout>
And here is the very basic view model:
package com.example;
import android.databinding.BaseObservable;
import android.databinding.Bindable;
import android.view.View;
public class ViewModel extends BaseObservable {
@Bindable
public int getDummyVisibility() {
// TODO: This is a work around. Currently data binding crashes on certain views if they don't have binding.
return View.VISIBLE;
}
}
Hopefully this is fixed in future and this workaround won't be required!
Edit
I found another issue regarding custom binding adapters which I have raised at https://code.google.com/p/android-developer-preview/issues/detail?id=2421
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With