2013-07-27 24 views
6

tôi đang làm việc trên hoạt động bằng cử chỉ trong android Tôi đã từng lớp để phát hiện các hành động swipe làAndroid làm thế nào để thêm swipe cử chỉ trên LinearLayout mà không onDown đúng

public class ActivitySwipeDetector implements View.OnTouchListener { 

    static final String logTag = "ActivitySwipeDetector"; 
    private Activity activity; 
    static final int MIN_DISTANCE = 100; 
    private float downX, downY, upX, upY; 

    public ActivitySwipeDetector(Activity activity){ 
     this.activity = activity; 
    } 

    public void onRightToLeftSwipe(){ 
     Log.i(logTag, "RightToLeftSwipe!"); 
     Toast.makeText(activity, "RightToLeftSwipe", Toast.LENGTH_SHORT).show(); 
     //activity.doSomething(); 
    } 

    public void onLeftToRightSwipe(){ 
     Log.i(logTag, "LeftToRightSwipe!"); 
     Toast.makeText(activity, "LeftToRightSwipe", Toast.LENGTH_SHORT).show(); 
     //activity.doSomething(); 
    } 

    public void onTopToBottomSwipe(){ 
     Log.i(logTag, "onTopToBottomSwipe!"); 
     //Toast.makeText(activity, "onTopToBottomSwipe", Toast.LENGTH_SHORT).show(); 
     //activity.doSomething(); 
    } 

    public void onBottomToTopSwipe(){ 
     Log.i(logTag, "onBottomToTopSwipe!"); 
     //Toast.makeText(activity, "onBottomToTopSwipe", Toast.LENGTH_SHORT).show(); 
     //activity.doSomething(); 
    } 

    public boolean onTouch(View v, MotionEvent event) { 
     switch(event.getAction()){ 
     case MotionEvent.ACTION_DOWN: { 
      downX = event.getX(); 
      downY = event.getY(); 
      return true; 
     } 
     case MotionEvent.ACTION_UP: { 
      upX = event.getX(); 
      upY = event.getY(); 

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

      // swipe horizontal? 
        if(Math.abs(deltaX) > MIN_DISTANCE){ 
         // left or right 
         if(deltaX < 0) { this.onLeftToRightSwipe(); return true; } 
         if(deltaX > 0) { this.onRightToLeftSwipe(); return true; } 
        } 
        else { 
         Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE); 
         return false; // We don't consume the event 
        } 

        // swipe vertical? 
        if(Math.abs(deltaY) > MIN_DISTANCE){ 
         // top or down 
         if(deltaY < 0) { this.onTopToBottomSwipe(); return true; } 
         if(deltaY > 0) { this.onBottomToTopSwipe(); return true; } 
        } 
        else { 
         Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE); 
         return false; // We don't consume the event 
        } 

        return true; 
     } 
     } 
     return false; 
    } 

} 

file xml của tôi là

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/action_settings" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/hello_world" /> 

</LinearLayout> 

Và trong Hoạt động của tôi, tôi gọi như thế này

ActivitySwipeDetector activitySwipeDetector = new ActivitySwipeDetector(this); 
     LinearLayout lowestLayout = (LinearLayout)this.findViewById(R.id.action_settings); 
     lowestLayout.setOnTouchListener(activitySwipeDetector); 

Tôi gọi là ActivitySwipeDetector(this); nên nó hoạt động tốt với đầy đủ ctivity. Nhưng làm thế nào tôi có thể áp dụng máy dò Swipe chỉ để LinearLayout không cho toàn bộ hoạt động. Hãy giúp tôi ra.

+0

Bạn đang gắn trình xử lý cảm ứng cho một chế độ xem cụ thể. Quyết định xem những gì bạn muốn phát hiện swipes trên, và đính kèm nó vào xem thích hợp. Nếu bạn đính kèm nó vào chế độ xem cấp cao nhất trong hoạt động như bạn đã làm ở đây, nó sẽ hoạt động trên toàn bộ hoạt động. –

+0

Nhưng nếu tôi đính kèm xem con của nó không hoạt động ... –

Trả lời

18

cần thiết cho tôi và tốt nhất là mã của bạn, vì vậy tôi cố định một chút, cần sửa chữa hơn, hãy xem các bình luận

public class RelativeLayoutTouchListener implements OnTouchListener { 

    static final String logTag = "ActivitySwipeDetector"; 
    private Activity activity; 
    static final int MIN_DISTANCE = 100;// TODO change this runtime based on screen resolution. for 1920x1080 is to small the 100 distance 
    private float downX, downY, upX, upY; 

    // private MainActivity mMainActivity; 

    public RelativeLayoutTouchListener(MainActivity mainActivity) { 
     activity = mainActivity; 
    } 

    public void onRightToLeftSwipe() { 
     Log.i(logTag, "RightToLeftSwipe!"); 
     Toast.makeText(activity, "RightToLeftSwipe", Toast.LENGTH_SHORT).show(); 
     // activity.doSomething(); 
    } 

    public void onLeftToRightSwipe() { 
     Log.i(logTag, "LeftToRightSwipe!"); 
     Toast.makeText(activity, "LeftToRightSwipe", Toast.LENGTH_SHORT).show(); 
     // activity.doSomething(); 
    } 

    public void onTopToBottomSwipe() { 
     Log.i(logTag, "onTopToBottomSwipe!"); 
     Toast.makeText(activity, "onTopToBottomSwipe", Toast.LENGTH_SHORT).show(); 
     // activity.doSomething(); 
    } 

    public void onBottomToTopSwipe() { 
     Log.i(logTag, "onBottomToTopSwipe!"); 
     Toast.makeText(activity, "onBottomToTopSwipe", Toast.LENGTH_SHORT).show(); 
     // activity.doSomething(); 
    } 

    public boolean onTouch(View v, MotionEvent event) { 
     switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: { 
      downX = event.getX(); 
      downY = event.getY(); 
      return true; 
     } 
     case MotionEvent.ACTION_UP: { 
      upX = event.getX(); 
      upY = event.getY(); 

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

      // swipe horizontal? 
      if (Math.abs(deltaX) > MIN_DISTANCE) { 
       // left or right 
       if (deltaX < 0) { 
        this.onLeftToRightSwipe(); 
        return true; 
       } 
       if (deltaX > 0) { 
        this.onRightToLeftSwipe(); 
        return true; 
       } 
      } else { 
       Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long horizontally, need at least " + MIN_DISTANCE); 
       // return false; // We don't consume the event 
      } 

      // swipe vertical? 
      if (Math.abs(deltaY) > MIN_DISTANCE) { 
       // top or down 
       if (deltaY < 0) { 
        this.onTopToBottomSwipe(); 
        return true; 
       } 
       if (deltaY > 0) { 
        this.onBottomToTopSwipe(); 
        return true; 
       } 
      } else { 
       Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long vertically, need at least " + MIN_DISTANCE); 
       // return false; // We don't consume the event 
      } 

      return false; // no swipe horizontally and no swipe vertically 
     }// case MotionEvent.ACTION_UP: 
     } 
     return false; 
    } 

} 

Cách sử dụng:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    rlTop = (RelativeLayout) findViewById(R.id.rlTop);  
    rlTop.setOnTouchListener(new RelativeLayoutTouchListener(this)); 
} 
+0

Tôi did't có được bạn, nơi bạn chính xác bị đánh ??? –

+0

Tôi vừa chỉnh sửa mã của bạn, hãy kiểm tra mã đó .. Tôi nghĩ rằng đã xong với sự cố của bạn .. –

+0

+1 nó đã giúp tôi cảm ơn :) –

0

tôi đã có kết quả tốt với bản gốc mã. Nhưng người nghe cần phải được thêm vào chế độ xem chứ không phải bố cục. Chúng tôi đang mở rộng View.OnTouchListener, sau khi tất cả.

+1

LinearLayout mở rộng ViewGroup mở rộng Chế độ xem. Tôi không thấy lý do tại sao người nghe không thể được thêm vào bố cục. – mhenry

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