简体   繁体   English

检查ScrollView是在底部,顶部还是侧面

[英]Check if ScrollView is on the bottom, top or sides

如何快速轻松地检查ScrollView或Horizo​​ntalScrollView是在其底部,顶部还是其一侧边缘?

As of API level 14 (Android 4.0 Ice Cream Sandwich), you can use View's methods canScrollHorizontally (int direction) and canScrollVertically (int direction) . 从API级别14开始(Android 4.0 Ice Cream Sandwich),您可以使用View的方法canScrollHorizo​​ntally(int方向)canScrollVertically(int方向)

Example usage: 用法示例:

//Check if ScrollView is at the bottom
//If not, scroll to the top
if(scrollView.canScrollVertically(1))
    scrollView.post(new Runnable() {

            @Override
            public void run() {
                scrollView.fullScroll(View.FOCUS_UP);
            }

        });

You can use this on ListView as well. 您也可以在ListView上使用它。 For example, when working with messaging app, you may want to check if user is reading older messages, so when he will receive a new one, he won't be disrupted by scrolling to the bottom, but rather keeping him at current position. 例如,在使用消息传递应用程序时,您可能希望检查用户是否正在阅读较旧的消息,因此当他将收到一条新消息时,不会滚动到底部,而是将其保持在当前位置,从而不会打扰到他。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM