2017-04-25 5 views
1

Comment appeler un événement de défilement après avoir appelé Longpress sans relâcher un écran (à l'aide de gesturedetector)?Appelez l'événement de défilement après l'appel de Longpress à l'aide de GestureDetector

Voici ma classe:

public class TestingGestureDetector extends AppCompatActivity implements GestureDetector.OnGestureListener { 
    TextView mTextView; 
    private GestureDetector mGestureDetector; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_testing_gesture_detector); 

     mTextView = (TextView) findViewById(R.id.gesture); 
     mTextView.setOnTouchListener(a()); 
     mGestureDetector = new GestureDetector(this, this);   // 


    } 


    protected View.OnTouchListener a() { 
     return new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       mGestureDetector.onTouchEvent(event);   // 

       if (event.getAction() == MotionEvent.ACTION_UP) 
        mTextView.setText("Release"); 
       return true; 
      } 
     }; 
    } 

    @Override 
    public boolean onDown(MotionEvent event) { 
     mTextView.setText("Press"); 
     return true; 
    } 

    @Override 
    public void onShowPress(MotionEvent event) { 

    } 

    @Override 
    public boolean onSingleTapUp(MotionEvent event) { 
     return true; 
    } 

    @Override 
    public boolean onScroll(MotionEvent event1, MotionEvent event2, float distanceX, float distanceY) { 

     mTextView.setText("Move"); 
     return true; 
    } 

    @Override 
    public void onLongPress(MotionEvent event) { 

     mTextView.setText("Long Press"); 
     //mGestureDetector.setIsLongpressEnabled(false); 
    } 

    @Override 
    public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) { 

     return true; 
    } 
} 

Répondre

1
  • Désactiver le LongPress en utilisant setIsLongpressEnabled (isLongpressEnabled) sur votre gestureDetector.
  • Vérifiez l'état de certains indicateurs pressés après 0,5 s à l'aide d'un temporisateur ou d'un filetage .

Essayez:

public boolean onTouchEvent(MotionEvent event) { 
     if (mGestureDetector.onTouchEvent(event)== true) 
     { 
      //Fling or other gesture detected (not logpress because it is disabled) 
     } 
     else 
     { 
      //Manually handle the event. 
      if (event.getAction() == MotionEvent.ACTION_DOWN) 
      { 
       //Remember the time and press position 
       Log.e("test","Action down"); 
      } 
      if (event.getAction() == MotionEvent.ACTION_MOVE) 
      { 
       //Check if user is actually longpressing, not slow-moving 
       // if current position differs much then press positon then discard whole thing 
       // If position change is minimal then after 0.5s that is a longpress. You can now process your other gestures 
       Log.e("test","Action move"); 
      } 
      if (event.getAction() == MotionEvent.ACTION_UP) 
      { 
       //Get the time and position and check what that was :) 
       Log.e("test","Action down"); 
      } 

     } 
     return true; 
    }