android - 使用数据绑定更新可绘制对象
问题描述
我有一个ImageView
我根据是否存在字符串来更改 alpha。
XML
<data>
<import type="android.text.TextUtils"/>
<variable name="model" type="myPackage.Model"/>
</data>
<TextView
android:text="@={model.stringAttribute}"/>
<ImageView
android:alpha='@{model.stringAttribute.eqals("") ? 0.5f : 1.0f }'
android:src="@drawable/thing"/>
在我的activity
:
binding.setModel(model);
这有效,ImageView
只是只有在重新启动活动时才会更新 alpha。我尝试在 @ 之后添加等号,但这不会编译。
模型
public class Model extends BaseObservable {
private String stringAttribute;
public void setStringAttribute(String s) {
stringAttribute = s
notifyPropertyChanged(BR.stringAttribute)
}
@Bindable
public String getStringAttribute() {
return stringAttribute;
}
}
并且澄清一下,TextView
当模型改变时更新。它只是在ImageView
Activity 重新启动之前不会更新的 alpha 版本。
解决方案
=
用于双向绑定,这对 imageView 没有意义(用户无法更改图像)。您没有显示myPackage.Model
. 确保模型扩展了 BaseObservable (还有其他方法可以做到这一点)并且 setter forstringAttribute
正在调用notifyChange()
or notifyPropertyChanged()
,例如:
public void setStringAttribute(String val) {
. . .
notifyPropertyChanged(BR.stringAttribute);
}
推荐阅读
- html - Firefox 在应用 CSS 之前呈现 HTML
- swift - 后台线程中的 SwiftUI 串行 DispatchQueue
- flutter - Flutter android 应用程序意外关闭并显示“与设备的连接丢失”消息?
- asp.net-web-api - Web API 错误 - “/api”应用程序中的服务器错误
- ios - Apple WebKit 已弃用?
- ruby-on-rails - Ruby on Rails 为下拉值创建方法“未初始化的常量”错误
- python - Python:以树状结构提取 MS Word 数据
- reactjs - 如何将反应静态 scss 实施到项目中?
- php - 搜索链接并添加后缀
- python-3.x - 基于另一个列字符串值的值创建新的数据框列