Skip to content

Instantly share code, notes, and snippets.

@gibson-khs
Forked from alecplumb/DeepScrollView.java
Created June 20, 2017 12:57
Show Gist options
  • Save gibson-khs/0fce6ed8724432bdac9f3c8885fdbc60 to your computer and use it in GitHub Desktop.
Save gibson-khs/0fce6ed8724432bdac9f3c8885fdbc60 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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment