Skip to content

Instantly share code, notes, and snippets.

@alecplumb
Created August 19, 2011 23:06
Show Gist options
  • Save alecplumb/1158254 to your computer and use it in GitHub Desktop.
Save alecplumb/1158254 to your computer and use it in GitHub Desktop.
Scroll an android ScrollView to a non-direct child
public class DeepScrollView extends ScrollView {
public ShrinkWatchScrollView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
public ShrinkWatchScrollView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ShrinkWatchScrollView(Context context) {
super(context);
}
public void scrollToDeepChild(View child) {
Point childOffset = new Point();
getDeepChildOffset(child.getParent(), child, childOffset);
Rect childRect = new Rect(childOffset.x, childOffset.y, childOffset.x + child.getWidth(), childOffset.y + child.getHeight());
int deltay = computeScrollDeltaToGetChildRectOnScreen(childRect);
smoothScrollBy(0, deltay);
}
private void getDeepChildOffset(ViewParent nextParent, View nextChild, Point accumulatedOffset) {
ViewGroup parent = (ViewGroup) nextParent;
accumulatedOffset.x += nextChild.getLeft();
accumulatedOffset.y += nextChild.getTop();
if(parent == this) {
return;
}
getDeepChildOffset(parent.getParent(), parent, accumulatedOffset);
}
}
@dajver
Copy link

dajver commented Jul 23, 2017

it doesn't work :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment