用于recyclerView的DataBinding注释@BindingAdapter不会触发

时间:2016-04-20 13:37:55

标签: android data-binding android-databinding

我疯狂地测试双向数据绑定RecyclerView。

我阅读了很多文章,并试图通过mvvm模式实现它。

所以我有:

  • 模型

    public class User {
    
       private String firstName;
       private String lastName;
    
    
       public User(String firstName, String lastName) {
           this.firstName = firstName;
           this.lastName = lastName;
       }
    
        public String getFirstName() {
           return firstName;
       }
    
       public void setFirstName(String firstName) {
           this.firstName = firstName;
       }
    
       public String getLastName() {
           return lastName;
       }
    
       public void setLastName(String lastName) {
           this.lastName = lastName;
       } 
    }
    
  • 视图模型

    public class UsersViewModel extends BaseObservable {
    
       @Bindable
       public ObservableArrayList<UserViewModel> usersTwoWay;
    
       public UsersViewModel() {
          usersTwoWay = new ObservableArrayList<>();
       }
    
       public void addUser(String firstName, String lastName) {
          usersTwoWay.add(new UserViewModel(new User(firstName, lastName)));
       }
    }
    

在XML文件中当然是关于数据绑定的头部中正确的初始化代码,我设置了RecyclerView的属性

   app:items="@{usersViewModel.usersTwoWay}"

应传递给数据下方的BindingAdpter以显示适配器。

最后是BindingAdapter

   @BindingAdapter("items")
   public static void setItems(RecyclerView recyclerView, ObservableArrayList<UserViewModel> items) {

      // Some code
   }

哪里错了? 为什么&#34; @ BindingAdpter&#34;的代码?不执行!

0 个答案:

没有答案