如何以编程方式将 ScrollView 滚动到底部 [英] How to Programmatically Scroll a ScrollView to Bottom

查看:45
本文介绍了如何以编程方式将 ScrollView 滚动到底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

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

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

推荐答案

这实际上并没有回答您的问题.但它是一种替代方法,几乎​​可以做同样的事情.

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.

即替换:

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

与:

Footer.requestFocus();

确保您指定视图,例如页脚"可聚焦.

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

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

这篇关于如何以编程方式将 ScrollView 滚动到底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆