与 SeekBar 的 onProgressChanged 的数据绑定不起作用



我正在使用带有DataBinding的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" />

型号等级低于

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

查看这个问题的首要答案:Seekbar数据绑定错误

看来您需要在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)}}

最新更新