Android 数据绑定:包含标签的可见性 [英] Android Data Binding: visibility on include tag

查看:41
本文介绍了Android 数据绑定:包含标签的可见性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 http://developer.android.com/tools/data-binding/guide.html#imports,我们可以在可见性中有这样简单的表达式:

As per http://developer.android.com/tools/data-binding/guide.html#imports, we can have such simple expressions in visibility:

<TextView
..
android:visibility="@{user.isAdult ? View.VISIBLE : View.GONE}"/>

但是当我尝试在 include 标签中做同样的事情时,就像这样:

But when I try to do the same in an include tag, like so:

<include
android:id="@+id/image_layout"
layout="@layout/image_layout"
android:visibility="@{notification.notifType == 0 ? View.VISIBLE : View.GONE}"/>

然后 Studio 不仅将表达式显示为红色,而且在构建时在自动生成的绑定类中给出以下错误:

Then Studio not only shows the expression in red, but upon building it gives the following error in the auto-generated binding class:

错误:(138, 29) 错误:找不到符号方法 setVisibility(int)

Error:(138, 29) error: cannot find symbol method setVisibility(int)

这里是自动生成的绑定类发生错误的地方

Here's where the error occurs in the auto-generated binding class

// batch finished
if ((dirtyFlags & 0x3L) != 0) {
    // api target 1
    this.imageLayout.setVisibility(NotifTypeNotificatio1);
}
imageLayout.executePendingBindings();

推荐答案

我想你正在尝试做的事情看起来像这样:

I imagine what you are trying to do would look something like this:

在您包含的布局中,指定一个布尔变量并将其绑定到所需视图的可见性

<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>

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

        <variable
            name="isVisible"
            type="boolean"/>

    </data>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="@{isVisible ? View.VISIBLE : View.GONE}"/>

</layout>

然后在你的调用布局中绑定你的值

<include
    android:id="@+id/image_layout"
    layout="@layout/image_layout"
    bind:isVisible="@{notification.notifType == 0}"/>

这篇关于Android 数据绑定:包含标签的可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆