【问题标题】:Binding a custom View to a Point将自定义视图绑定到点
【发布时间】:2018-02-21 08:18:50
【问题描述】:

我有一个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;
    }
}

这是我的对象:

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>

但这似乎是不可能的。有人知道解决方案吗?

【问题讨论】:

    标签: android view binding components


    【解决方案1】:

    我找到了解决此问题的方法。我没有绑定到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 的值绑定到pointXmyObject.getPoint().ypointY。这不是最漂亮的解决方案,但它确实有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-05
      • 1970-01-01
      • 2019-02-03
      • 2015-12-12
      相关资源
      最近更新 更多