使用SeekBar的onProgressChanged进行数据绑定不起作用

时间:2020-03-10 14:47:47

标签: android data-binding

我正在使用带有数据绑定的Seekbar,下面是我的代码

<data>

    <variable
        name="generatePasswordModel"
        type="android.account.model.GeneratePasswordModel" />

</data>

 <SeekBar
       android:id="@+id/sbPasswordLength"
       android:layout_width="0dp"
       android:layout_height="wrap_content"
       android:layout_marginLeft="10dp"
       android:layout_marginRight="10dp"
       android:layout_weight="1"
            android:onProgressChanged="@{generatePasswordModel.onValueChanged()}"
       android:max="20"
       android:min="4" />

并且Model类在下面

data class GeneratePasswordModel(
    private var seekValue: String,
    private var seekDisplay: String
) : BaseObservable() {


    var mSeekDisplay: String
        @Bindable get() = seekDisplay
        set(value) {
            seekDisplay = value
            notifyPropertyChanged(BR.mSeekDisplay)
        }

    fun onValueChanged(seekBar: SeekBar, progresValue: Int, fromUser: Boolean) {
        mSeekDisplay = progresValue.toString()
    }

}

但是在构建apk时出现以下错误

ANTLR Tool version 4.5.3 used for code generation does not match the current runtime version 4.7.1ANTLR Runtime version 4.5.3 used for parser compilation does not match the current runtime version 4.7.1ANTLR Tool version 4.5.3 used for code generation does not match the current runtime version 4.7.1ANTLR Runtime version 4.5.3 used for parser compilation does not match the current runtime version 4.7.1/Users/amitsiddhpura/Documents/.../app/build/generated/source/kapt/debug/.../android/DataBinderMapperImpl.java:18: error: cannot find symbol


                                                 import android.databinding.ActivityGeneratePasswordBindingImpl;
22:40:06.432 [ERROR] [system.err]                                         ^
22:40:06.432 [ERROR] [system.err]   symbol:   class ActivityGeneratePasswordBindingImpl

1 个答案:

答案 0 :(得分:2)

查看有关以下问题的最佳答案:Seekbar databinding error

您似乎需要在XML中指定参数:

android:onProgressChanged="@{(seekBar, value, fromUser)->generatePasswordModel.onValueChanged(seekBar, value, fromUser)}}

但是,由于您实际上并未在Kotlin代码中使用seekBarfromUser,因此可以像下面这样修改函数的定义:

fun onValueChanged(progresValue: Int) {
        mSeekDisplay = progresValue.toString()
    }

然后XML将如下所示:

android:onProgressChanged="@{(seekBar, value, fromUser)->generatePasswordModel.onValueChanged(value)}}
相关问题