【问题标题】:check if an android scrollview can scroll检查android滚动视图是否可以滚动
【发布时间】:2014-07-28 08:14:10
【问题描述】:

你知道是否有可能知道Android Widget ScrollView 是否可以滚动? 如果它有足够的空间,则不需要滚动,但只要尺寸超过最大值,小部件就可以滚动。

我在参考中没有看到可以提供此信息的方法。 也许可以对滚动视图内的线性布局大小做一些事情?

【问题讨论】:

标签: android scrollview


【解决方案1】:

我使用了以下受https://stackoverflow.com/a/18574328/3439686 启发的代码,它可以工作!

ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView);
int childHeight = ((LinearLayout)findViewById(R.id.scrollContent)).getHeight();
boolean isScrollable = scrollView.getHeight() < childHeight + scrollView.getPaddingTop() + scrollView.getPaddingBottom();

【讨论】:

  • 附加注释:它在创建和显示视图时起作用。不能在 onCreate 中使用(例如:getHeight 将返回 0,对于 match_parent)
  • 答案是正确的,但该方法应该在视图创建后调用。在这里查看:stackoverflow.com/questions/19503573/…
【解决方案2】:

感谢:@johanvs 和 https://stackoverflow.com/a/18574328/3439686

private boolean canScroll(HorizontalScrollView horizontalScrollView) {
    View child = (View) horizontalScrollView.getChildAt(0);
    if (child != null) {
        int childWidth = (child).getWidth();
        return horizontalScrollView.getWidth() < childWidth + horizontalScrollView.getPaddingLeft() + horizontalScrollView.getPaddingRight();
    }
    return false;

}

private boolean canScroll(ScrollView scrollView) {
    View child = (View) scrollView.getChildAt(0);
    if (child != null) {
        int childHeight = (child).getHeight();
        return scrollView.getHeight() < childHeight + scrollView.getPaddingTop() + scrollView.getPaddingBottom();
    }
    return false;
}

【讨论】:

    【解决方案3】:

    除了@johanvs 回复:

    您应该等待视图显示

     final ScrollView scrollView = (ScrollView) v.findViewById(R.id.scrollView);
        ViewTreeObserver viewTreeObserver = scrollView.getViewTreeObserver();
    
        viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                scrollView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                int childHeight = ((LinearLayout) v.findViewById(R.id.dataContent)).getHeight();
                boolean isScrollable = scrollView.getHeight() < childHeight + scrollView.getPaddingTop() + scrollView.getPaddingBottom();
                if (isScrollable) {
                    //Urrah! is scrollable
                }
            }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-29
      • 1970-01-01
      • 2021-10-12
      • 1970-01-01
      • 2017-01-11
      • 2014-04-20
      相关资源
      最近更新 更多