开发者

Start the HorizontalScrollView by some scrollX

开发者 https://www.devze.com 2023-04-04 03:03 出处:网络
How can I specify a value that when visible the HorizontalScrollView will be already scrolled by the value?

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?


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);
    }


you will probably have to use scrollTo anyway, since it does exactly what you want, albeit you may need to call it at the right time. (in some onPostSomething, i guess)


I believe that it is unecessary to to a postDelayed, a simple post is sufficient since it will be executed after all the view dimensions have been calculated, you can even add this in onCreate(..).

scrollView.post(new Runnable() {

    @Override
    public void run() {
        // TODO Auto-generated method stub
        scrollView.setScrollX(x);
    }
});

Also note that setScrollX is probably what you want to use if your SDK level is high enough.

0

精彩评论

暂无评论...
验证码 换一张
取 消