首页 > 解决方案 > 如何检查所有文本视图文本是否在滚动视图中可见

问题描述

我尝试检查文本的高度textview是否大于scroll视图以处理用户不必滚动阅读文本但在线没有任何内容对我有用的情况。

我在网上尝试了解决方案,但那些也不起作用

 if (binding.wvTermsAndCond.getLocalVisibleRect(scrollBounds)) {            
      // imageView is within the visible window
      Utils.showToast(mContext, "View is within the visible window", true);
 } else {
      // imageView is not within the visible window
      Utils.showToast(mContext, "View is not within the visible window", true);
 }

不可见的块被调用

标签: javaandroidtextviewscrollview

解决方案


如果要检查视图是否可见,可以使用以下方法:

    public static boolean isVisible(final View view) {
    if (view == null) {
      return false;
    }
    if (!view.isShown()) {
      return false;
    }
    final Rect actualPosition = new Rect();
    view.getGlobalVisibleRect(actualPosition);
    final Rect screen = new Rect(0, 0, getScreenWidth(), getScreenHeight());
    return actualPosition.intersect(screen);
  }

  public static int getScreenWidth() {
    return Resources.getSystem().getDisplayMetrics().widthPixels;
  }

  public static int getScreenHeight() {
    return Resources.getSystem().getDisplayMetrics().heightPixels;
  }

推荐阅读