Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android databinding: set default visibility in xml

I show items in recyclerview and use databinding. In xml layout I has such view:

 <include
        android:visibility="@{viewmodel.expandable ? View.VISIBLE : View.GONE}"
        bind:viewmodel="@{viewmodel}"
        layout="@layout/full_station_layout"/>

It works well but I has one issue: while recyclerview initializing and bind items to views this layout flashes once on the screen although initial value viewmodel.expandable is false. So, I decided temporary hide this layout and tried using default-parameter in xml like this:

        <include
        android:visibility="@{viewmodel.expandable ? View.VISIBLE : View.GONE, default=View.GONE}"
        bind:viewmodel="@{viewmodel}"
        layout="@layout/full_station_layout"/>

But something went wrong:

error: 'View' is incompatible with attribute android:visibility (attr) enum [gone=2, invisible=1, visible=0].

So, or I incorrectly use this parameter or Google remove this keyword from xml databinding rules (I've seen example of usage default-keyword in xml on Google developers before, but now I couldn't)

like image 658
Alex Zezekalo Avatar asked May 11 '18 16:05

Alex Zezekalo


2 Answers

You can set gone, visible, invisible in default property. Replace with below.

<include
        android:visibility="@{viewmodel.expandable ? View.VISIBLE : View.GONE, default=gone}"
        bind:viewmodel="@{viewmodel}"
        layout="@layout/full_station_layout"/>
like image 95
Khemraj Sharma Avatar answered Nov 17 '22 06:11

Khemraj Sharma


Check if you have already imported the View class.

<data>
    <import type="android.view.View"/>
    <variable ..... />
</data>

Also, the default correct syntax for default value for visibility is default=gone, no default=View.GONE

like image 36
jantursky Avatar answered Nov 17 '22 08:11

jantursky