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

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

问题描述

按照 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数据绑定:include标签的可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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