简体   繁体   English

当数据绑定android中的数据发生变化时如何更新UI?

[英]How to update UI when data changes in data-binding android?

I have an activity that shows one text-view in that:我有一个活动,其中显示一个文本视图:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
        <variable
            name="Users"
            type="com.example.mvvm.model.UserInfoModel" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".view.MainActivity">

        <TextView
            android:id="@+id/tv_main"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="50dp"
            android:text="@{Users.name,default = hello}"/>

    </LinearLayout>

</layout>

And my UserInfoModel class:还有我的 UserInfoModel 类:

public class UserInfoModel {
private String name;

public UserInfoModel() {
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}
}

And in my Activity:在我的活动中:

public class MainActivity extends AppCompatActivity {

ActivityMainBinding binding;
UserInfoModel users;
TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
    
    textView = binding.tvMain;

    users = new UserInfoModel();

    users.setName("Stack");

    viewAction();

    binding.setLifecycleOwner(this);
    binding.setUsers(users);
}
}

My Problem is, When I Change name in viewAction method like this:我的问题是,当我在viewAction方法中更改名称时,如下所示:

 private void viewAction() {
    textView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            users.setName("Change Text");
        }
    });
}

My UI doesn't change and text-view still showing Stack .我的 UI 没有改变,文本视图仍然显示Stack I try to use binding.notifyPropertyChanged(id) but it doesn't affect.我尝试使用binding.notifyPropertyChanged(id)但它不影响。 Where is my mistake?我的错误在哪里? How can I change the data?如何更改数据?

thanks for your attention.感谢您的关注。

Actually the problem is that generated binding class doesn't know when property is changed.实际上问题是生成的绑定类不知道何时更改属性。 You should notify it.你应该通知它。 First, your viewmodel can be inherited from BaseObservable class, then you have to add @Bindable annotation to getters in your viewmodel and call notifyPropertyChanged inside setters with appropriate id from BR class(it's going to be generated for you by databinding library, just add @Bindable annotation. for instance notifyPropertyChanged(BR.name) ).首先,您的视图模型可以从 BaseObservable 类继承,然后您必须在视图模型中的 getter 中添加 @Bindable 注释,并在 setter 中使用来自 BR 类的适当 id 调用 notifyPropertyChanged(它将由数据绑定库为您生成,只需添加 @可绑定注释。例如 notifyPropertyChanged(BR.name) )。 Also you should pay attention how you store "binding" variable in MainActivity class.It can lead to memory leaks.您还应该注意如何在 MainActivity 类中存储“绑定”变量。它可能导致内存泄漏。 You can investigate that issue.你可以调查那个问题。 See library like databindingPropertyDelegate.请参阅像 databindingPropertyDelegate 这样的库。

Something like this像这样的东西

public class UserInfoModel extends BaseObservable{
     @Bindable
     public String getName() {
           return name; 
     } 
     public void setName(String name) {
           this.name = name; 
           notifyPropertyChanged(BR.name)
     }
}

You can just make your fields observable in the models.你可以让你的字段在模型中可见。

public class UserInfoModel {
private ObservableField<String> name;

public UserInfoModel() {
 this.name = new ObservableField<>();
}

public ObservableField<String> getName() {
return name;
}

public void setName(String name) {
this.name.set(name);
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM