I'm trying to scroll a ScrollView in a loop and do something between the scrolls until it reaches the bottom. Ideally, I would like to scroll it "page-by-page" to minimize the number of scrollBy invocations and to avoid showing the same content twice.
I tried something like this:
var afterScrollY: Int
val scrollBy = scrollView.height
do {
val beforeScrollY = scrollView.scrollY
// This is why I need to scroll "page-by-page" instead of going straight to the bottom
doSomethingAtCurrentScrollPosition()
scrollView.scrollBy(0, scrollBy)
afterScrollY = scrollView.scrollY
} while (afterScrollY != beforeScrollY)
but it looks like scrolling by the ScrollView's height doesn't do exactly what I want.
My layout is very simple and doesn't contain any margins or paddings:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="@+id/scroll"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:fillViewport="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:id="@+id/frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:orientation="vertical" />
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

ScrollView's position to the views inside it. I thought maybe there is a better property thanheightto find the right number of pixels to scroll by. For context: my goal is to take a screenshot of the wholeScrollView's content.