下拉刷新组件中嵌套ViewPager的一个手势冲突

在项目中使用了 android-Ultra-Pull-To-Refresh 组件实现下拉刷新,有个页面是在下拉刷新ListView中包含了一个ViewPager,因为ListView和ViewPager的事件冲突导致ViewPager左右滑动的时候非常难,这是个常见的问题,使用下面改造的ViewPager就能解决这个问题。 

Java代码   收藏代码
  1. package com.myzaker.autoinsurance.ui.view;  
  2.   
  3. import android.content.Context;  
  4. import android.os.Debug;  
  5. import android.support.v4.view.ViewPager;  
  6. import android.util.AttributeSet;  
  7. import android.view.MotionEvent;  
  8. import android.view.ViewGroup;  
  9.   
  10.   
  11.   
  12. /** 
  13.  * Created by Denny on 2015/8/1. 
  14.  */  
  15. public class DisallowParentTouchViewPager extends ViewPager {  
  16.   
  17.     private ViewGroup parent;  
  18.   
  19.     public DisallowParentTouchViewPager(Context context) {  
  20.         super(context);  
  21.     }  
  22.   
  23.     public DisallowParentTouchViewPager(Context context, AttributeSet attrs) {  
  24.         super(context, attrs);  
  25.     }  
  26.   
  27.     public void setNestParent(ViewGroup parent) {  
  28.         this.parent = parent;  
  29.     }  
  30.   
  31.     @Override  
  32.     public boolean dispatchTouchEvent(MotionEvent ev) {  
  33.         if (parent != null) {  
  34.             parent.requestDisallowInterceptTouchEvent(true);  
  35.         }  
  36.         return super.dispatchTouchEvent(ev);  
  37.     }  
  38.   
  39.     @Override  
  40.     public boolean onInterceptTouchEvent(MotionEvent ev) {  
  41.         if (parent != null) {  
  42.             parent.requestDisallowInterceptTouchEvent(true);  
  43.         }  
  44.         return super.onInterceptTouchEvent(ev);  
  45.     }  
  46.   
  47.     @Override  
  48.     public boolean onTouchEvent(MotionEvent ev) {  
  49.         if (parent != null) {  
  50.             parent.requestDisallowInterceptTouchEvent(true);  
  51.         }  
  52.         return super.onTouchEvent(ev);  
  53.     }  
  54. }  


这段代码也很好理解,就是在处理ViewPager touch事件的时候把他的父组件的事件屏蔽掉,这样就只响应了ViewPager touch事件,其实主要是parent.requestDisallowInterceptTouchEvent(true);这句代码的作用,这个是android组件的约定,这个会一级一级的上传,让所有的父组件都不响应touch事件。 

但是在android-Ultra-Pull-To-Refresh的PtrFrameLayout上使用上面改造的ViewPager却没有效果,查看PtrFrameLayout源码发现他在实现dispatchTouchEvent方法时,没有考虑到FLAG_DISALLOW_INTERCEPT的因素,导致requestDisallowInterceptTouchEvent方法不起作用。OK,找到了问题点那就改造下PtrFrameLayout好了, 如下: 

Java代码   收藏代码
  1. package com.myzaker.autoinsurance.ui.view;  
  2.   
  3. import android.content.Context;  
  4. import android.util.AttributeSet;  
  5. import android.view.MotionEvent;  
  6.   
  7. import in.srain.cube.views.ptr.PtrFrameLayout;  
  8.   
  9. /** 
  10.  * Created by Denny on 2015/8/1. 
  11.  */  
  12. public class FixRequestDisallowTouchEventPtrFrameLayout extends PtrFrameLayout {  
  13.   
  14.     private boolean disallowInterceptTouchEvent = false;  
  15.   
  16.     public FixRequestDisallowTouchEventPtrFrameLayout(Context context) {  
  17.         super(context);  
  18.     }  
  19.   
  20.     public FixRequestDisallowTouchEventPtrFrameLayout(Context context, AttributeSet attrs) {  
  21.         super(context, attrs);  
  22.     }  
  23.   
  24.     public FixRequestDisallowTouchEventPtrFrameLayout(Context context, AttributeSet attrs, int defStyle) {  
  25.         super(context, attrs, defStyle);  
  26.     }  
  27.   
  28.     @Override  
  29.     public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {  
  30.         disallowInterceptTouchEvent = disallowIntercept;  
  31.         super.requestDisallowInterceptTouchEvent(disallowIntercept);  
  32.     }  
  33.   
  34.     @Override  
  35.     public boolean dispatchTouchEvent(MotionEvent e) {  
  36.         if (disallowInterceptTouchEvent) {  
  37.             return dispatchTouchEventSupper(e);  
  38.         }  
  39.         return super.dispatchTouchEvent(e);  
  40.     }  
  41. }  


继承PtrFrameLayout,当如果调用了requestDisallowInterceptTouchEvent方法,那么dispatchTouchEvent方法就调用dispatchTouchEventSupper,这个其实就是ViewGroup中dispatchTouchEvent的实现。 OK,使用这个FixRequestDisallowTouchEventPtrFrameLayout和DisallowParentTouchViewPager类, 就解决了android-Ultra-Pull-To-Refresh下拉刷新组件和ViewPager事件冲突的问题。
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值