Vertical Scroll Arrangement scroll to top even though it is motion/fling

I am writing an extension to stop the fling/scroll of the vertical scroll view. I am able to scroll to the top with the below function only when the scroll view is not in the motion/fling/on going scroll.

How to do scrolltoTop even though the scoll view is scrilling.

sceinario:
I have a VSA(vertical scroll arrangement), and a lable. I have text which is large and need to scroll down to see the content. I will scroll down and come back and then one another content, the that will also scroll.

@SimpleFunction(description = "Scroll to the top of vertical scrol view")
public void ScrollTop(VerticalScrollArrangement verticalScrollArrangement) {
    final ScrollView scrollView = (ScrollView) verticalScrollArrangement.getView();
    if (scrollView == null) {
        return;
    }
    scrollView.scrollTo(0, 0);
}

I have seen ColinTree Scroll Hnadler/vknow360 code. But I want it simple, without registration and all.

I could rectify the problem and found the solution. Here is the full function:


import android.view.ViewTreeObserver;


@SimpleFunction(description = "Scroll to the top of Vertical Scroll Arangement(VSA) view")
public void ScrollToTop(VerticalScrollArrangement vsa) {
    final ScrollView scrollView = (ScrollView) vsa.getView();
    if (scrollView == null) {
        return;
    }
    scrollView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            scrollView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            scrollView.fullScroll(View.FOCUS_UP);
        }
    });
}
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.