Currently I'm developing a screen which will generate views dynamically based on a response and it involves a lot of cases where I should change the focus/position of scroll view to the selected view. So far I have managed to solve most of issues by using this method when view is not visible on screen:
fun View.requestViewOnScreen() {
val rect = Rect(0, 0, width, height)
requestRectangleOnScreen(rect, false)
}
The problem is that I have a hierarchy of nested views, and when the view is visible, it stays where it is when I call the above code, even if i try it with the code below (also with delay):
scrollView.smoothScrollTo(0, view.top)
Hierarchy is like:
ScrollView widget:
<androidx.core.widget.NestedScrollView
android:id="@+id/scrollView"
android:layout_width="0dp"
android:layout_height="0dp"
android:fillViewport="true"
android:visibility="gone"
app:layout_constraintBottom_toTopOf="@+id/stickyCtaView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:visibility="visible">
Any idea how can I adapt my code to make the NestedScrollView to scroll and position the requested view in center? Also I want to note that I have a reference to the view object itself. Thanks in advance!
view.top
returns the top position of a view relative to its direct parent, in your case it's the top position in relation to a greater parent is required for that you can use a method like the following to aggregate the top position of a view in relation to the scrollView and scroll to it
private fun ScrollView.scrollTo(target: View) {
var topPosition = 0
var view = target
while (view !== this) {
topPosition += view.top
view = view.parent as View
}
smoothScrollTo(0, topPosition)
}
so to scroll to any view you just call scrollView.scrollTo(view)