2017-09-12 5 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> 

を作成した。そして、私はpointYpointXからmyObject.getPoint().xmyObject.getPoint().yの値を結合しました。これは最も美しい解決策ではありませんが、機能します。

関連する問題