【发布时间】:2021-03-10 20:10:23
【问题描述】:
其他问题已通过将kotlin-kapt 插件添加到gradle 文件中得到解决,但我的问题似乎并没有就此消失。我正在尝试设置进度条的可见性,以便用户知道应用程序何时在后台运行。我为此使用数据绑定,但出现编译错误:
找不到接受参数类型'androidx.databinding.ObservableField
我认为字符串或整数值在那里也不起作用。至少对于 boolean 方法,它不会抱怨 setter,只会抱怨 getter。尽管我将数据绑定与其他视图一起使用并且效果很好。 这就是我目前在 HomeViewModel 中实现我的解决方案的方式:
@Bindable
var barProgress = ObservableField<Boolean>()
fun doSomething(){
this.barProgress.set(true)
}
@BindingAdapter("android:visibility")
fun setVisibility(view: View, visible: Boolean) {
view.visibility = if (visible) View.INVISIBLE else View.VISIBLE
}
在布局文件中:
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginLeft="15dp"
app:layout_constraintStart_toEndOf="@+id/tag_button"
app:layout_constraintTop_toBottomOf="@+id/editTextTextMultiLine"
android:visibility="@={myViewModel.barProgress}" //--> this is where it complains />
所以在我的 HomeViewModel 中,我应该能够将 barProgress 设置为 true 或 false。
我的build.gradle:
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
}
知道我应该怎么做吗?
【问题讨论】:
标签: android kotlin view build.gradle kapt