Why doesn’t this work?
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);
}
This is my xml file which is inflated:
<?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>
I want to have a reference to the scrollView in the custom View. My findScrollParent method finds the LinearLayout then returns null. I call this method in the View v‘s constructor, is that the problem?
If you are calling it in the view’s constructor then surely it will not have it’s parent assigned yet?
As far as I can see from skimming the source the parent attribute is not set until the view is added to the parent at which point it will do something like:
So if you are in the constructor for the view then my guess is it is too early and the view will be added after it is constructed. I was not 100% sure what combination of classes you were talking about / how you had them fitting together so may have looked at the wrong source.
EDIT
Just to clarify with pseudo code the process might look like this
EDIT 2 – @codelark ‘s suggestion