I'm using databinding in my project and I have one for setting a visibility condition from the viewmodel:
<View
app:visibilityCondition="@{viewModel.showingItems}" />
This all works fine, but suddenly when I want to use it on an include tag like this:
<include
layout="@layout/my_include_layout
app:visibilityCondition="@{viewModel.showingItems}" />
It doesn't build, with the following error:
e: [kapt] An exception occurred: android.databinding.tool.util.LoggedErrorException: Found data binding errors. Cannot find the setter for attribute 'app:visibilityCondition' with parameter type boolean on com.example.CustomBinding.
Since the CustomBinding
class doesn't actually extend from View
, but instead ViewDataBinding
, it doesn't look like I have a way to do this.
Is there a way around that, or am I forced to set the visibility of this included layout programatically? I know that will work, but I'd really like to keep it within the databinding if possible.
Apparently, currently you are not able to use BindingAdapters
with included
layout elements, but you can pass your variables inside the included layouts (for them to handle).
What Keshav Aggarwal proposed is almost ok. You will have to pass the data inside the included
layout, but exposing the whole ViewModel
inside the layout is unnecessary and a bit inelegant.
my_include_layout
, adding a variable with the bound parameter.<layout>
<data>
<variable
name="visibilityCondition"
type="<the_type_of_the_visibility>"/>
</data>
<View
app:visibilityCondition="@{visibilityCondition}"/>
</layout>
bind
namespace to pass the visibility parameter inside the included
layout:<include
layout="@layout/my_include_layout
bind:visibilityCondition="@{viewModel.showingItems}" />
So, there is some catch if you want Data Binding to work when you are using include
in your layouts, So do these things:
Step 1: Change your xml file in which you are using include
tag to something like this. Look closely how i am playing with the variable names in this Data Binding.
<data>
<import type="com.example.jenny.MyViewModel"/>
<variable
name="viewModelNew"
type="MyViewModel"/>
<include
layout="@layout/my_include_layout
app:viewModel="@{viewModelNew}"/> <!--this, viewModel is the variable declared in xml which you are including here-->
</data>
Step 2: And in the layout which you are including i.e. in my_include_layout
, do the Real Data Binding there like this:
<data>
<import type="com.example.jenny.MyViewModel"/>
<variable
name="viewModel"
type="MyViewModel"/>
</data>
<View
app:visibilityCondition="@{viewModel.showingItems}"/><!--the real Data Binding is happening here-->
Step 3: Also, in your activity or fragment, where ever you are initializing Data Binding, do write
dataBinding.viewModelNew = new MyViewModel() // or something like this, you have to initialze the data binding variable
Let me know, if this helps.
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