23

I have two buttons that switching to the next list of events and the previous one.

When I go to next\previous event, scrolls remains somewhere below. But I need to "rewind" it up.

I'm trying:

scrollViewEventDetails.pageScroll(ScrollView.FOCUS_UP);

and:

scrollViewEventDetails.scrollTo(0, 0); 

but it doesn't work. Please, help!

ihrupin
  • 6,932
  • 2
  • 31
  • 47

5 Answers5

35

You shoud write next:

scrollViewEventDetails.fullScroll(View.FOCUS_UP);//if you move at the end of the scroll

scrollViewEventDetails.pageScroll(View.FOCUS_UP);//if you move at the middle of the scroll
dimas
  • 387
  • 3
  • 9
23

This did not work for me:

scrollViewEventDetails.fullScroll(View.FOCUS_UP); //if you move at the end of the scroll

This worked for me:

scroll_view.smoothScrollTo(0,0);
dystopiandev
  • 134
  • 1
  • 6
madhu527
  • 4,644
  • 1
  • 28
  • 29
14

scrollview.pageScroll(View.FOCUS_UP); is more consistent and worked for me. :)

Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
Ash
  • 1,391
  • 15
  • 20
2

The timing of these calls is crucial. getViewTreeObserver().addOnGlobalLayoutListener didn't work for me but using the onWindowFocusChanged callback in my activity did:

@Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);

        if (hasFocus) {
            mScrollView.fullScroll(View.FOCUS_UP);
        }
    }
Damian
  • 8,062
  • 4
  • 42
  • 43
0

I know this is an old post but there is a reason the code will work some places and not others. It will get interrupted by the main thread if called in the wrong place, especially if the ScrollView must scroll a large distance. Therefore put your call in its own thread. Fixed.

(new Thread(new Runnable(){
   public void run(){
      mScrollView.fullScroll(View.FOCUS_UP);
   }
})).start();