通过Android数据绑定动态切换布局元素的可见性. [英] Dynamically toggle visibility of layout elements with Android data-binding.

查看:137
本文介绍了通过Android数据绑定动态切换布局元素的可见性.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个Android应用程序,我想在其中基于按钮单击来切换某些视图元素的可见性.我正在尝试使用数据绑定而不是使用findViewById()对其进行存档,但是直到变量更改后,我到目前为止发现的所有解决方案都不会更新布局.

I am building an Android app, where I want to toggle the visibility of some view elements based on a button click. I am trying to archive it with data-binding instead of using findViewById(), but all the solutions I've have found until now doesn't update the layout, when the variable is changed.

这是我到目前为止所拥有的. (我已经简化了代码,以解决问题)

Here is what I have so far. (I've simplified the code, to focus on the problem)

Activicy.java

Activicy.java

public class RecipeActivity extends AppCompatActivity {
private Recipe recipe;
private ActivityRecipeBinding binding;
private RecipeBinderHelper rbhelper = new RecipeBinderHelper();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    recipe = intent.getParcelableExtra("recipe");
    binding = DataBindingUtil.setContentView(this, R.layout.activity_recipe);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle(recipe.getName());
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    binding.recipeContent.setRecipe(recipe);
    binding.recipeContent.setHelper(rbhelper);

    binding.Button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //HERE I CHANGE THE VALUE OF THE VARIBLE
            rbhelper.setPresentationViewVisible(false);
            binding.notifyChange();
        }
    });
}
}

助手类

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>

推荐答案

我强烈建议阅读George Mount的关于Android数据的帖子-绑定,它们非常有用.

I highly recommend to read George Mount's posts about Android data-binding, they are very useful.

为解决该问题,我将辅助程序类扩展为 BaseObservable 文档所述.

To solve the problem I extended the helper class as an BaseObservable as described in 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();不是必需的,可以将其删除.

The binding.notifyChange();in the Activity is not necessary and can be removed.

现在,当单击按钮时,该应用程序将根据需要删除TextView.

The app now removes the TextView as desired, when the button is clicked.

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

One peculiar thing is that Android Studio (2.1.2, Ubuntu) gives me a Cannot resolve symbol 'BR' warning, but the app compiles and runs as expected.

这篇关于通过Android数据绑定动态切换布局元素的可见性.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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