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
- Public abstract class Simpledetector extends Gesturedetector.simpleongesturelistener implements view.ontouchlistener{
- Private final gesturedetector mdetector;
- private final int mslop; The meaning of slop sloshing
- private Boolean mignore; Whether to ignore listening up and down scrolling
- private float mdowny;
- public abstract void Onscrolldown ();
- public abstract void Onscrollup ();
- Public 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 ();
- }
- }
- @Override
- Public Boolean ondown (Motionevent e) {
- //TODO auto-generated method stub
- Mdowny = E.gety ();
- return false;
- }
- @Override
- Public Boolean onscroll (motionevent E1, motionevent E2, float Distancex,
- float Distancey) {
- //TODO auto-generated method stub
- if (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;
- }
- @Override
- Public Boolean OnTouch (View V, motionevent event) {
- //TODO auto-generated method stub
- Mdetector.ontouchevent (event);
- return false;
- }
- }
Handling animated display hidden event logic processing class
[Java]View Plaincopy
- Public class Showhideonscroll extends Simpledetector implements animatorlistener{
- Private final View MView;
- private int mshowanimation;
- 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;
- }
- @Override
- public void Onscrolldown () {
- Mview.setvisibility (view.visible);
- Animateshow ();
- Curshowhide = 0;
- }
- @Override
- public 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);
- }
- @Override
- public void Onanimationstart (Animator animation) {
- //TODO auto-generated method stub
- }
- @Override
- public void Onanimationend (Animator animation) {
- //TODO auto-generated method stub
- if (curshowhide==0) {
- Mview.setvisibility (view.visible);
- Mview.settranslationy (0);
- }Else if (curshowhide = = 1) {
- Mview.setvisibility (view.invisible);
- Mview.settranslationy (Mtranslationy);
- }
- Setignore (false);
- }
- @Override
- public void Onanimationcancel (Animator animation) {
- //TODO auto-generated method stub
- }
- @Override
- public void Onanimationrepeat (Animator animation) {
- //TODO auto-generated method stub
- }
- }
Gesturedetector package gesture detection swipe up and down