2013-07-08 120 views
26

列表視圖項目可以響應向左滑動並向右滑動嗎?列表視圖項向左滑動並向右滑動?

如果是這樣,如何應用android滑動左右滾動來打開不同的意圖?我想要默認情況下在android中的聯繫人視圖中調用電話或消息。

感謝

+0

可能,這將幫助:http://stackoverflow.com/questions/4139288/android-how-to-handle右向左滑動手勢 – g00dy

+1

請注意您所談論的聯繫人事項僅適用於三星手機。 –

回答

52

試試這個:

navigaList.setOnTouchListener(swipeDetector); 
navigaList.setOnItemClickListener(listener); 

OnItemClickListener listener = new OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, 
          long arg3) { 
     if(swipeDetector.swipeDetected()) { 
      if(swipeDetector.getAction() == Action.RL) { 

      } else { 

      } 
     }   
    }; 

SwipeDetector.java

public class SwipeDetector 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_MOVE: { 
      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) { 
        Logger.show(Log.INFO,logTag, "Swipe Left to Right"); 
        mSwipeDetected = Action.LR; 
        return true; 
       } 
       if (deltaX > 0) { 
        Logger.show(Log.INFO,logTag, "Swipe Right to Left"); 
        mSwipeDetected = Action.RL; 
        return true; 
       } 
      } else 

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

爲什麼你不分享所有的源代碼? – mehmetakifalp

0

我想你可以通過MotionEvents使用OnTouchListener和捕捉填充,初始和當前運動運動名錄的項目,如ACTION_DOWNACTION_MOVEUPCANCEL

相關問題