Android 数据绑定:在 xml 中设置默认可见性
Android databinding: set default visibility in xml
我在 recyclerview 中显示项目并使用 数据绑定。在 xml 布局中,我有这样的视图:
<include
android:visibility="@{viewmodel.expandable ? View.VISIBLE : View.GONE}"
bind:viewmodel="@{viewmodel}"
layout="@layout/full_station_layout"/>
它运行良好,但我有一个问题:当 recyclerview 初始化并将项目绑定到视图时,此布局在屏幕上闪烁一次,尽管初始值 viewmodel.expandable 为 false。因此,我决定暂时隐藏此布局并尝试在 xml 中使用 default 参数,如下所示:
<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 数据绑定规则中删除了这个关键字(我看过使用示例 default-以前 Google 开发人员 xml 中的关键字,但现在我不能)
您可以在default
属性中设置gone
、visible
、invisible
。替换为以下内容。
<include
android:visibility="@{viewmodel.expandable ? View.VISIBLE : View.GONE, default=gone}"
bind:viewmodel="@{viewmodel}"
layout="@layout/full_station_layout"/>
检查您是否已经导入视图class。
<data>
<import type="android.view.View"/>
<variable ..... />
</data>
此外,可见性默认值的默认正确语法是 default=gone
,没有 default=View.GONE
我在 recyclerview 中显示项目并使用 数据绑定。在 xml 布局中,我有这样的视图:
<include
android:visibility="@{viewmodel.expandable ? View.VISIBLE : View.GONE}"
bind:viewmodel="@{viewmodel}"
layout="@layout/full_station_layout"/>
它运行良好,但我有一个问题:当 recyclerview 初始化并将项目绑定到视图时,此布局在屏幕上闪烁一次,尽管初始值 viewmodel.expandable 为 false。因此,我决定暂时隐藏此布局并尝试在 xml 中使用 default 参数,如下所示:
<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 数据绑定规则中删除了这个关键字(我看过使用示例 default-以前 Google 开发人员 xml 中的关键字,但现在我不能)
您可以在default
属性中设置gone
、visible
、invisible
。替换为以下内容。
<include
android:visibility="@{viewmodel.expandable ? View.VISIBLE : View.GONE, default=gone}"
bind:viewmodel="@{viewmodel}"
layout="@layout/full_station_layout"/>
检查您是否已经导入视图class。
<data>
<import type="android.view.View"/>
<variable ..... />
</data>
此外,可见性默认值的默认正确语法是 default=gone
,没有 default=View.GONE