2013-04-15 33 views
30

Tôi có một ứng dụng Android trong đó có Danh sách Xem Như thế này mộtxem danh sách Android Phải/trái swipes như nhật ký cuộc gọi

enter image description here

Bây giờ tôi muốn thực hiện hai hoạt động khác nhau ở bên phải/swipe Left of Danh mục tài liệu trong cùng một cách như thế nào mẹ đẻ nhật ký cuộc gọi làm việc

Ngay Vuốt danh sách

enter image description here

Tôi muốn loại swipes này. Có ai biết làm thế nào để làm cho nó xảy ra. Về cơ bản tôi wan để thực hiện SimpleOnGestureListener.

Tôi đã đọc một bài trả lời bởi thưa ông GAVFling gesture detection on grid layout và sau đó tôi thành công trong việc thực hiện swipe trái và phát hiện swipe đúng nhưng điều duy nhất tôi không nhận được trên đó Danh sách mục swipe đã xảy ra.

Cập nhật 24 tháng 5 năm 2013

Bây giờ tôi có thể phát hiện hành động swipe sử dụng mã này -

SwipeDetector.java

/** 
* Class swipe detection to View 
*/ 
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 // Action not found 
    } 

    private static final int HORIZONTAL_MIN_DISTANCE = 30; // The minimum 
    // distance for 
    // horizontal swipe 
    private static final int VERTICAL_MIN_DISTANCE = 80; // The minimum distance 
    // for vertical 
    // swipe 
    private float downX, downY, upX, upY; // Coordinates 
    private Action mSwipeDetected = Action.None; // Last action 

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

    public Action getAction() { 
     return mSwipeDetected; 
    } 

    /** 
    * Swipe detection 
    */@Override 
    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) > HORIZONTAL_MIN_DISTANCE) { 
        // left or right 
        if (deltaX < 0) { 

         mSwipeDetected = Action.LR; 
         return true; 
        } 
        if (deltaX > 0) { 

         mSwipeDetected = Action.RL; 
         return true; 
        } 
       } else 

       // vertical swipe detection 
       if (Math.abs(deltaY) > VERTICAL_MIN_DISTANCE) { 
        // top or down 
        if (deltaY < 0) { 

         mSwipeDetected = Action.TB; 
         return false; 
        } 
        if (deltaY > 0) { 

         mSwipeDetected = Action.BT; 
         return false; 
        } 
       } 
       return true; 
      } 
     } 
     return false; 
    } 

} 

Bây giờ sử dụng nó với ListView của bạn trong thời trang này

// Set the touch listener 
    final SwipeDetector swipeDetector = new SwipeDetector(); 
    lv.setOnTouchListener(swipeDetector); 

Trong bạn setOnItemClickListener bạn có thể phát hiện các sự kiện swipe như thế này

lv.setOnItemClickListener(new OnItemClickListener() { 


    @Override 
    public void onItemClick(AdapterView <? > parent, View view, 
     int position, long id) { 

     if (swipeDetector.swipeDetected()) { 
      if (swipeDetector.getAction() == SwipeDetector.Action.LR) { 

       Toast.makeText(getApplicationContext(), 
        "Left to right", Toast.LENGTH_SHORT).show(); 

      } 
      if (swipeDetector.getAction() == SwipeDetector.Action.RL) { 

       Toast.makeText(getApplicationContext(), 
        "Right to left", Toast.LENGTH_SHORT).show(); 

      } 
     } 
    } 
}); 

Nhưng tôi vẫn không thể để animate swipe như thế này:

enter image description here

+1

có lẽ đây sẽ giúp http://stackoverflow.com/questions/11833504/simple-swipe-gesture- to-activity-tutorial – Elior

+0

@Nikhil Agrawal có u nhận được giải pháp cho –

+0

@Nikhil Agrwal: Tôi đang tìm kiếm tương tự, bạn có giải pháp nào cho vấn đề này không ?? – Jayesh

Trả lời

1

bạn có thể sử dụng mảnh vỡ cho yêu cầu này. Sử dụng FragmentPagerAdapter và vượt qua getItem (vị trí int) sẽ trả về cho bạn mảnh. đặt bộ điều hợp cho chế độ xemPager và trong setOnPageChangeListener bạn có thể có logic để gọi các đoạn khác nhau.

Shrishail

13

OnSwipeTouchListener.java:

import android.view.GestureDetector; 
import android.view.GestureDetector.SimpleOnGestureListener; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 

public class OnSwipeTouchListener implements OnTouchListener { 

    private final GestureDetector gestureDetector = new GestureDetector(new GestureListener()); 

    public boolean onTouch(final View view, final MotionEvent motionEvent) { 

     super.onTouch(view, motionEvent); 
     return gestureDetector.onTouchEvent(motionEvent); 

    } 

    private final class GestureListener extends SimpleOnGestureListener { 

     private static final int SWIPE_THRESHOLD = 100; 
     private static final int SWIPE_VELOCITY_THRESHOLD = 100; 

     @Override 
     public boolean onDown(MotionEvent e) { 
      return true; 
     } 

     @Override 
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 

      boolean result = false; 
      try { 
       float diffY = e2.getY() - e1.getY(); 
       float diffX = e2.getX() - e1.getX(); 
       if (Math.abs(diffX) > Math.abs(diffY)) { 
        if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) { 
         if (diffX > 0) { 
          onSwipeRight(); 
         } else { 
          onSwipeLeft(); 
         } 
        } 
       } else { 
        if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) { 
         if (diffY > 0) { 
          onSwipeBottom(); 
         } else { 
          onSwipeTop(); 
         } 
        } 
       } 
      } catch (Exception exception) { 
       exception.printStackTrace(); 
      } 
      return result; 
     } 
    } 
    public void onSwipeRight() {} 
    public void onSwipeLeft() {} 
    public void onSwipeTop() {} 
    public void onSwipeBottom() {} 
} 

SỬ DỤNG:

imageView.setOnTouchListener(new OnSwipeTouchListener() { 

    public void onSwipeTop() { 
     Toast.makeText(MyActivity.this, "top", Toast.LENGTH_SHORT).show(); 
    } 
    public void onSwipeRight() { 
     Toast.makeText(MyActivity.this, "right", Toast.LENGTH_SHORT).show(); 
    } 
    public void onSwipeLeft() { 
     Toast.makeText(MyActivity.this, "left", Toast.LENGTH_SHORT).show(); 
    } 
    public void onSwipeBottom() { 
     Toast.makeText(MyActivity.this, "bottom", Toast.LENGTH_SHORT).show(); 
    } 

}); 
+0

hmmmm ... Tôi sẽ xem và cho bạn biết nhưng tốt hơn dán mã ở đây chỉ –

+0

Nếu chúng tôi có thể kết hợp DSLV và SwipeListView thì chúng tôi có thể thực hiện vuốt như cuộc gọi trong samsung. – Jayesh

+0

Super.onTouch (xem, motionEvent); trong phương pháp OnTouch. –

Các vấn đề liên quan