如何检查滚动型是否可以滚动

2023-09-07 14:54:10 作者:偏执像风

我想知道,如何检查是否当前的滚动型是滚动?看来,有一个名为不是公共的方法 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.

要计算的原始高度,你可以使用View.getMeasuredHeight(). 由于滚动型是一个ViewGroup中,具有最大一个孩子,让这孩子与高度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();

使用一个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
        }
    }
});
相关推荐
 
精彩推荐
图片推荐