Gesturedetector package gesture detection swipe up and down

Source: Internet
Author: User
Tags gety

The project needs to detect the top slide of the ListView to hide the tops of the view controls, and there have been many implementation cases on the web before. Find a good example of a package on git and write it down.

Gesturedetector is a gesture detection class with a simpleongesturelistener gesture listener inside.

Define an abstract class Simpledetector, inherit Gesturedetector.simpleongesturelistener abstract class, implement View.ontouchlistener interface. What good does it do? First the ListView as long as Setontouchlistener, the definition of this abstract class Simpledetector set into good. Then this class of simpledetector only need to be responsible for detecting the slip or slide event, the logic is separated.

In order to animate the top view control of a ListView, it is necessary to define another class that inherits the abstract Simpledetector class, which handles animations or other logical events that need to be performed when the slide is slipping in this class alone. The Simpledetector abstract class above provides two abstract methods for subclasses to implement. So the whole package looks perfect.

[Java]View Plaincopy
  1. Public abstract class Simpledetector extends Gesturedetector.simpleongesturelistener implements view.ontouchlistener{
  2. Private final gesturedetector mdetector;
  3. private final int mslop; The meaning of slop sloshing
  4. private Boolean mignore; Whether to ignore listening up and down scrolling
  5. private float mdowny;
  6. public abstract void Onscrolldown ();
  7. public abstract void Onscrollup ();
  8. Public Simpledetector (context context) {
  9. Mdetector = New Gesturedetector (context, this);
  10. Mslop = Getslop (context);
  11. }
  12. Public Boolean Isignore () {
  13. return mignore;
  14. }
  15. public void Setignore (boolean mignore) {
  16. This.mignore = Mignore;
  17. }
  18. protected int Getslop (context context) {
  19. if (Build.VERSION.SDK_INT < build.version_codes. FROYO) {
  20. return Viewconfiguration.gettouchslop () * 2;
  21. }else{
  22. return Viewconfiguration.get (context). Getscaledpagingtouchslop ();
  23. }
  24. }
  25. @Override
  26. Public Boolean ondown (Motionevent e) {
  27. //TODO auto-generated method stub
  28. Mdowny = E.gety ();
  29. return false;
  30. }
  31. @Override
  32. Public Boolean onscroll (motionevent E1, motionevent E2, float Distancex,
  33. float Distancey) {  
  34. //TODO auto-generated method stub
  35. if (mignore)
  36. return false;
  37. if (distancey==0) {
  38. Mdowny = E2.gety ();
  39. }
  40. Float distance = mdowny-e2.gety ();
  41. if (distance <-mslop) {
  42. Onscrolldown ();
  43. }Else if (distance > Mslop) {
  44. Onscrollup ();
  45. }
  46. return false;
  47. }
  48. @Override
  49. Public Boolean OnTouch (View V, motionevent event) {
  50. //TODO auto-generated method stub
  51. Mdetector.ontouchevent (event);
  52. return false;
  53. }
  54. }


Handling animated display hidden event logic processing class

[Java]View Plaincopy
  1. Public class Showhideonscroll extends Simpledetector implements animatorlistener{
  2. Private final View MView;
  3. private int mshowanimation;
  4. private int mhideanimation;
  5. private int mtranslationy;
  6. private int curshowhide = 0;
  7. Public showhideonscroll (view view,int translationy) {
  8. Super (View.getcontext ());
  9. MView = view;
  10. Mtranslationy = Translationy;
  11. }
  12. Public showhideonscroll (view view,int show,int hide,int translationy) {
  13. Super (View.getcontext ());
  14. MView = view;
  15. Mshowanimation = Show;
  16. Mhideanimation = hide;
  17. Mtranslationy = Translationy;
  18. }
  19. @Override
  20. public void Onscrolldown () {
  21. Mview.setvisibility (view.visible);
  22. Animateshow ();
  23. Curshowhide = 0;
  24. }
  25. @Override
  26. public void Onscrollup () {
  27. Mview.setvisibility (view.visible);
  28. Animatehide ();
  29. Curshowhide = 1;
  30. }
  31. private void Animateshow () {
  32. Mview.settranslationy (Mtranslationy);
  33. Mview.animate (). Translationy (0). Setinterpolator (new Acceleratedecelerateinterpolator ())
  34. . Setstartdelay (0). Setduration (+). Setlistener (Showhideonscroll.   This). Start ();
  35. Setignore (true);
  36. }
  37. private void Animatehide () {
  38. Mview.settranslationy (0);
  39. Mview.animate (). Translationy (Mtranslationy). Setinterpolator (new Acceleratedecelerateinterpolator ())
  40. . Setstartdelay (0). Setduration (+). Setlistener (Showhideonscroll.   This). Start ();
  41. Setignore (true);
  42. }
  43. @Override
  44. public void Onanimationstart (Animator animation) {
  45. //TODO auto-generated method stub
  46. }
  47. @Override
  48. public void Onanimationend (Animator animation) {
  49. //TODO auto-generated method stub
  50. if (curshowhide==0) {
  51. Mview.setvisibility (view.visible);
  52. Mview.settranslationy (0);
  53. }Else if (curshowhide = = 1) {
  54. Mview.setvisibility (view.invisible);
  55. Mview.settranslationy (Mtranslationy);
  56. }
  57. Setignore (false);
  58. }
  59. @Override
  60. public void Onanimationcancel (Animator animation) {
  61. //TODO auto-generated method stub
  62. }
  63. @Override
  64. public void Onanimationrepeat (Animator animation) {
  65. //TODO auto-generated method stub
  66. }
  67. }

Gesturedetector package gesture detection swipe up and down

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.