如何使用 android 数据绑定动态更改视图可见性

How to dynamically change view visibility using android data binding

我正在尝试使用数据绑定实现一种简单的视图 hide/show。我有一个 api 调用,我必须在 api 调用进行时显示一个进度条。一旦我得到响应,就必须取消此进度并显示数据。我尝试使用数据绑定动态更改进度条的可见性。但是没有任何反应。仅在第一次根据绑定变量设置进度条可见性。当我更新绑定变量时,它不会动态更新。

以下是我的示例代码

Activity:

public class MainActivity extends AppCompatActivity {

private static final String TAG = MainActivity.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
    final ViewModel viewmodel = new ViewModel();
    binding.setViewmodel(viewmodel);

    findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            loadData(viewmodel);
        }
    });
}

private void loadData(final ViewModel viewmodel) {
    viewmodel.setLoading(true);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Log.d(TAG, "Loading finished");
            viewmodel.setLoading(false);
        }
    }, 2000);
}
}

视图模型:

import android.databinding.BaseObservable;
import android.databinding.Bindable;

public class ViewModel extends BaseObservable {

private boolean isLoading;
private String data;


@Bindable
public String getData() {
    return data;
}

public void setData(String data) {
    this.data = data;
}

@Bindable
public boolean isLoading() {
    return isLoading;
}

public void setLoading(boolean loading) {
    isLoading = loading;
 }
} 

activity_main.xml

<?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>
    <import type="android.view.View"/>
    <variable name="viewmodel" type="com.test.databindnig.ViewModel"/>
</data>
<LinearLayout
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    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="com.test.databindnig.MainActivity">

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:visibility="@{viewmodel.isLoading ? View.VISIBLE : View.GONE}"/>
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@{viewmodel.data}"
        android:visibility="@{viewmodel.isLoading ? View.GONE : View.VISIBLE}"/>


    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Load data"/>
</LinearLayout>
</layout>

以及应用的 build.gradle

dataBinding {
    enabled = true
}

这里缺少什么?为什么它不起作用?提前致谢..

您需要通知您的 属性 值已更改,试试这个

public void setLoading(boolean loading) {
    isLoading = loading;
    notifyPropertyChanged(BR._all);
}

或者如果你只想为单个变量通知它,使用BR.propertyName

notifyPropertyChanged(BR.loading);

我想澄清一下,如果您使用 notifyPropertyChanged(BR._all);,它会不必要地通知所有可观察对象。

您应该使用 notifyPropertyChanged(BR.field); 来通知仅更改的项目。

如果你想要model.setLoading(true)。它将折射该领域的相关观点。

请注意, 您的访问器应使用 @Bindable 属性进行注释。否则BRclass不会有你的字段名

@Bindable
public boolean isLoading() {
    return isLoading;
}

public void setLoading(boolean isLoading ) {
    this.isLoading = isLoading ;
    notifyPropertyChanged(BR.isLoading );
}

add <import type="android.view.View" /> on top of your XMl file result solution will be like following

<data>
    <import type="android.view.View" />
    <variable
        name="obj"
        type="com.you.modal.class" />
</data>


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