如何以编程方式将 ScrollView 滚动到底部
我有一个无法解决的问题:在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您需要使用消息队列,否则它将无法工作。试试这个:
这对我有用。
You need to use the message queue or else it won't work. Try this:
This is what worked for me.
这实际上并不能回答你的问题。
但这是一种几乎做同样事情的替代方案。
不要滚动到屏幕底部,而是将焦点更改为位于屏幕底部的视图。
也就是说,将:替换
为:
确保指定视图(例如“页脚”)是可聚焦的。
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:
with:
Make sure you specify that the view, say 'Footer' is focusable.
这样就可以了
this will be ok
您尝试过
scroll.fullScroll(View.FOCUS_DOWN)
吗?Have you tried
scroll.fullScroll(View.FOCUS_DOWN)
?因为,我也使用隐藏键盘,所以下面延迟 200 毫秒的代码对我来说效果更好。
注意:我刚刚将上面示例中的 post() 替换为 postDelayed() :
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:
你也可以应用这个
you can apply this also
我希望它能工作
试试这个:
nsscroll.post(new Runnable() {
@覆盖
I Hope it Work
Try this:
nsscroll.post(new Runnable() {
@Override