Android判断view是否部分被遮挡(或者移出屏幕)的方法

部分内容参考http://blog.csdn.net/peidonghui/article/details/49583263,在此对原作者表示感谢。


在某些需求下,需要判断某一个View是否被遮挡,比如ListView的headerView中某一个View在ListView向上滑动到开始不可见时,在页面顶部固定显示一个View。在刚开始时,处理方式是在ListView的onScroll()中判断该View距离屏幕顶部的距离,来作为判断条件,网上好不容易找到解决方法,但测试的妹纸却在android 6.0的手机上测出兼容性BUG ,害得我周末跟妹纸约会都在想这个问题该怎么办,看来还是要仔细系统的研究一下自定义View和ViewGroup的相关问题


废话不多说,直接上代码。

1.当Android 的版本为6.0以下时


[java]  view plain  copy
  1. </pre><pre name="code" class="java">  
[java]  view plain  copy
  1. </pre><pre name="code" class="java">  
[java]  view plain  copy
  1. pullListView.setOnScrollListener(new AbsListView.OnScrollListener() {  
  2.             @Override  
  3.             public void onScrollStateChanged(AbsListView view, int scrollState) {  
  4.   
  5.   
  6.             }  
  7.   
  8.   
  9.             @Override  
  10.             public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {  
  11.                 Rect rect = new Rect();  
  12.                 line.getGlobalVisibleRect(rect);  
  13.                 if ((rect.top < statusBarHeight + DimenUtil.dip2px(mContext, 97)) ) {  
  14.                     if (rl_choosed_tag.getVisibility() == View.INVISIBLE)  
  15.                         rl_choosed_tag.setVisibility(View.VISIBLE);  
  16.                 } else {  
  17.                     if (rl_choosed_tag.getVisibility() == View.VISIBLE)  
  18.                         rl_choosed_tag.setVisibility(View.INVISIBLE);  
  19.                 }  
  20.             }  
  21.         });  


以上line为listView的headerView布局中的一个view,通过line.getGlobalVisibleRect(rect);来判断该view距离屏幕顶部的距离是否小于指定值(此处该临界值为

statusBarHeight + DimenUtil.dip2px(mContext, 97))来作为另外一个View是否显示的条件


2.当Android 的S版本为6.0时

在onScroll()中这样处理

[java]  view plain  copy
  1. if (Build.VERSION.SDK_INT >= 23) {  
  2.     if (isViewCovered(mSortFlowLayout)) {  
  3.         if (rl_choosed_tag.getVisibility() == View.VISIBLE)  
  4.             rl_choosed_tag.setVisibility(View.INVISIBLE);  
  5.     } else {  
  6.         if (rl_choosed_tag.getVisibility() == View.INVISIBLE)  
  7.             rl_choosed_tag.setVisibility(View.VISIBLE);  
  8.     }  
  9. }  


涉及的isViewCovered()函数如下:

[java]  view plain  copy
  1. public boolean isViewCovered(final View view) {  
  2.     View currentView = view;  
  3.   
  4.     Rect currentViewRect = new Rect();  
  5.     boolean partVisible = currentView.getGlobalVisibleRect(currentViewRect);  
  6.     boolean totalHeightVisible = (currentViewRect.bottom - currentViewRect.top) >= view.getMeasuredHeight();  
  7.     boolean totalWidthVisible = (currentViewRect.right - currentViewRect.left) >= view.getMeasuredWidth();  
  8.     boolean totalViewVisible = partVisible && totalHeightVisible && totalWidthVisible;  
  9.     if (!totalViewVisible)//if any part of the view is clipped by any of its parents,return true  
  10.         return true;  
  11.   
  12.     while (currentView.getParent() instanceof ViewGroup) {  
  13.         ViewGroup currentParent = (ViewGroup) currentView.getParent();  
  14.         if (currentParent.getVisibility() != View.VISIBLE)//if the parent of view is not visible,return true  
  15.             return true;  
  16.   
  17.         int start = indexOfViewInParent(currentView, currentParent);  
  18.         for (int i = start + 1; i < currentParent.getChildCount(); i++) {  
  19.             Rect viewRect = new Rect();  
  20.             view.getGlobalVisibleRect(viewRect);  
  21.             View otherView = currentParent.getChildAt(i);  
  22.             Rect otherViewRect = new Rect();  
  23.             otherView.getGlobalVisibleRect(otherViewRect);  
  24.             if (Rect.intersects(viewRect, otherViewRect))//if view intersects its older brother(covered),return true  
  25.                 return true;  
  26.         }  
  27.         currentView = currentParent;  
  28.     }  
  29.     return false;  
  30. }  
  31.   
  32.   
  33. private int indexOfViewInParent(View view, ViewGroup parent) {  
  34.     int index;  
  35.     for (index = 0; index < parent.getChildCount(); index++) {  
  36.         if (parent.getChildAt(index) == view)  
  37.             break;  
  38.     }  
  39.     return index;  
  40. }  

虽然一个简单的判断却还需要分版本不同单独处理,不过应该有更加简洁优雅的解决方案,欢迎各位给出指导。
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值