我想让ScrollView从底部开始,有什么方法吗?
我想让ScrollView从底部开始,有什么方法吗?
你应该像这样在scroll.post内运行代码:
scroll.post(new Runnable() {
@Override
public void run() {
scroll.fullScroll(View.FOCUS_DOWN);
}
});
scroll.scrollTo(0, scroll.getHeight());
跳转到底部,或者使用scroll.smoothScrollTo(0, scroll.getHeight());
进行更平滑的滚动。 - yuvalscrollView.post { scrollView.fullScroll(View.FOCUS_DOWN) }
的意思是滚动视图到底部。 - Albert Vila Calvoscroll.fullScroll(View.FOCUS_DOWN)
也应该可行。
将此放入 scroll.Post(Runnable run)
中。
Kotlin 代码
scrollView.post {
scrollView.fullScroll(View.FOCUS_DOWN)
}
scroll.fullScroll(View.FOCUS_DOWN)
会导致焦点的改变。当有多个可聚焦视图时,例如两个EditText,这将带来一些奇怪的行为。请查看我下面的答案。 - peacepassionscroll.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);
fun ScrollView.scrollToBottom() {
val lastChild = getChildAt(childCount - 1)
val bottom = lastChild.bottom + paddingBottom
val delta = bottom - (scrollY+ height)
smoothScrollBy(0, delta)
}
fun ScrollView.scrollToBottom() {
val lastChild = children.lastOrNull() ?: return
val bottom = lastChild.bottom + paddingBottom
val currentY = height + scrollY
val alreadyAtBottom = bottom <= currentY
if (!alreadyAtBottom) {
val delta = bottom - currentY
smoothScrollBy(0, delta)
} else {
// already at bottom, do nothing
}
}
scrollView.post(new Runnable() {
@Override
public void run() {
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
});
但是,如果您使用scrollView.postDelayed,它肯定会起作用。
scrollView.postDelayed(new Runnable() {
@Override
public void run() {
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
},1000);
对我最有效的是
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());
}
});
我希望我的递增函数能够完美工作。
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();
}
注意
这个答案是针对非常旧的Android版本的解决方法。今天,postDelayed
已经没有那个问题了,您应该使用它。
postDelayed
:) - ademar111190我尝试过这个并且成功了。
scrollView.postDelayed(new Runnable() {
@Override
public void run() {
scrollView.smoothScrollTo(0, scrollView.getHeight());
}
}, 1000);
这里有几种滚动到底部的方法:
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()
}
使用
如果您的滚动视图已经布局完成
my_scroll_view.scrollToBottom()
如果您的滚动视图没有完成布局(例如:在Activity的onCreate
方法中滚动到底部...)
my_scroll_view.post {
my_scroll_view.scrollToBottom()
}
当视图尚未加载时,您无法滚动。您可以像上面那样使用post或sleep调用来稍后执行,但这并不是非常优雅的做法。
更好的方法是计划滚动并在下一个onLayout()中执行。以下是示例代码:
有一件事需要考虑,那就是不要设置什么。请确保你的子控件,尤其是 EditText 控件,没有设置 RequestFocus 属性。这可能是布局中最后一个被解释的属性,它会覆盖其父级(布局或 ScrollView)上的重力设置。