Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
413 views
in Technique[技术] by (71.8m points)

java - How to Programmatically Scroll a ScrollView to Bottom

I've a problem I can't solve: inside a ScrollView I only have a LinearLayout. By a user action I'm programmatically adding 2 TextView on this LinearLayout, but by the default the scroll keeps on the top. Since I controll the user action, I should be easy to scroll to the bottom with something like:

ScrollView scroll = (ScrollView) this.findViewById(R.id.scroll);
scroll.scrollTo(0, scroll.getBottom());

But actually not. Because immediately after adding this two new elements getBottom() still returns the previous two. I tried to refresh the state invoking refreshDrawableState(), but I doesn't work.

Do you have any idea how could I get the actual bottom of a ScrollView after adding some elements?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to use the message queue or else it won't work. Try this:

scrollView.post(new Runnable() {
    @Override
    public void run() {
        scrollView.fullScroll(ScrollView.FOCUS_DOWN);
    }
});

This is what worked for me.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...