GestureDetector封装手势检测上下滑动

项目中需要检测ListView的上滑下滑隐藏顶部View控件,之前在网上也有很多实现案例。在git上发现个封装很不错的例子,记录下来。

 

GestureDetector是一个手势检测类,内部有个SimpleOnGestureListener手势监听类。

定义一个抽象类SimpleDetector,继承GestureDetector.SimpleOnGestureListener抽象类,实现View.OnTouchListener接口。这样做有什么好处呢?首先ListView只要setOnTouchListener,把定义的这个抽象类SimpleDetector设置进就好。然后这个类SimpleDetector只需要负责检测上滑还是下滑事件,逻辑得到了分离。

为了要实现ListView顶部View控件的动画效果,需要定义另外一个类继承上面抽象的SimpleDetector类,在这个类里单独处理上滑下滑时候需要执行的动画或者其它逻辑事件。上面的SimpleDetector抽象类提供两个抽象方法供子类去实现。这样整个封装就显得非常完美了。

 

[java] view plaincopy
 
  1. public abstract class SimpleDetector extends GestureDetector.SimpleOnGestureListener implements View.OnTouchListener{  
  2.   
  3.     private final GestureDetector mDetector;  
  4.     private final int mSlop;//slop晃荡的意思  
  5.     private boolean mIgnore;//是否忽略监听上下滚动  
  6.     private float mDownY;  
  7.       
  8.     public abstract void onScrollDown();  
  9.     public abstract void onScrollUp();  
  10.       
  11.     public SimpleDetector(Context context){  
  12.         mDetector = new GestureDetector(context,this);  
  13.         mSlop = getSlop(context);  
  14.     }  
  15.       
  16.     public boolean isIgnore() {  
  17.         return mIgnore;  
  18.     }  
  19.     public void setIgnore(boolean mIgnore) {  
  20.         this.mIgnore = mIgnore;  
  21.     }  
  22.     protected int getSlop(Context context){  
  23.         if(Build.VERSION.SDK_INT < Build.VERSION_CODES.FROYO){  
  24.             return ViewConfiguration.getTouchSlop() * 2;  
  25.         }else{  
  26.             return ViewConfiguration.get(context).getScaledPagingTouchSlop();  
  27.         }  
  28.     }  
  29.       
  30.     @Override  
  31.     public boolean onDown(MotionEvent e) {  
  32.         // TODO Auto-generated method stub  
  33.         mDownY = e.getY();  
  34.         return false;  
  35.     }  
  36.       
  37.     @Override  
  38.     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,  
  39.             float distanceY) {  
  40.         // TODO Auto-generated method stub  
  41.         if(mIgnore)  
  42.             return false;  
  43.         if(distanceY==0){  
  44.             mDownY = e2.getY();  
  45.         }  
  46.           
  47.         float distance = mDownY - e2.getY();  
  48.           
  49.         if(distance < -mSlop){  
  50.             onScrollDown();  
  51.         }else if(distance > mSlop){  
  52.             onScrollUp();  
  53.         }  
  54.         return false;  
  55.     }  
  56.       
  57.     @Override  
  58.     public boolean onTouch(View v, MotionEvent event) {  
  59.         // TODO Auto-generated method stub  
  60.         mDetector.onTouchEvent(event);  
  61.         return false;  
  62.     }  
  63.   
  64. }  


处理动画显示隐藏事件逻辑处理类

 

 

[java] view plaincopy
 
  1. public class ShowHideOnScroll extends SimpleDetector implements AnimatorListener{  
  2.   
  3.     private final View mView;  
  4.     private int mShowAnimation;  
  5.     private int mHideAnimation;  
  6.     private int mTranslationY;  
  7.     private int curShowHide = 0;  
  8.       
  9.     public ShowHideOnScroll(View view,int translationY){  
  10.         super(view.getContext());  
  11.         mView = view;  
  12.         mTranslationY = translationY;  
  13.     }  
  14.       
  15.     public ShowHideOnScroll(View view,int show,int hide,int translationY) {  
  16.         super(view.getContext());  
  17.         mView = view;  
  18.         mShowAnimation = show;  
  19.         mHideAnimation = hide;  
  20.         mTranslationY = translationY;  
  21.     }  
  22.   
  23.     @Override  
  24.     public void onScrollDown() {  
  25.         mView.setVisibility(View.VISIBLE);  
  26.         animateShow();  
  27.         curShowHide = 0;  
  28.     }  
  29.   
  30.     @Override  
  31.     public void onScrollUp() {  
  32.         mView.setVisibility(View.VISIBLE);  
  33.         animateHide();  
  34.         curShowHide = 1;  
  35.     }  
  36.     private void animateShow(){  
  37.         mView.setTranslationY(mTranslationY);  
  38.         mView.animate().translationY(0).setInterpolator(new AccelerateDecelerateInterpolator())  
  39.         .setStartDelay(0).setDuration(400).setListener(ShowHideOnScroll.this).start();  
  40.         setIgnore(true);  
  41.     }  
  42.     private void animateHide(){  
  43.         mView.setTranslationY(0);  
  44.         mView.animate().translationY(mTranslationY).setInterpolator(new AccelerateDecelerateInterpolator())  
  45.         .setStartDelay(0).setDuration(400).setListener(ShowHideOnScroll.this).start();  
  46.         setIgnore(true);  
  47.     }  
  48.   
  49.     @Override  
  50.     public void onAnimationStart(Animator animation) {  
  51.         // TODO Auto-generated method stub  
  52.           
  53.     }  
  54.   
  55.     @Override  
  56.     public void onAnimationEnd(Animator animation) {  
  57.         // TODO Auto-generated method stub  
  58.         if(curShowHide==0){  
  59.             mView.setVisibility(View.VISIBLE);  
  60.             mView.setTranslationY(0);  
  61.         }else if(curShowHide == 1){  
  62.             mView.setVisibility(View.INVISIBLE);  
  63.             mView.setTranslationY(mTranslationY);  
  64.         }  
  65.         setIgnore(false);  
  66.     }  
  67.   
  68.     @Override  
  69.     public void onAnimationCancel(Animator animation) {  
  70.         // TODO Auto-generated method stub  
  71.           
  72.     }  
  73.   
  74.     @Override  
  75.     public void onAnimationRepeat(Animator animation) {  
  76.         // TODO Auto-generated method stub  
  77.           
  78.     }  
  79.       
  80.   
  81.   
  82. }  

 

posted on 2015-09-22 13:58  lion armor  阅读(199)  评论(0编辑  收藏  举报