android:在显示之前具有条件延迟的可见性

标签 android data-binding

我正在尝试使用 android 数据绑定(bind)。但是当我在条件下使用 android:visibility 时遇到了一些问题。比如 android:visibility="size == 0 ? View.GONE : View.VISIBLE”。

当它返回 false 时,它​​会先显示这个 View ,然后一会儿它就消失了。

我可以解决这个问题吗?或者这是数据的错误?

最佳答案

我曾经尝试过这样的代码 android:visibility="@{size==0?View.GONE:View.VISIBLE}", 但是编译不了,不知道为什么,可能是bug。

所以我使用以下代码代替,首先,编写一个 Helper。

public class Helper {
    @BindAdapter("bind:attr")
    public static void setAttr(View view, Object obj) {
        if (someCondition(obj)){
            view.doSomething();
        }
    }
}

其次,在xml文件中使用自定义属性

<View
    ...other attrs...
    app:attr="@{obj}" />

note:

the 'attr' in the annotation must be the same with 'attr' used in xml file.

the View in the static method must be the same with the View in xml file.

我的Gradle版本和dataBinder版本

dependencies {
    classpath 'com.android.tools.build:gradle:1.5.0'
    classpath "com.android.databinding:dataBinder:1.+"
}

并且,删除与此 View 无关的其他操作。

或提供您的 Activity 代码。

关于android:在显示之前具有条件延迟的可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35787096/

相关文章:

安卓聊天应用开发

php - JSONObject 无法转换为 JSONArray - 我看不到 ListView

c# - 在单元格值中绑定(bind) WPF DataGrid 列标题文本

c# - 使用 ItemsSource 将预定义项添加到 ComboBox

android - 以编程方式更改 xml 创建的 View

java - 如何在android中定义地理围栏的矩形

android - iOS:如何实现相对布局

javascript - 在angulajs中转换java对象中的json时,获取嵌套异常是com.fasterxml.jackson.databind.JsonMappingException

wpf - 如果第一个属性是 "undefined",则绑定(bind)到第二个属性

wpf - IValueConverter 上的数据绑定(bind)