I have added a scrollview and the subchilds inside the scrollview. At some point i need to scroll to a specific view.
<scrollview> 1. <linearlayout> <textview></textview> <textview></textview> </linearlayout> 2. <linearlayout> <textview></textview> <textview></textview> </linearlayout> 3. <linearlayout> <textview></textview> <textview></textview> </linearlayout> 4. <linearlayout> <textview></textview> <textview></textview> </linearlayout> 5. <linearlayout> <textview></textview> <textview></textview> </linearlayout> 6. <linearlayout> <textview></textview> <textview></textview> </linearlayout> 7. <linearlayout> <textview></textview> <textview></textview> </linearlayout> <button> </button> </scrollview>
The above layout was created dynamically. so i can't have the xml file posted here. Layout creation is completely dynamic. Even the number of child view inside the linearlayout may also vary.
So when i click on the button i need to get scrolled to a particular view say here when i click on button i need to scroll to the linear layout 4. I tried with the scrollTo method but it scrolls to the top of the scrollview.
Please provide some suggestions.
In order to place multiple views in the scroll view, one needs to make a view group(like LinearLayout) as a direct child and then we can define many views inside it. A ScrollView supports Vertical scrolling only, so in order to create a horizontally scrollable view, HorizontalScrollView is used.
If your list items are of equal height, you can use them to implement scrolling to a specific item by calling scrollTo({y:itemIndex * ROW_HEIGHT}) . Show activity on this post. I have published react-native-scroll-into-view, a library which enable "scrollIntoView" feature for React Native ScrollView.
NestedScrollView is just like ScrollView, but it supports acting as both a nested scrolling parent and child on both new and old versions of Android. It is enabled by default. NestedScrollView is used when there is a need for a scrolling view inside another scrolling view.
If child that we need to scroll to is not a direct child then above solutions don't work
I have used below solution in my project, sharing it may be helpful to others
/** * Used to scroll to the given view. * * @param scrollViewParent Parent ScrollView * @param view View to which we need to scroll. */ private void scrollToView(final ScrollView scrollViewParent, final View view) { // Get deepChild Offset Point childOffset = new Point(); getDeepChildOffset(scrollViewParent, view.getParent(), view, childOffset); // Scroll to child. scrollViewParent.smoothScrollTo(0, childOffset.y); } /** * Used to get deep child offset. * <p/> * 1. We need to scroll to child in scrollview, but the child may not the direct child to scrollview. * 2. So to get correct child position to scroll, we need to iterate through all of its parent views till the main parent. * * @param mainParent Main Top parent. * @param parent Parent. * @param child Child. * @param accumulatedOffset Accumulated Offset. */ private void getDeepChildOffset(final ViewGroup mainParent, final ViewParent parent, final View child, final Point accumulatedOffset) { ViewGroup parentGroup = (ViewGroup) parent; accumulatedOffset.x += child.getLeft(); accumulatedOffset.y += child.getTop(); if (parentGroup.equals(mainParent)) { return; } getDeepChildOffset(mainParent, parentGroup.getParent(), parentGroup, accumulatedOffset); }
This should do the trick:
View targetView = findViewById(R.id.DESIRED_VIEW_ID); targetView.getParent().requestChildFocus(targetView,targetView);
public void RequestChildFocus (View child, View focused)
child - The child of this ViewParent that wants focus. This view will contain the focused view. It is not necessarily the view that actually has focus.
focused - The view that is a descendant of child that actually has focus
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With