Android数据绑定:在xml中设置默认可见性 [英] Android databinding: set default visibility in xml
问题描述
我在recyclerview中显示项目并使用数据绑定.在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"/>
它工作正常,但是我有一个问题:尽管初始值viewmodel.expandable为false,但在recyclerview初始化并将项目绑定到视图时,此布局在屏幕上闪烁一次.因此,我决定暂时隐藏此布局,并尝试在XML中使用默认参数,如下所示:
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"/>
但是出了点问题:
error: 'View' is incompatible with attribute android:visibility (attr) enum [gone=2, invisible=1, visible=0].
因此,或者我不正确地使用了此参数,或者Google从xml数据绑定规则中删除了此关键字(以前我在Google开发人员中看到过xml中使用 default -keyword的示例,但现在我不能了) t)
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)
推荐答案
您可以在default
属性中设置gone
,visible
,invisible
.在下面替换.
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"/>
这篇关于Android数据绑定:在xml中设置默认可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!