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

发布于 2024-12-13 15:45:42 字数 428 浏览 5 评论 0原文

我有一个无法解决的问题:在 ScrollView 中我只有一个 线性布局。通过用户操作,我以编程方式添加 2 个 TextView 在此 LinearLayout 上,但默认情况下滚动保持在顶部。 由于我控制用户操作,我应该很容易滚动到 底部有类似的内容:

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

但实际上不是。因为在添加这两个新的之后立即 elements getBottom() 仍然返回前两个。我尝试过 调用 refreshDrawableState() 刷新状态,但我不起作用。

你知道我怎样才能得到 ScrollView 的实际底部吗 添加一些元素后?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

伪装你 2024-12-20 15:45:42

您需要使用消息队列,否则它将无法工作。试试这个:

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

这对我有用。

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.

转瞬即逝 2024-12-20 15:45:42

这实际上并不能回答你的问题。
但这是一种几乎做同样事情的替代方案。

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

也就是说,将:替换

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

为:

Footer.requestFocus();

确保指定视图(例如“页脚”)是可聚焦的。

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

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"
孤寂小茶 2024-12-20 15:45:42

这样就可以了

scrollView.post(new Runnable() {
    @Override

    public void run() {

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

    }
});

this will be ok

scrollView.post(new Runnable() {
    @Override

    public void run() {

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

    }
});
╄→承喏 2024-12-20 15:45:42

您尝试过scroll.fullScroll(View.FOCUS_DOWN)吗?

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

〃温暖了心ぐ 2024-12-20 15:45:42

因为,我也使用隐藏键盘,所以下面延迟 200 毫秒的代码对我来说效果更好。
注意:我刚刚将上面示例中的 post() 替换为 postDelayed() :

    final ScrollView scrollView_main = (ScrollView) findViewById(R.id.scrollView_main);
    scrollView_main.postDelayed(new Runnable() {
        @Override
        public void run() {
            scrollView_main.fullScroll(View.FOCUS_DOWN);
        }
    }, 200);

Since, I am also using hide keyboard, below code with a delay of 200 milli second worked better for me.
Note: I just replaced post() with postDelayed() from above examples:

    final ScrollView scrollView_main = (ScrollView) findViewById(R.id.scrollView_main);
    scrollView_main.postDelayed(new Runnable() {
        @Override
        public void run() {
            scrollView_main.fullScroll(View.FOCUS_DOWN);
        }
    }, 200);
淑女气质 2024-12-20 15:45:42

你也可以应用这个

 mScrollviewHomepage.setScrollY(View.FOCUS_DOWN);

you can apply this also

 mScrollviewHomepage.setScrollY(View.FOCUS_DOWN);
゛清羽墨安 2024-12-20 15:45:42

我希望它能工作

试试这个:

nsscroll.post(new Runnable() {
@覆盖

                    public void run() {

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

                    }
                });

I Hope it Work

Try this:

nsscroll.post(new Runnable() {
@Override

                    public void run() {

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

                    }
                });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文