À L'Aide De La Vue.getParent() en Vue du constructeur

Pourquoi ne pas ce travail?

private ScrollView findScrollParent(View v)
{

    if(v==null)
    {
        return null;
    }
    else if(v instanceof ScrollView)
    {
        return (ScrollView) v;
    }
    else
    {
        return findScrollParent((View)v.getParent());
    }
}

CustomView(Context context, AttributeSet as)
{
     super(context,as);
     ScrollView sv = findScrollParent(this);
}

C'est mon fichier xml qui est gonflé:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_height="fill_parent"
     android:layout_width="fill_parent">
<ScrollView
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

  <LinearLayout android:layout_height="wrap_content"
        android:layout_width="wrap_content">

       <com.myapp.CustomView android:layout_height="wrap_content"
               android:layout_width="wrap_content"/>

  </LinearLayout>

</ScrollView>
</RelativeLayout>

Je veux avoir une référence à la scrollView dans la Vue personnalisée. Mon findScrollParent méthode trouve le LinearLayout puis renvoie la valeur null. J'appelle cette méthode dans le View v'constructeur, c'est que le problème?

OriginalL'auteur you786 | 2011-08-17