ScrollView scrollTo not working

Same problem as ScrollView.scrollTo not working? Saving ScrollView position during rotation

I am dynamically adding elements to scrollView in onCreate. I will try the following after all the elements have been added:

// no effect ScrollView mainScroll = (ScrollView) findViewById(R.id.average_scroll_mainScroll); // no effect ScrollView mainScroll = (ScrollView) findViewById(R.id.average_scroll_mainScroll); mainScroll.post(new Runnable() { public void run(){ ScrollView mainScroll = (ScrollView) findViewById(R.id.average_scroll_mainScroll); mainScroll.scrollTo(0, 0); } }); // works like a charm ScrollView mainScroll = (ScrollView) findViewById(R.id.average_scroll_mainScroll); mainScroll.postDelayed(new Runnable() { public void run(){ ScrollView mainScroll = (ScrollView) findViewById(R.id.average_scroll_mainScroll); mainScroll.scrollTo(0, 0); } }, 30); 

I came to the conclusion that there is some kind of event like "DOM-ready"? Are there any callbacks?

+4
source share
3 answers

You do not need to extend ScrollView and create your own according to David Daudelin's answer for this question .

Get ViewTreeObserver ScrollView and add OnGlobalLayoutListener to ViewTreeObserver . Then call the ScrollView.scrollTo(x,y) method from the onGlobalLayout() OnGlobalLayoutListener . The code:

  ScrollView mainScroll = (ScrollView) findViewById(R.id.average_scroll_mainScroll); ViewTreeObserver vto = scrollView.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { public void onGlobalLayout() { mainScroll.scrollTo(0, 0); } }); 
+14
source

When making some changes to the ScrollView, it takes some time for it to replicate the layout changes to the display list and notify ScrollView that it is indeed allowed to scroll to the position.

I managed to get it to work by extending the ScrollView with a ScrollView of my own and adding a method that adds OnGlobalLayoutListener (as suggested by MH) as needed and scrolls it later. This is a bit more automated than applying it for every case you need (but then you need to use the new ScrollView ). Anyway, this is the corresponding code:

 public class ZScrollView extends ScrollView { // Properties private int desiredScrollX = -1; private int desiredScrollY = -1; private OnGlobalLayoutListener gol; // ================================================================================================================ // CONSTRUCTOR ---------------------------------------------------------------------------------------------------- public ZScrollView(Context __context) { super(__context); } public ZScrollView(Context __context, AttributeSet __attrs) { super(__context, __attrs); } public ZScrollView(Context __context, AttributeSet __attrs, int __defStyle) { super(__context, __attrs, __defStyle); } // ================================================================================================================ // PUBLIC INTERFACE ----------------------------------------------------------------------------------------------- public void scrollToWithGuarantees(int __x, int __y) { // REALLY Scrolls to a position // When adding items to a scrollView, you can't immediately scroll to it - it takes a while // for the new addition to cycle back and update the scrollView max scroll... so we have // to wait and re-set as necessary scrollTo(__x, __y); desiredScrollX = -1; desiredScrollY = -1; if (getScrollX() != __x || getScrollY() != __y) { // Didn't scroll properly: will create an event to try scrolling again later if (getScrollX() != __x) desiredScrollX = __x; if (getScrollY() != __y) desiredScrollY = __y; if (gol == null) { gol = new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { int nx = desiredScrollX == -1 ? getScrollX() : desiredScrollX; int ny = desiredScrollY == -1 ? getScrollY() : desiredScrollY; desiredScrollX = -1; desiredScrollY = -1; scrollTo(nx, ny); } }; getViewTreeObserver().addOnGlobalLayoutListener(gol); } } } } 

Very useful for me, since I wanted to scroll to a specific view inside the ScrollView immediately after adding it.

+1
source

This works for me, replace scrollView.scrollTo() with

  scrollView.post(new Runnable() { @Override public void run() { scrollView.scrollTo(0, ScrollViewYPosition);//0 is x position } }); 

Vasily Kabunov helped me answer in

ScrollView.scrollTo not working? Saving ScrollView position during rotation

0
source

All Articles