【问题标题】:Data Binding Compiler cannot find custom binding adapter provided数据绑定编译器找不到提供的自定义绑定适配器
【发布时间】:2019-11-12 06:34:11
【问题描述】:

我在Fragment 中静态地提供了一个绑定适配器,它基本上将我的按钮外观从“停止”更改为“播放”,反之亦然。

companion object {
        @BindingAdapter("playState")
        fun Button.setPlayState(item: UIState) {
            item.let {
                if (it.isPlaying) {
                    setText("Stop")
                    setBackgroundColor(ContextCompat.getColor(context, R.color.colorStop))
                } else {
                    setText("Play")
                    setBackgroundColor(ContextCompat.getColor(context, R.color.colorPlay))
                }
            }
        }
    }

这是我的布局文件。我已经为它提供了一个数据类。

<?xml version="1.0" encoding="utf-8"?>
<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>
        <!-- stuff here -->
        <variable
            name="viewmodel"
            type="com.mypackage.ui.ViewModel"/>
        <variable
            name="uistate"
            type="com.mypackage.ui.UIState" />
    </data>
    <!-- layout, buttons, and more stuff here. Just pay attention to this following button -->
     <Button
            android:id="@+id/play_button"
            android:layout_width="150sp"
            android:layout_height="75sp"
            android:layout_marginTop="20sp"
            android:onClick="@{() -> viewmodel.onPlayClicked()}"
            android:text="@string/play_button"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.498"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/minus_layout"
            app:layout_constraintVertical_bias="0.026"
            app:playState="@{uistate}"/>


</layout>

UIState 本身是不言自明的。

data class UIState(var isPlaying: Boolean)

() -&gt; viewmodel.onPlayClicked()UIState 处翻转Boolean

编译后,Data Binding Compiler 抛出此错误:

Cannot find a setter for <android.widget.Button app:playState> 
that accepts parameter type 'com.mypackage.ui.UIState'

我试过了:

  1. 通过删除 .gradle 文件夹重建项目
  2. 正在寻找答案herehere
  3. 移除了扩展函数中的@JvmStatic注解
  4. 将扩展函数移至顶层,而不是 Fragment 的伴随对象。

【问题讨论】:

    标签: android kotlin android-databinding


    【解决方案1】:

    我认为你错过了在你的 gradle 中添加 kotlin 插件

    apply plugin: 'kotlin-kapt'
    

    【讨论】:

    • 是的。谢谢!
    【解决方案2】:

    您不必使用@JvmStatic,因为您使用的是 Kotlin 扩展功能。

    【讨论】:

      【解决方案3】:

      您需要将视图引用作为参数添加到您的 BindingAdapter 方法中。

       @BindingAdapter("playState")
          fun setPlayState(button:Button,item: UIState) {
              //do your work here
          }
      

      【讨论】:

      • 这个和他用的一样。他只是使用了扩展功能的方式。有什么区别?
      【解决方案4】:

      你的命名空间

      xmlns:app="http://schemas.android.com/apk/res-auto"

      对于自定义绑定适配器是错误的。请使用命名空间

      xmlns:app="http://schemas.android.com/tools"

      因为app:playState 不在您指定的命名空间中,所以它不能正常工作

      【讨论】:

      • 我在 tools 命名空间中使用了“schemas.android.com/tools”。我也尝试过您的解决方案,但没有任何改变。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-16
      • 2019-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多