2013-09-04 28 views
7

içinde sırasıyla [GestureDetector.OnGestureListener] [1] sınıf yöntemi [onSingleTapUp (MotionEvent)] [2]: a kademe kadar MotionEvent que Oluştuğunda fark Android

Onaylanmış tetiklenen o.

Bu yöntem aynı onclick'in işlevine sahiptir? Onclick yönteminden istediğim aynı davranışı uygulamak için bu yöntemi kullanabilir miyim?

cevap

0

Bazı testler yaptım ve cevabın YES olduğu görülüyor, onclick yerine onSingleTapUp kullanabilirsiniz. OnSingleTapUp yöntemi, çift veya uzun tıklatmanın basit bir tıklamasını iyi bir şekilde ayırt edebilir. Benim testi: Basit bir tıklama için

public class MainFragmentScrollerController 
{ 
    private MainFragment mFragment; 
    private GestureDetectorCompat mGestureDetector; 

    public MainFragmentScrollerController(MainFragment fragment) 
    { 
     mFragment = fragment; 
     mGestureDetector = new GestureDetectorCompat(mFragment.getActivity(), new GestureListener()); 
    } 

    public void setGestureListener() 
    { 
     mFragment.myView.setOnTouchListener(new OnTouchListener() 
     { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) 
      { 
       boolean gestureResult = mGestureDetector.onTouchEvent(event); 
       Log.i("GestureListener", "gestureResult = " + gestureResult); 
       return gestureResult; 
      } 
     }); 
    } 

    public class GestureListener extends SimpleOnGestureListener 
    { 
     private final String TAG = "GestureListener"; 
     @Override 
     public boolean onDown(MotionEvent event) 
     { 
      Log.d(TAG,"onDown: " + event.toString()); 
      return true; 
     } 

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

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

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

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

Sonuç: kaçamağı için

09-04 14:22:12.520: D/GestureListener(17689): onDown: MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=179.22656, y[0]=41.0625, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=34538199, downTime=34538199, deviceId=1, source=0x1002 } 
09-04 14:22:12.520: I/GestureListener(17689): gestureResult = true 
09-04 14:22:12.560: I/GestureListener(17689): gestureResult = false 
09-04 14:22:12.930: I/GestureListener(17689): gestureResult = false 
09-04 14:22:12.950: I/GestureListener(17689): gestureResult = false 
09-04 14:22:13.010: I/GestureListener(17689): gestureResult = false 
09-04 14:22:13.100: I/GestureListener(17689): gestureResult = false 
09-04 14:22:13.130: I/GestureListener(17689): gestureResult = false 
09-04 14:22:13.150: I/GestureListener(17689): gestureResult = false 
09-04 14:22:13.180: I/GestureListener(17689): gestureResult = false 
09-04 14:22:13.200: D/GestureListener(17689): onLongPress: MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=179.22656, y[0]=41.0625, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=34538199, downTime=34538199, deviceId=1, source=0x1002 } 
09-04 14:22:13.230: I/GestureListener(17689): gestureResult = false 

Sonuç:

09-04 14:24:09.650: D/GestureListener(17689): onDown: MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=332.15625, y[0]=2.625, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=34655337, downTime=34655337, deviceId=1, source=0x1002 } 
09-04 14:24:09.650: I/GestureListener(17689): gestureResult = true 
09-04 14:24:09.690: D/GestureListener(17689): onScroll: MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=332.15625, y[0]=2.625, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=34655337, downTime=34655337, deviceId=1, source=0x1002 }MotionEvent { action=ACTION_MOVE, id[0]=0, x[0]=331.9604, y[0]=-72.36835, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=2, eventTime=34655368, downTime=34655337, deviceId=1, source=0x1002 } 
09-04 14:24:09.690: I/GestureListener(17689): gestureResult = true 
09-04 14:24:09.710: D/GestureListener(17689): onScroll: MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=332.15625, y[0]=2.625, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=34655337, downTime=34655337, deviceId=1, source=0x1002 }MotionEvent { action=ACTION_MOVE, id[0]=0, x[0]=337.91425, y[0]=-124.40411, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=2, eventTime=34655385, downTime=34655337, deviceId=1, source=0x1002 } 
09-04 14:24:09.710: I/GestureListener(17689): gestureResult = true 
09-04 14:24:09.730: D/GestureListener(17689): onFling: MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=332.15625, y[0]=2.625, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=34655337, downTime=34655337, deviceId=1, source=0x1002 }MotionEvent { action=ACTION_UP, id[0]=0, x[0]=337.91425, y[0]=-124.40411, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=34655414, downTime=34655337, deviceId=1, source=0x1002 } 
09-04 14:24:09.730: I/GestureListener(17689): gestureResult = true 
uzun tıklama için

09-04 14:20:58.040: D/GestureListener(17689): onDown: MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=188.71875, y[0]=26.0625, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=34463725, downTime=34463725, deviceId=1, source=0x1002 } 
09-04 14:20:58.040: I/GestureListener(17689): gestureResult = true 
09-04 14:20:58.100: I/GestureListener(17689): gestureResult = false 
09-04 14:20:58.130: D/GestureListener(17689): onSingleTapUp: MotionEvent { action=ACTION_UP, id[0]=0, x[0]=188.71875, y[0]=26.0625, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=34463811, downTime=34463725, deviceId=1, source=0x1002 } 
09-04 14:20:58.130: I/GestureListener(17689): gestureResult = true 

Sonucu