Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scroll to top of long ScrollView layout?

People also ask

How do I scroll to the top of ScrollView?

current. scrollTo with 0 to scroll the ScrollView to the top when it's pressed.

How do I scroll to top programmatically?

Use the View. scollTo(int x, int y) instead to scroll to the top.

How do I scroll to the top in react native?

The expected native behavior of scrollable components is to respond to events from navigation that will scroll to top when tapping on the active tab as you would expect from native tab bars. In order to achieve it we export useScrollToTop which accept ref to scrollable component (e,g. ScrollView or FlatList ).


Try

mainScrollView.fullScroll(ScrollView.FOCUS_UP);

it should work.


Had the same issue, probably some kind of bug.

Even the fullScroll(ScrollView.FOCUS_UP) from the other answer didn't work.
Only thing that worked for me was calling scroll_view.smoothScrollTo(0,0) right after the dialog is shown.


i had the same problem and this fixed it. Hope it helps you.

listView.setFocusable(false);

scrollViewObject.fullScroll(ScrollView.FOCUS_UP) this works fine, but only the problem with this line is that, when data is populating in scrollViewObject, has been called immediately. You have to wait for some milliseconds until data is populated. Try this code:

scrollViewObject.postDelayed(new Runnable() {
    @Override
    public void run() {
        scroll.fullScroll(ScrollView.FOCUS_UP);
    }
}, 600);

OR

 scrollViewObject.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        scrollViewObject.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        scrollViewObject.fullScroll(View.FOCUS_UP);
    }
});

The main problem to all these valid solutions is that you are trying to move up the scroll when it isn't drawn on screen yet.

You need to wait until scroll view is on screen, then move it to up with any of these solutions. This is better solution than a postdelay, because you don't mind about the delay.

    // Wait until my scrollView is ready
    scrollView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            // Ready, move up
            scrollView.fullScroll(View.FOCUS_UP);
        }
    });