2012-03-16 4 views
0

私のプログラムは拡張可能なリストを作成し、スワイプを実装しましたが、ダブルタップを追加する方法はわかりません。Android ExpandableLists&DoubleTapジェスチャーの追加、リスナーの追加方法

まず、私は私のメインクラス内list= getExpandableListView();

を持って、私は次のコード使用して気の抜けたビールを実装:

final ActivitySwipeDetector swipeDetector = new ActivitySwipeDetector(); 


     list.setOnTouchListener(swipeDetector); 

     list.setOnItemClickListener(new OnItemClickListener() { 

       public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
         if (swipeDetector.swipeDetected()){ 
          // do the onSwipe action 
         } else { 
          // do the onItemClick action 
         } 
        } 
           }); 


      list.setOnItemLongClickListener(new OnItemLongClickListener() { 
       public boolean onItemLongClick(AdapterView<?> parent, View view,int position, long id) { 
        if (swipeDetector.swipeDetected()){ 
         // do the onSwipe action 
         return true; 
        } else { 
         // do the onItemLongClick action 
         return false; 
        } 
       } 
      }); 

をそして私は私のスワイプ検出器であるクラス作っ:

public class ActivitySwipeDetector implements View.OnTouchListener { 

public static enum Action { 
    LR, // Left to Right 
    RL, // Right to Left 
    TB, // Top to bottom 
    BT, // Bottom to Top 
    None // when no action was detected 
} 


private static final String logTag = "SwipeDetector"; 
private static final int MIN_DISTANCE = 100; 
private float downX, downY, upX, upY; 
private Action mSwipeDetected = Action.None; 


public boolean swipeDetected(){ 
    return mSwipeDetected != Action.None; 
} 

public Action getAction(){ 
    return mSwipeDetected; 
} 



public boolean onTouch(View v, MotionEvent event) { 
    switch (event.getAction()) { 
    case MotionEvent.ACTION_DOWN: { 
     downX = event.getX(); 
     downY = event.getY(); 
     mSwipeDetected = Action.None; 
     return false; // allow other events like Click to be processed 
    } 
    case MotionEvent.ACTION_UP: { 
     upX = event.getX(); 
     upY = event.getY(); 

     float deltaX = downX - upX; 
     float deltaY = downY - upY; 

     // horizontal swipe detection 
       if (Math.abs(deltaX) > MIN_DISTANCE) { 
        // left or right 
        if (deltaX < 0) { 
         Log.i(logTag, "Swipe Left to Right"); 
         mSwipeDetected = Action.LR; 
         return false; 
        } 
        if (deltaX > 0) { 
         Log.i(logTag, "Swipe Right to Left"); 
         mSwipeDetected = Action.RL; 
         return false; 
        } 
       } else 

       // vertical swipe detection 
       if (Math.abs(deltaY) > MIN_DISTANCE) { 
        // top or down 
        if (deltaY < 0) { 
         Log.i(logTag, "Swipe Top to Bottom"); 
         mSwipeDetected = Action.TB; 
         return false; 
        } 
        if (deltaY > 0) { 
         Log.i(logTag, "Swipe Bottom to Top"); 
         mSwipeDetected = Action.BT; 
         return false; 
        } 
       } 
       return false; 
    } 
    } 
    return false; 
} 

}

プログラムを作成するために追加するものdダブルタップを見ますか? 私はスワイプクラスに次を追加しようとしました:

public boolean onDoubleTap(MotionEvent e) { 
    float x = e.getX(); 
    float y = e.getY(); 

    Log.d("Double Tap", "Tapped at: (" + x + "," + y + ")"); 

    return true; 
} 

しかしイムメインクラス内でのリスナーを追加する方法がわからない...

この私だけ「のリストにエラーが発生します.setOnDoubleTapListener」と "新onDoubleTapListener" ..

list.setOnDoubleTapListener(new OnDoubleTapListener(){ 
       public boolean onDoubleTap(MotionEvent e) { 

        return false; 
       } 
       public boolean onDoubleTapEvent(MotionEvent e) { 
        // viewA.setText("-" + "onDoubleTapEvent" + "-"); 
        return false; 
       } 
       public boolean onSingleTapConfirmed(MotionEvent e) { 
        //viewA.setText("-" + "onSingleTapConfirmed" + "-"); 
        return false; 
       } 

     }); 

Z.

を助けてください10

答えて

1

上記問題に対する解決策:

追加さスワイプ検出クラスに次ActionDown

case MotionEvent.ACTION_DOWN: { 

     downX = event.getX(); 
     downY = event.getY(); 
     mSwipeDetected = Action.None; 


     long thisTime = System.currentTimeMillis(); 

     if (thisTime - lastTouchTime < 250) { 

      // Double click 
     // p = mapView.getProjection().fromPixels((int) e.getX(), (int) e.getY()); 
      lastTouchTime = -1; 
      Log.i(logTag, "DOUBLE TAP"); 
     } else { 
      // too slow 
      lastTouchTime = thisTime; 
     } 


     return false; // allow other events like Click to be processed 

    } 
private static final int MIN_DISTANCE = 100; private float downX, downY, upX, upY; private Action mSwipeDetected = Action.None; private long lastTouchTime = -1;

追加され、これを

関連する問題