【问题标题】:Android data binding Two-way binding cannot resolve a setter for boolean propertyAndroid数据绑定双向绑定无法解析布尔属性的setter
【发布时间】:2020-02-21 10:18:12
【问题描述】:

我正在尝试根据在下面的 BaseObservable 类中定义的布尔值动态设置和更改背景可绘制对象,但我收到错误消息,指出它无法对“isFollowing”方法进行双向数据绑定。

错误是这样的:

表达式 'viewModel.isFollowing()' 不能倒置,所以它 不能用于双向绑定详细信息:双向绑定不能 解析布尔属性“isFollowing”的设置器

 private var isFollowing: Boolean = false

 @Bindable
 fun isFollowing(): Boolean {
      return isFollowing
 }
    
 @BindingAdapter("android:background")
 fun setIsFollowing(frameLayout: FrameLayout, isFollowing: Boolean) {

      if (this.isFollowing != isFollowing) {

           this.isFollowing = isFollowing
           if(this.isFollowing){
               frameLayout.background = frameLayout.context.getDrawable(R.drawable.following)
           }else{
               frameLayout.background = frameLayout.context.getDrawable(R.drawable.follow)
            }          
      }
  }
    

这是我的布局文件

  <FrameLayout
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:background="@={viewModel.isFollowing}"
       app:layout_constraintBottom_toBottomOf="parent"
       app:layout_constraintEnd_toEndOf="parent"
       app:layout_constraintRight_toRightOf="parent"
       app:layout_constraintTop_toTopOf="parent">

我按照建议尝试了这个,但没有任何乐趣:

 companion object {

     private var isFollow: Boolean = false

     @JvmStatic
     val isFollowing: Boolean
     @Bindable get() = isFollow

  }
 

【问题讨论】:

    标签: android data-binding


    【解决方案1】:

    您可以将方法更改为getter:

    val isFollowing: Boolean
        @Bindable get() = _isFollowing
    

    【讨论】:

    • 您好,我仍然收到相同的错误并将 isFollowing 转换为 @get:Bindable val isFollowing : Boolean get() = this.isFollow
    • 我编辑了我的帖子,很遗憾我现在无法在我的电脑上试用。
    • _isFollowing 从何而来?
    • 我将 isFollowing 从您的示例重命名为 _isFollowing,并使用 isFollowing 作为属性名称。
    • 好的,我尝试了上面的方法,仍然得到类似的错误:表达式 'com.catwalkcouture.theeditor.ui.home.model.HomeFeedTopLookViewModel.isFollowing()' 不能倒置,所以它不能用于双向绑定详细信息:双向绑定无法解析布尔属性“isFollowing”的设置器我什至将它包装在一个compaion对象中并添加了@JvmStatic
    猜你喜欢
    • 1970-01-01
    • 2016-08-25
    • 1970-01-01
    • 1970-01-01
    • 2016-06-23
    • 1970-01-01
    • 2014-02-21
    • 1970-01-01
    • 2015-12-25
    相关资源
    最近更新 更多