Android:ScrollView强制到底部
我希望 ScrollView 从底部开始。有什么方法吗?
I would like a ScrollView to start all the way at the bottom. Any methods?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我希望 ScrollView 从底部开始。有什么方法吗?
I would like a ScrollView to start all the way at the bottom. Any methods?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(19)
你应该像这样运行scroll.post中的代码:
you should run the code inside the scroll.post like this:
scroll.fullScroll(View.FOCUS_DOWN)
也应该可以工作。将其放入
scroll.Post(Runnable run)
Kotlin 代码中
scroll.fullScroll(View.FOCUS_DOWN)
also should work.Put this in a
scroll.Post(Runnable run)
Kotlin Code
scroll.fullScroll(View.FOCUS_DOWN)
会导致焦点的改变。当有多个可聚焦视图(例如两个 EditText)时,这会带来一些奇怪的行为。这个问题还有另一种方式。这效果很好。
Kotlin 扩展
或者,如果您想进一步增强以在滚动之前检查它是否已经在底部,您可以执行以下操作:
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.This works well.
Kotlin Extension
Or if you want to further enhance to check whether it is already at the bottom before scrolling, you can do something like this:
有时scrollView.post不起作用
,但如果你使用scrollView.postDelayed,它肯定会起作用
Sometimes scrollView.post doesn't work
BUT if you use scrollView.postDelayed, it will definitely work
对我来说最有效的是
What worked best for me is
我增加工作完美。
注意
这个答案是针对旧版本 Android 的解决方法。今天
postDelayed
不再有这个错误,你应该使用它。I increment to work perfectly.
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.我试过了,成功了。
i tried that successful.
以下是滚动到底部的一些其他方法
使用
如果您的滚动视图已经布局
如果您的滚动视图尚未完成布局(例如:您在 Activity
onCreate
方法中滚动到底部。 ..)Here is some other ways to scroll to bottom
Using
If you scrollview already laid out
If your scrollview is not finish laid out (eg: you scroll to bottom in Activity
onCreate
method ...)当视图尚未加载时,您无法滚动。您可以“稍后”通过上面的 post 或 sleep 调用来完成此操作,但这不是很优雅。
最好规划滚动并在下一个 onLayout() 上执行。示例代码如下:
https://stackoverflow.com/a/10209457/1310343
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.
It is better to plan the scroll and do it on the next onLayout(). Example code here:
https://stackoverflow.com/a/10209457/1310343
需要考虑的一件事是不应该设置的内容。确保您的子控件(尤其是 EditText 控件)没有设置 RequestFocus 属性。这可能是布局上最后解释的属性之一,它将覆盖其父级(布局或 ScrollView)上的重力设置。
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 获得焦点后立即向下滚动。然而,接受的答案将使 ET 也立即失去焦点(我认为是 ScrollView)。
我的解决方法如下:
Not exactly the answer to the question, but I needed to scroll down as soon as an EditText got the focus. However the accepted answer would make the ET also lose focus right away (to the ScrollView I assume).
My workaround was the following:
我实际上发现调用 fullScroll 两次可以解决问题:
它可能与执行第一次(不成功)滚动后立即激活 post() 方法有关。我认为这种行为发生在 myScrollView 上的任何先前方法调用之后,因此您可以尝试用任何可能与您相关的其他方法替换第一个 fullScroll() 方法。
I actually found that calling fullScroll twice does the trick:
It may have something to do with the activation of the post() method right after performing the first (unsuccessful) scroll. I think this behavior occurs after any previous method call on myScrollView, so you can try replacing the first fullScroll() method by anything else that may be relevant to you.
使用 Kotlin 协程还有另一种很酷的方法。与具有可运行(post/postDelayed)的处理程序相比,使用协程的优点是它不会启动昂贵的线程来执行延迟操作。
将协程的 HandlerContext 指定为 UI 非常重要,否则可能无法从 UI 线程调用延迟操作。
Using there is another cool way to do this with Kotlin coroutines. The advantage of using a coroutine opposed to a Handler with a runnable (post/postDelayed) is that it does not fire up an expensive thread to execute a delayed action.
It is important to specify the coroutine's HandlerContext as UI otherwise the delayed action might not be called from the UI thread.
在这种情况下,仅使用 scroll.scrollTo(0, sc.getBottom()) 不起作用,请使用 scroll.post
示例:
In those case were using just scroll.scrollTo(0, sc.getBottom()) don't work, use it using scroll.post
Example:
即使封装在
.post()
中,scroll.fullScroll(View.FOCUS_DOWN)
也可能无法工作,一个可能的原因是视图未布局。在这种情况下 View.doOnLayout()可能是更好的选择:或者,为勇敢的灵魂提供更详细的选择: https ://chris.banes.dev/2019/12/03/suspending-views/
One possible reason of why
scroll.fullScroll(View.FOCUS_DOWN)
might not work even wrapped in.post()
is that the view is not laid out. In this case View.doOnLayout() could be a better option:Or, something more elaborated for the brave souls: https://chris.banes.dev/2019/12/03/suspending-views/
所有答案的组合对我来说很有效:
扩展函数PostDelayed
扩展函数measureScrollHeight
扩展函数ScrolltoBottom
请注意,最小延迟应至少为325毫秒,否则滚动将出现错误(不会滚动到整个底部)。当前高度与底部之间的增量越大,延迟时间就应该越大。
A combination of all answers did the trick for me:
Extension Function PostDelayed
Extension Function measureScrollHeight
Extension Function ScrolltoBottom
Be aware that the minimum delay should be at least 325ms or the scrolling will be buggy (not scrolling to the entire bottom). The larger your delta between the current height and the bottom is, the larger should be the delayed time.
这里有人说scrollView.post不起作用。
如果您不想使用scrollView.postDelayed,另一个选择是使用侦听器。这是我在另一个用例中所做的:
您可以通过这种方式将其添加到您的视图中:
Some people here said that scrollView.post didn't work.
If you don't want to use scrollView.postDelayed, another option is to use a listener. Here is what I did in another use case :
You can add it to your view this way :
如果您的最低 SDK 为 29 或更高,您可以使用以下命令:
If your minimum SDK is 29 or upper you could use this:
这立即生效。毫不拖延。
This works instantly. Without delay.