如何使用Android数据绑定动态更改视图可见性 [英] How to dynamically change view visibility using android data binding
问题描述
我正在尝试使用数据绑定实现一种简单的视图隐藏/显示.我有一个api调用,并且在api调用正在进行时,我必须显示一个进度栏.一旦获得响应,就必须取消此进度并显示数据.我试图使用数据绑定来动态更改progressbar的可见性.但是什么也没发生.仅第一次根据绑定变量设置进度栏可见性.当我更新绑定变量时,它不会动态更新.
I'm trying to achieve one simple view hide/show using data binding. I have an api call and I have to show one progressbar while the api call is going on. Once I get the response have to dismiss this progress and display the data. I tried to change the visibility of progressbar dynamically using data binding. But nothing happens. Only for the first time the progresbar visibility is set according to the binding variable. its not dynamically updating as I update the binding variable.
以下是我的示例代码
活动:
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);
}
}
ViewModel:
ViewModel:
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
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
and also in the app's build.gradle
dataBinding {
enabled = true
}
这里缺少什么?为什么不起作用?预先感谢..
What is missing over here? why doesn't it work? Thanks in advance..
推荐答案
您需要通知您属性值已更改,请尝试
you need to notify that your property value is changed, try this
public void setLoading(boolean loading) {
isLoading = loading;
notifyPropertyChanged(BR._all);
}
或者如果您只想通知单个变量,请使用BR.propertyName
or if you want to notify it just for single variable, use BR.propertyName
notifyPropertyChanged(BR.loading);
这篇关于如何使用Android数据绑定动态更改视图可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!