As per http://developer.android.com/tools/data-binding/guide.html#imports, we can have such simple expressions in visibility:
<TextView .. android:visibility="@{user.isAdult ? View.VISIBLE : View.GONE}"/>
But when I try to do the same in an include
tag, like so:
<include android:id="@+id/image_layout" layout="@layout/image_layout" android:visibility="@{notification.notifType == 0 ? View.VISIBLE : View.GONE}"/>
Then Studio not only shows the expression in red, but upon building it gives the following error in the auto-generated binding class:
Error:(138, 29) error: cannot find symbol method setVisibility(int)
Here's where the error occurs in the auto-generated binding class
// batch finished if ((dirtyFlags & 0x3L) != 0) { // api target 1 this.imageLayout.setVisibility(NotifTypeNotificatio1); } imageLayout.executePendingBindings();
There is nothing ViewBinding can do that DataBinding cannot but it costs longer build times. Remember you don't need to use both, if you are using DataBinding, there is no need adding ViewBinding.
The Bindable annotation should be applied to any getter accessor method of an Observable class. Bindable will generate a field in the BR class to identify the field that has changed. When applied to an accessor method, the Bindable annotation takes an optional list of property names that it depends on.
Generated data binding code automatically checks for null values and avoid null pointer exceptions. For example, in the expression @{user.name} , if user is null, user.name is assigned its default value of null . If you reference user. age , where age is of type int , then data binding uses the default value of 0 .
I imagine what you are trying to do would look something like this:
In the layout you are including, specify a boolean variable and bind it to the desired view's visibility
<layout xmlns:android="http://schemas.android.com/apk/res/android"> <data> <import type="android.view.View"/> <variable name="isVisible" type="boolean"/> </data> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="@{isVisible ? View.VISIBLE : View.GONE}"/> </layout>
Then In your calling layout bind your value
<include android:id="@+id/image_layout" layout="@layout/image_layout" bind:isVisible="@{notification.notifType == 0}"/>
Try:
this.imageLayout.getRoot().setVisibility(NotifTypeNotificatio1);
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