如何检查滚动型是否可以滚动 [英] How to check whether ScrollView is scrollable

查看:148
本文介绍了如何检查滚动型是否可以滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,如何检查是否当前的滚动型是滚动?看来,有一个名为不是公共的方法 canScroll isScrollable 滚动型

I was wondering, how to check whether the current ScrollView is scrollable? It seems that, there isn't public method called canScroll or isScrollable in ScrollView.

滚动:您可以移动的ViewGroup的滚动型内上下,当你将它向上和向下的滚动条将可见。所以,如果在只有很少的行滚动型,他们可以装进一个画面,滚动型不可滚动然后。

Scrollable : You can move the ViewGroup inside the ScrollView up and down, and the scroll bar will be visible when you move it up and down. So, if there is only little rows in the ScrollView, and they can fit inside single screen, ScrollView is not scrollable then.

推荐答案

您可以做一些小的数学计算的原始高度和内容高度的意见。如果这个高度差为< 0的观点是滚动的。

You can do some little math to calculate the views raw height and the height of the content. If the difference of this heights is < 0 the view is scrollable.

要计算的原始高度,你可以使用<一个href="http://developer.android.com/reference/android/view/View.html#getMeasuredHeight%28%29">View.getMeasuredHeight(). 由于滚动型是一个ViewGroup中,具有最大一个孩子,让这孩子与高度<一href="http://developer.android.com/reference/android/view/ViewGroup.html#getChildAt%28int%29">ViewGroup.getChildAt(0).getHeight();

To calculate the raw height you can use View.getMeasuredHeight(). Because ScrollView is a ViewGroup and has max one child, get the height of that child with ViewGroup.getChildAt(0).getHeight();

使用一个<一个href="http://developer.android.com/reference/android/view/ViewTreeObserver.html">ViewTreeObserver得到的高度,因为它会被调用的时刻的布局/视图是变化的可见性,否则的高度可以是0

Use a ViewTreeObserver to get the heights, because it will be called at the moment the layout / view is changing the visibility, otherwise the heights could be 0.

ScrollView scrollView = (ScrollView)findViewById(R.id...);
ViewTreeObserver observer = scrollView.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        int viewHeight = scrollView.getMeasuredHeight();
        int contentHeight = scrollView.getChildAt(0).getHeight();
        if(viewHeight - contentHeight < 0) {
            // scrollable
        }
    }
});

这篇关于如何检查滚动型是否可以滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆