2014-03-28 6 views
1

제스처 감지기가 작동하지 않습니다. 나는 아래로 구현할 말을 여러 가지 다른 답변을 보았다. 그리고 나는 가지고있다. 그러나 그것은 아직도 doesnt 한 일을 가지고있다. 아무도 도와 줄 수 있니? 다음은 작동하지 않는 코드입니다. 보시다시피 onpleted onDown제스처 디텍터가 제스처를 인식하지 못합니다.

public class HomeActivity extends FragmentActivity implements 
GestureDetector.OnGestureListener, 
GestureDetector.OnDoubleTapListener{ 
    private static final String DEBUG_TAG = "Gestures"; 
    GestureDetectorCompat mDetector; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     View contentView = findViewById(R.id.container); 
     System.out.println("hi"); 
     if (savedInstanceState == null) { 

      getSupportFragmentManager().beginTransaction() 
        .add(R.id.container, new PlaceholderFragment()).commit(); 
     } 
     SystemBarTintManager tintManager = new SystemBarTintManager(this); 
     tintManager.setStatusBarTintEnabled(true); 
     tintManager.setStatusBarTintColor(Color.parseColor("#33000000")); 
     mDetector = new GestureDetectorCompat(this,this); 
     // Set the gesture detector as the double tap 
     // listener. 
     mDetector.setOnDoubleTapListener(this); 
    } 


    @Override 
    public boolean onDown(MotionEvent event) { 
     Log.d(DEBUG_TAG,"onDown: " + event.toString()); 
     Toast.makeText(this,"hi", Toast.LENGTH_LONG).show(); 
     System.out.println("ondown"); 
     return true; 
    } 

    @Override 
    public boolean onFling(MotionEvent event1, MotionEvent event2, 
      float velocityX, float velocityY) { 
     Log.d(DEBUG_TAG, "onFling: " + event1.toString()+event2.toString()); 
     return true; 
    } 

    @Override 
    public void onLongPress(MotionEvent event) { 
     Log.d(DEBUG_TAG, "onLongPress: " + event.toString()); 
    } 

    @Override 
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, 
      float distanceY) { 
     Log.d(DEBUG_TAG, "onScroll: " + e1.toString()+e2.toString()); 
     return true; 
    } 

    @Override 
    public void onShowPress(MotionEvent event) { 
     Log.d(DEBUG_TAG, "onShowPress: " + event.toString()); 
    } 

    @Override 
    public boolean onSingleTapUp(MotionEvent event) { 
     Log.d(DEBUG_TAG, "onSingleTapUp: " + event.toString()); 
     return true; 
    } 

    @Override 
    public boolean onDoubleTap(MotionEvent event) { 
     Log.d(DEBUG_TAG, "onDoubleTap: " + event.toString()); 
     return true; 
    } 

    @Override 
    public boolean onDoubleTapEvent(MotionEvent event) { 
     Log.d(DEBUG_TAG, "onDoubleTapEvent: " + event.toString()); 
     return true; 
    } 

    @Override 
    public boolean onSingleTapConfirmed(MotionEvent event) { 
     Log.d(DEBUG_TAG, "onSingleTapConfirmed: " + event.toString()); 
     return true; 
    } 

    /** 
    * A placeholder fragment containing a simple view. 
    */ 
    public static class PlaceholderFragment extends Fragment { 

     public PlaceholderFragment() { 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.fragment_main, container, 
        false); 
      return rootView; 
     } 
    } 


    @Override 
    public boolean onTouchEvent(MotionEvent event){ 

     int action = MotionEventCompat.getActionMasked(event); 
     switch(action) { 
      case (MotionEvent.ACTION_SCROLL) : 
      return true; 
      default : 
       return super.onTouchEvent(event); 
     } 

    } 

답변

2

SimpleOnGestureListener의 확장에 이벤트 핸들러를 넣으십시오. 여기에 마지막 예를 살펴 : https://developer.android.com/training/gestures/detector.html

당신의 코드에서 : 당신이 MyGestureListener이 SimpleOnGesture 리스너를 확장해야

mDetector = new GestureDetectorCompat(this,new MyGestureListener()); 

가 :

class MyGestureListener extends GestureDetector.SimpleOnGestureListener { 
     private static final String DEBUG_TAG = "Gestures"; 

     @Override 
     public boolean onDown(MotionEvent event) { 
      Log.d(DEBUG_TAG,"onDown: " + event.toString()); 
      return true; 
     } 

     @Override 
     public boolean onFling(MotionEvent event1, MotionEvent event2, 
       float velocityX, float velocityY) { 
      Log.d(DEBUG_TAG, "onFling: " + event1.toString()+event2.toString()); 
      return true; 
     } 
    } 
+0

가 네이했다. 감사 –

관련 문제