The problem is with this statement
app:visibility="@{viewModel.searchMode ? View.VISIBLE : View.GONE}"
it evaluates and pass View.VISIBLE
or View.GONE
to the binding adapter method,But
@BindingAdapter("visibility")
fun setVisibility(target: View, visible: Boolean)
As your method signature says it expects a boolen but evaluation results in int
i.e. either View.VISIBLE
or View.GONE
.
The issue can be solved by removing the evaluation and passing the boolean directly.
app:visibility="@{viewModel.searchMode}"
I assument viewModel.searchMode
is a boolean variable.
Lets you create a kotlin file Named BindingAdapters.kt
Paste this method directly there
@BindingAdapter("visibility")
fun setVisibility(target: View, visible: Boolean) {
target.visibility = if (visible) View.VISIBLE else View.GONE
}
else lets say you have a class BindingAdapters
in a file BindingAdapters.kt
class BindingAdapters{
companion object{
@BindingAdapter("visibility")
@JvmStatic// it is important
fun setVisibility(target: View, visible: Boolean) {
target.visibility = if (visible) View.VISIBLE else View.GONE
}
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…