【发布时间】:2020-03-19 20:30:59
【问题描述】:
您好,我正在尝试进行双向绑定。我试图在 XML 文件中绑定的属性是。
android:visibility="@={chart.ShowProgress ? View.VISIBLE : View.GONE}"
在文件 ChartViewModel 文件中:
private int mShowProgress = View.GONE;
public int getShowProgress() {
if (mColor.colorId == mColorId) {
return mShowProgress;
}
mShowProgress = View.GONE;
return mShowProgress;
}
public void setShowProgress(@NonNull int isProgressVisible) {
this.mShowProgress = isProgressVisible;
}
但是,当我尝试构建时出现错误
The expression '((chartShowProgress) ? (android.view.View.VISIBLE) :
(android.view.View.GONE))' cannot be inverted, so it cannot be used in a two-way binding
Details: The condition of a ternary operator must be constant:
android.databinding.tool.writer.KCode@18e15570
请有人指出我做错了什么?谢谢!
【问题讨论】:
-
嗨,您是否在布局中导入了
View类? -
嗨@Zain,是的。我已经实现了一个 CharViewModel 类。我已更新我的问题以包含此信息。
-
对不起,我的意思是将这个
<import type="android.view.View"/>添加到<layout><data>标签中 -
对不起,我误会了。是的。我已经在侧面布局中导入了它。
-
我看不到@BindingAdapter注解..你能提供吗
标签: java android data-binding