我有View
,此视图包含Point
。我想将数据绑定到这一点,但据我了解,我无法在attrs.xml
中使用Point
作为格式创建自定义属性。
这是观点:
public class MyView extends android.support.v7.widget.AppCompatImageView {
private Point point;
public MyView(Context context) {
super(context);
}
public Point getPoint() {
return point;
}
public void setPoint(Point point) {
this.point = point;
}
}
这是MyObject:
public class MyObject extends BaseObservable {
private Point point;
@Bindable
public Point getPoint() {
return point;
}
public void setPoint(Point point) {
this.point = point;
notifyPropertyChanged(BR.point);
}
}
现在我想绑定像这样的MyView:
<MyView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:point="@{myObject.point}"/>
和attrs.xml
这样的文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyView">
<attr name="point" format="Point" />
</declare-styleable>
</resources>
但这似乎不可能。有人知道解决方案吗?
答案 0 :(得分:0)
我找到了解决这个问题的方法。我创建了
,而不是绑定到Point
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyView">
<attr name="pointX" format="integer" />
<attr name="pointY" format="integer" />
</declare-styleable>
</resources>
然后我将myObject.getPoint().x
的值绑定到pointX
和myObject.getPoint().y
到pointY
。这不是最美丽的解决方案,但它确实有效。