Search code examples
androidandroid-layoutandroid-scrollview

Start the HorizontalScrollView by some scrollX


How can I specify a value that when visible the HorizontalScrollView will be already scrolled by the value?

I already tried the scrollTo method, but it's only work on a post layout Context, I'm not able to start the HorizontalScrollView on a specific position.

How can I do that?


Solution

  • I solved this way, any better solution is also welcome:

        public void scrollWhenAvailable(final int x) {
            if (x == getScrollX())
                return;
    
            scrollTo(x, 0);
    
            postDelayed(new Runnable() {
    
                @Override
                public void run() {
                    scrollWhenAvailable(x);
                }
            }, 10);
        }