-
-
Save gibson-khs/0fce6ed8724432bdac9f3c8885fdbc60 to your computer and use it in GitHub Desktop.
Scroll an android ScrollView to a non-direct child
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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