Gesture Detector Packaging Gesture Detection for Up and Down Sliding

Links to the original text: http://www.cnblogs.com/hxdhero/p/4828765.html

The project needs to detect ListView's sliding up and down to hide the top View control. There have been many implementation cases on the Internet before. A good example of encapsulation was found on git and recorded.

 

GestureDetector is a gesture detection class with a SimpleOnGestureListener gesture listener class inside.

Define an abstract class SimpleDetector, inherit the GestureDetector.SimpleOnGestureListener Abstract class, and implement the View.OnTouchListener interface. What's the advantage of doing this? First, ListView only needs to set OnTouchListener to set the abstract class SimpleDetector defined in it. Then the SimpleDetector of this class is only responsible for detecting up-and-down events, and the logic is separated.

In order to achieve the animation effect of the View control on the top of ListView, another class needs to be defined to inherit the abstract SimpleDetector class above, in which animation or other logical events that need to be executed when sliding up or down are handled separately. The SimpleDetector abstract class above provides two abstract methods for subclasses to implement. So the whole package is perfect.

 

  1. public abstract class SimpleDetector extends GestureDetector.SimpleOnGestureListener implements View.OnTouchListener{  
  2.   
  3.     private final GestureDetector mDetector;  
  4.     private final int mSlop;//Meaning of slop sloshing
  5.     private boolean mIgnore;//Ignore listening scrolling up and down
  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. }  


Processing Animation Display Hidden Event Logic Processing Class

 

 

  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. }  

 

Reprinted at: https://www.cnblogs.com/hxdhero/p/4828765.html

Tags: git

Posted on Tue, 08 Oct 2019 14:33:31 -0700 by daniel_lee_hill