Android databinding: set default visibility in xml

元气小坏坏 提交于 2020-06-24 21:30:30

问题


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)


回答1:


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"/>



回答2:


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



来源:https://stackoverflow.com/questions/50296658/android-databinding-set-default-visibility-in-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!