如何以编程方式将ScrollView滚动到底部

时间:2022-08-24 23:26:21

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中我只有一个LinearLayout。通过用户操作,我以编程方式在此LinearLayout上添加2个TextView,但默认情况下,滚动保持在顶部。由于我控制用户操作,我应该很容易滚动到底部,例如:

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.

但事实并非如此。因为在添加这两个新元素后立即getBottom()仍然返回前两个。我试图刷新调用refreshDrawableState()的状态,但我不起作用。

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

你知道如何在添加一些元素后获得ScrollView的实际底部吗?

5 个解决方案

#1


51  

This doesn't actually answer your question. But it's an alternative which pretty much does the same thing.

这实际上并没有回答你的问题。但它是一个替代品,几乎可以做同样的事情。

Instead of Scrolling to the bottom of the screen, change the focus to a view which is located at the bottom of the screen.

而不是滚动到屏幕底部,将焦点更改为位于屏幕底部的视图。

That is, Replace:

也就是说,替换:

scroll.scrollTo(0, scroll.getBottom());

with:

有:

Footer.requestFocus();

Make sure you specify that the view, say 'Footer' is focusable.

确保指定视图,说“页脚”是可聚焦的。

android:focusable="true"
android:focusableInTouchMode="true"

#2


74  

You need to do the scrolling on a separate thread 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.

这对我有用。

#3


16  

this will be ok

这没关系

scrollView.post(new Runnable() {
    @Override

    public void run() {

        scroll.scrollTo(0, scroll.getBottom());

    }
});

#4


3  

Have you tried scroll.fullScroll(View.FOCUS_DOWN)?

你试过scroll.fullScroll(View.FOCUS_DOWN)吗?

#5


0  

you can apply this also

你也可以申请

 mScrollviewHomepage.setScrollY(View.FOCUS_DOWN);

#1


51  

This doesn't actually answer your question. But it's an alternative which pretty much does the same thing.

这实际上并没有回答你的问题。但它是一个替代品,几乎可以做同样的事情。

Instead of Scrolling to the bottom of the screen, change the focus to a view which is located at the bottom of the screen.

而不是滚动到屏幕底部,将焦点更改为位于屏幕底部的视图。

That is, Replace:

也就是说,替换:

scroll.scrollTo(0, scroll.getBottom());

with:

有:

Footer.requestFocus();

Make sure you specify that the view, say 'Footer' is focusable.

确保指定视图,说“页脚”是可聚焦的。

android:focusable="true"
android:focusableInTouchMode="true"

#2


74  

You need to do the scrolling on a separate thread 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.

这对我有用。

#3


16  

this will be ok

这没关系

scrollView.post(new Runnable() {
    @Override

    public void run() {

        scroll.scrollTo(0, scroll.getBottom());

    }
});

#4


3  

Have you tried scroll.fullScroll(View.FOCUS_DOWN)?

你试过scroll.fullScroll(View.FOCUS_DOWN)吗?

#5


0  

you can apply this also

你也可以申请

 mScrollviewHomepage.setScrollY(View.FOCUS_DOWN);