2017-09-12 3 views
1

J'ai une View et cette vue contient un Point. Je voudrais lier des données à ce point, mais autant que je comprenne je ne peux pas créer un attribut personnalisé dans attrs.xml avec un Point comme format.Liaison d'une vue personnalisée à un point

est la vue:

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; 
    } 
} 

Et ceci est 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); 
    } 
} 

Et maintenant, je veux lier MyView quelque chose comme ceci:

<MyView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:point="@{myObject.point}"/> 

et un fichier attrs.xml comme ceci:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="MyView"> 
     <attr name="point" format="Point" /> 
    </declare-styleable> 
</resources> 

Mais cela ne semble pas possible. Est-ce que quelqu'un connaît une solution?

Répondre

0

J'ai trouvé une solution pour ce problème. Au lieu de se lier à un Point, j'ai créé

<?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> 

Et puis-je lié les valeurs de myObject.getPoint().x-pointX et myObject.getPoint().y-pointY. Ce n'est pas la plus belle solution, mais ça marche.