安卓数据绑定(bind) : Is there a good way to show/hide an includes tag?

标签 android android-layout android-databinding

我刚刚开始弄乱 android 中的数据绑定(bind)。理想情况下,我希望有一个包含一些通用 xml 元素的根 xml 文件,然后它的内部内容可以是像这样的三个 xml 文件之一

<data>
  <variable name="action" type="com.example.android.action"/>
</data>

<TextView>
<TextView>
<!--Only show one of these includes based on the binding data-->

<!-- if action.item -->
<include layout="item.xml"
 bind:item="@{action.item}">

<!-- else if action.udpate -->
<include layout="update.xml"
 bind:update="@{action.update}">

<!-- else if action.video -->
<include layout="video.xml"
 bind:video="@{action.video}">

<TextView>
... etc

所以基本上基于操作(项目、更新或视频)中存在的子对象显示它的布局并绑定(bind) View 但不显示其他包含。我应该只使用 android 的 View:Visibility 还是有一些我忽略的内容?

最佳答案

Should I just use android's View:Visibility

是的,最好的方法是检查数据绑定(bind)布局中的 boolean 并相应地设置可见性,如下所示。

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    >

    <data>

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

        <variable
            name="action"
            type="com.example.android.action"/>
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include
            layout="@layout/item"
            android:visibility="@{action.item ? View.VISIBLE: View.GONE}"/>

        <include
            layout="@layout/update"
            android:visibility="@{action.someOtherObject!=null ? View.VISIBLE: View.GONE}"/>

    </LinearLayout>
</layout>

在这里你可以像上面一样检查NULLBoolean

关于安卓数据绑定(bind) : Is there a good way to show/hide an includes tag?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51251215/

相关文章:

android - 点击事件在MVVM架构中的位置

android - android 中源数据绑定(bind)的一种方法

android - 如何在Android中获取ObservableField的值

android - 取消日历应用程序中的完成按钮 - 它是操作栏的一部分吗?

android - 如何在 phonegap 应用程序中添加滚动条

android - 我们可以同时使用 Retrofit、RxJava 和 RxAndroid 吗?

Android studio compiledsdkversion 23 不工作

android - 从 getDrawingCache() 在 android 中获取空位图

android - Scrollview 在软输入键盘的回车键上自动滚动

android - 创建带权重的线性布局