使用Android数据绑定动态切换布局元素的可见性。

时间:2016-07-07 08:38:25

标签: android android-layout data-binding android-databinding

我正在构建一个Android应用,我希望根据按钮点击切换一些视图元素的可见性。我试图用数据绑定而不是使用findViewById()来存档它,但是直到现在我找到的所有解决方案都没有更新布局,当变量发生变化时。

这是我到目前为止所拥有的。 (我简化了代码,专注于问题)

Activicy.java

public class RecipeBinderHelper{
private Boolean presentationElementsVisible;
private Boolean timerElementsVisible;

public RecipeBinderHelper(){
    this.presentationElementsVisible = true;
    this.timerElementsVisible = false;
}
public void setPresentationViewVisible(boolean presentationElementsVisible) {
    this.presentationElementsVisible = presentationElementsVisible;
}
public Boolean getPresentationElementsVisible() {
    return presentationElementsVisible;
}
//getters and setters for private Boolean timerElementsVisible;
}

助手班

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
    <import type="android.view.View"/>
    <variable
        name="helper"
        type="com.myapps.recipeApp.RecipeBinderHelper"/>
    <variable
        name="recipe"
        type="com.myapps.recipeApp.Recipe"/>
</data>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.myapp.recipeApp.RecipeActivity"
    tools:showIn="@layout/activity_recipe">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/r_source"
        android:textStyle="bold"
        android:text="@{recipe.source}"
        android:visibility="@{helper.presentationElementsVisible ? View.VISIBLE : View.GONE}" />
<!-- More TextViews here -->
<!-- Button is located in parret layout -->
</RelativeLayout>
</layout>

布局

{{1}}

1 个答案:

答案 0 :(得分:3)

我强烈推荐阅读George Mount的posts about Android data-binding,它们非常有用。

为了解决这个问题,我将助手类扩展为BaseObservable,如the documentation中所述。

助手班

public class RecipeBinderHelper{
    private Boolean presentationElementsVisible;
    private Boolean timerElementsVisible;

    public RecipeBinderHelper(){
        this.presentationElementsVisible = true;
        this.timerElementsVisible = false;
    }
    public void setPresentationViewVisible(boolean presentationElementsVisible) {
        this.presentationElementsVisible = presentationElementsVisible;
        //Notifying change in the setter. 
        notifyPropertyChanged(BR.presentationElementsVisible);
    }
    //assigning Bindable annotation to the getter 
    @Bindable
    public Boolean getPresentationElementsVisible() {
        return presentationElementsVisible;
    }
    //getters and setters for private Boolean timerElementsVisible;
}

活动中的binding.notifyChange();不是必需的,可以删除。

当点击按钮时,应用程序现在可以根据需要删除TextView。

一个奇怪的事情是Android Studio(2.1.2,Ubuntu)给了我一个Cannot resolve symbol 'BR'警告,但该应用程序编译并按预期运行。