如何使用数据绑定更新布局元素的可见性

时间:2016-11-06 11:49:13

标签: java android android-layout data-binding

我很难理解如何使用数据绑定更新视图。我有一个我正在试验的简单例子,其中一个切换开关将使一个按钮在视图中出现/消失。

预期行为:
切换按钮应切换/上的“添加键”按钮(即VISIBLEGONE)。

实际行为:
“添加密钥”按钮的可见性不会更新。它只在加载布局时设置一次。

这是布局文件:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

        <variable
            name="mainActivity"
            type="tanager.sandbox.MainActivity" />

        <variable
            name="businessLogic"
            type="tanager.sandbox.BusinessLogic" />
    </data>

    <RelativeLayout
        android:id="@+id/activity_main"
        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"
        tools:context="tanager.sandbox.MainActivity">

        <LinearLayout
            android:id="@+id/linear_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <Button
                android:id="@+id/add_key_button"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="@{businessLogic.getVisible()}"
                android:text="@string/add_key" />

        </LinearLayout>

        <ToggleButton
            android:id="@+id/toggleButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@id/linear_layout"
            android:layout_weight="3"
            android:text="ToggleButton"
            android:onClick="@{() -> mainActivity.click()}"/>
    </RelativeLayout>
</layout>

当按下切换按钮时,它会调用主活动上的方法,该方法切换业务逻辑中的值。我希望add_key_button在按下切换按钮时自动更新其可见性。这是MainActivity

public class MainActivity extends AppCompatActivity {
    private BusinessLogic _businessLogic;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        _businessLogic = new BusinessLogic();

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActivityMainBinding binding = DataBindingUtil.setContentView(this,
                R.layout.activity_main);

        binding.setMainActivity(this);
        binding.setBusinessLogic(_businessLogic);
    }

    public void click() {
        _businessLogic.toggleVisibility();
    }
}

这是业务逻辑类:

public class BusinessLogic extends BaseObservable {

    private boolean visible;
    public BusinessLogic(){
        visible = true;
    }

    public void toggleVisibility(){
        visible = !visible;
        notifyPropertyChanged(BR.visible);
    }

    @Bindable
    public int getVisible(){
        return visible? View.VISIBLE:View.GONE;
    }
}

1 个答案:

答案 0 :(得分:0)

我稍微重构了你的代码,这是有效的

public void click() {
    _businessLogic.toggleVisibility(binding.toggleButton.isChecked());
}

BusinessLogic方法现在看起来像

public class BusinessLogic extends BaseObservable {

private boolean isToggleOn;

public BusinessLogic() {
    isToggleOn = false;
}

public void toggleVisibility(boolean toggleOn) {
    isToggleOn = toggleOn;
    notifyPropertyChanged(BR.toggleOn);
}

@Bindable
public boolean getToggleOn() {
    return isToggleOn;
}

然后在布局中,您可以根据businessLogic类的getToggleOn方法设置可见性

<Button
            android:id="@+id/add_key_button"
            android:layout_width="match_parent"
            android:visibility="@{businessLogic.toggleOn ? View.VISIBLE : View.GONE}"
            android:layout_height="wrap_content"
            android:text="Add key" />

请记住在布局中导入视图类

<data>

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

    <variable
        name="mainActivity"
        type="stackoverflow.MainActivity" />