Android:ScrollView 强制到底部
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3080402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: ScrollView force to bottom
提问by Noah Seidman
I would like a ScrollView to start all the way at the bottom. Any methods?
我希望 ScrollView 一直从底部开始。有什么方法吗?
回答by hungson175
you should run the code inside the scroll.post like this:
你应该像这样运行 scroll.post 中的代码:
scroll.post(new Runnable() {
@Override
public void run() {
scroll.fullScroll(View.FOCUS_DOWN);
}
});
回答by CommonsWare
scroll.fullScroll(View.FOCUS_DOWN)
also should work.
scroll.fullScroll(View.FOCUS_DOWN)
也应该工作。
Put this in a scroll.Post(Runnable run)
把这个放在一个 scroll.Post(Runnable run)
Kotlin Code
科特林代码
scrollView.post {
scrollView.fullScroll(View.FOCUS_DOWN)
}
回答by peacepassion
scroll.fullScroll(View.FOCUS_DOWN)
will lead to the change of focus. That will bring some strange behavior when there are more than one focusable views, e.g two EditText. There is another way for this question.
scroll.fullScroll(View.FOCUS_DOWN)
会导致焦点的变化。当有多个可聚焦视图(例如两个 EditText)时,这会带来一些奇怪的行为。这个问题还有另一种方法。
View lastChild = scrollLayout.getChildAt(scrollLayout.getChildCount() - 1);
int bottom = lastChild.getBottom() + scrollLayout.getPaddingBottom();
int sy = scrollLayout.getScrollY();
int sh = scrollLayout.getHeight();
int delta = bottom - (sy + sh);
scrollLayout.smoothScrollBy(0, delta);
This works well.
这很好用。
Kotlin Extension
Kotlin 扩展
fun ScrollView.scrollToBottom() {
val lastChild = getChildAt(childCount - 1)
val bottom = lastChild.bottom + paddingBottom
val delta = bottom - (scrollY+ height)
smoothScrollBy(0, delta)
}
回答by Ajay Shrestha
Sometimes scrollView.post doesn't work
有时 scrollView.post 不起作用
scrollView.post(new Runnable() {
@Override
public void run() {
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
});
BUT if you use scrollView.postDelayed, it will definitely work
但是如果你使用scrollView.postDelayed,它肯定会起作用
scrollView.postDelayed(new Runnable() {
@Override
public void run() {
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
},1000);
回答by https
What worked best for me is
对我来说最有效的是
scroll_view.post(new Runnable() {
@Override
public void run() {
// This method works but animates the scrolling
// which looks weird on first load
// scroll_view.fullScroll(View.FOCUS_DOWN);
// This method works even better because there are no animations.
scroll_view.scrollTo(0, scroll_view.getBottom());
}
});
回答by ademar111190
I increment to work perfectly.
我递增以完美地工作。
private void sendScroll(){
final Handler handler = new Handler();
new Thread(new Runnable() {
@Override
public void run() {
try {Thread.sleep(100);} catch (InterruptedException e) {}
handler.post(new Runnable() {
@Override
public void run() {
scrollView.fullScroll(View.FOCUS_DOWN);
}
});
}
}).start();
}
Note
笔记
This answer is a workaround for really old versions of android. Today the postDelayed
has no more that bug and you should use it.
此答案是针对真正旧版 android 的解决方法。今天postDelayed
没有更多的错误,你应该使用它。
回答by Frank
When the view is not loaded yet, you cannot scroll. You can do it 'later' with a post or sleep call as above, but this is not very elegant.
当视图尚未加载时,您无法滚动。您可以像上面一样通过 post 或 sleep 调用“稍后”执行此操作,但这不是很优雅。
It is better to plan the scroll and do it on the next onLayout(). Example code here:
最好计划滚动并在下一个 onLayout() 上进行。示例代码在这里:
回答by Epsilon3
One thing to consider is what NOT to set. Make certain your child controls, especially EditText controls, do not have the RequestFocus property set. This may be one of the last interpreted properties on the layout and it will override gravity settings on its parents (the layout or ScrollView).
需要考虑的一件事是不要设置什么。确保您的子控件,尤其是 EditText 控件,没有设置 RequestFocus 属性。这可能是布局上最后解释的属性之一,它将覆盖其父项(布局或 ScrollView)上的重力设置。
回答by Yusuf Ya?ar
i tried that successful.
我试过成功。
scrollView.postDelayed(new Runnable() {
@Override
public void run() {
scrollView.smoothScrollTo(0, scrollView.getHeight());
}
}, 1000);
回答by Phan Van Linh
Here is some other ways to scroll to bottom
这是滚动到底部的其他一些方法
fun ScrollView.scrollToBottom() {
// use this for scroll immediately
scrollTo(0, this.getChildAt(0).height)
// or this for smooth scroll
//smoothScrollBy(0, this.getChildAt(0).height)
// or this for **very** smooth scroll
//ObjectAnimator.ofInt(this, "scrollY", this.getChildAt(0).height).setDuration(2000).start()
}
Using
使用
If you scrollview already laid out
如果你的滚动视图已经布局
my_scroll_view.scrollToBottom()
If your scrollview is not finish laid out (eg: you scroll to bottom in Activity onCreate
method ...)
如果您的滚动视图未完成布局(例如:您在 ActivityonCreate
方法中滚动到底部...)
my_scroll_view.post {
my_scroll_view.scrollToBottom()
}