Gesturedetector package gesture detection up/down swipe

Source: Internet
Author: User
Tags gety

In the project, it is necessary to check the top slide of the ListView to hide the tops of the view controls, before there are many implementations on the web. Find a good example of a package on git and write it down.


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

Defines an abstract class simpledetector. Inherit Gesturedetector.simpleongesturelistener abstract class, implement View.ontouchlistener interface. What are the advantages of doing this? First the ListView only needs to be setontouchlistener. The definition of this abstract class simpledetector set into good. Then this class of Simpledetector only has to be responsible for checking the slide or the falling event. Logic has been separated.

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

Public abstract class Simpledetector extends Gesturedetector.simpleongesturelistener implements view.ontouchlistener{ Private final gesturedetector mdetector;private final int mslop;//slop sloshing meaning private Boolean mignore;//whether to ignore listening up and down scrolling private float mdowny;public abstract void Onscrolldown ();p ublic abstract void Onscrollup ();p ublic simpledetector (Context Context) {Mdetector = new gesturedetector (context,this); mslop = Getslop (context);} public Boolean Isignore () {return mignore;} public void Setignore (Boolean mignore) {this.mignore = Mignore;} protected int Getslop (context context) {if (Build.VERSION.SDK_INT < build.version_codes. FROYO) {return Viewconfiguration.gettouchslop () * 2;} Else{return viewconfiguration.get (context). Getscaledpagingtouchslop ();}} @Overridepublic Boolean Ondown (motionevent e) {//TODO auto-generated Method stubmdowny = E.gety (); return false;}  @Overridepublic boolean onscroll (motionevent E1, motionevent E2, float distancex,float distancey) {//TODO auto-generated Method Stubif (MIgnore) return False;if (distancey==0) {mdowny = E2.gety ();} float distance = mdowny-e2.gety (); if (distance <-mslop) {Onscrolldown ();} else if (Distance > Mslop) {Onscrollup ();} return false;} @Overridepublic boolean OnTouch (View V, motionevent event) {//TODO auto-generated method Stubmdetector.ontouchevent ( event); return false;}}

Handling animated display hidden event logic processing class

public class Showhideonscroll extends Simpledetector implements animatorlistener{private final View mview;private int mSh owanimation;private int mhideanimation;private int mtranslationy;private int curshowhide = 0;public ShowHideOnScroll ( View View,int translationy) {super (View.getcontext ()); MView = View;mtranslationy = Translationy;} Public Showhideonscroll (View view,int show,int hide,int translationy) {super (View.getcontext ()); mView = view; Mshowanimation = Show;mhideanimation = Hide;mtranslationy = Translationy;} @Overridepublic void Onscrolldown () {mview.setvisibility (view.visible); Animateshow (); curshowhide = 0;} @Overridepublic void Onscrollup () {mview.setvisibility (view.visible); Animatehide (); curshowhide = 1;} private void Animateshow () {mview.settranslationy (mtranslationy); Mview.animate (). Translationy (0). Setinterpolator ( New Acceleratedecelerateinterpolator ()). Setstartdelay (0). Setduration (+). Setlistener (Showhideonscroll.this). Start (); Setignore (true);} private void Animatehide () {mview.seTtranslationy (0); Mview.animate (). Translationy (Mtranslationy). Setinterpolator (New Acceleratedecelerateinterpolator ()). Setstartdelay (0). Setduration (+). Setlistener (Showhideonscroll.this). Start (); Setignore (true);} @Overridepublic void Onanimationstart (Animator animation) {//TODO auto-generated method stub} @Overridepublic void Onanimationend (Animator animation) {//TODO auto-generated method stubif (curshowhide==0) {mview.setvisibility ( view.visible); mview.settranslationy (0);} else if (curshowhide = = 1) {mview.setvisibility (view.invisible); Mview.settranslationy (mtranslationy);} Setignore (false);} @Overridepublic void Onanimationcancel (Animator animation) {//TODO auto-generated method stub} @Overridepublic void Onanimationrepeat (Animator animation) {//TODO auto-generated method stub}}

All right. The package of the above two classes is very easy. But it is perfect, and the logical processing of each event is separated. A control such as a ListView just needs to pass in its own Touchlistener event.

This can be extrapolate applied to other places, and it is important to write code frameworks later. The logic of things should be separated. The code is perfect and impeccable.

The new company has been in the one-month class. The pressure Alexander, all is the top student, the postgraduate also to write the code, the sincerity ~ then was the boss in front of the research intern face training once. It's hard to be true. or to improve their ability to have time to learn more. You have the power, you don't have to prove yourself.





Gesturedetector package gesture detection up/down swipe

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.