2017-09-12 213 views
1

我有一個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

我找到了解決此問題的方法。相反,結合一個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。這不是最美麗的解決方案,但它的工作原理。

相關問題