2013-04-16 2 views
4

나는 활동이있는 두 개의 단편을 가지고있다. 그 조각 중 하나에서 스 와이프를 구현하고 싶습니다. 내 레이아웃은 다음과 같습니다조각 내에서 가로 스 와이프를 구현하는 방법은 무엇입니까?

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#ffffff" 
    android:orientation="horizontal" > 

    <fragment 
     android:id="@+id/image_list_fragment" 
     android:name="com.example.fragments.QGridFragment" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="100" > 
    </fragment> 

    <fragment 
     android:id="@+id/image_viewer_fragment" 
     android:name="com.example.fragments.QViewerFragment" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="85" /> 

</LinearLayout> 

내가 image_viewer_fragment 조각에 슬쩍을 구현하려는. ViewPager 또는 GestureListener을 사용하는 방법을 찾고 있습니다. 그것에 파편이없는 정상적인 활동을 위해서는 this 접근법을 사용할 수 있습니다.

+0

은 무엇 조각에 슬쩍의 주요 목적이 될 것인가? –

+0

@RaviSharma 활동이 조각으로 두 부분으로 나뉘어져 있습니다. 한 조각에서만 스 와이프 제스처를 감지하고 싶습니다. 사용자가 왼쪽/오른쪽으로 스 와이프하면 어떤 동작을 수행 할 것입니다. – Jaguar

+0

'Fragment'의'view'에'setOnTouchListener'를 추가하고'Fragment'의'onTouch' 메소드에서 스 와이프를 감지 한 다음 부모'Activity'를 호출하여'Fragment'를 변경할 수 있습니다. 또한 XML에서 정적 조각을 변경하여 'FrameLayout'을 사용하여 프래그먼트를 부모 작업에서 변경할 수있게해야합니다. – Naveen

답변

2

내 응용 프로그램에서 스 와이프 기능을 구현하려고 시도한 결과 OnTouchListener을 구현하는 SwipeDetector 클래스를 만들었습니다. 당신이 슬쩍 작업을 추가 뷰의 setOnTouchListener 방법을 설정하여 SwipeDetector을 구현하고자하는 조각의

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

public class SwipeDetector implements 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 
     Click 
    } 

    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; 
      // Log.i(logTag, "Click On List"); 
      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) { 

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

        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; } } 
      */ 

      mSwipeDetected = Action.Click; 
      return false; 
     } 
     } 
     return false; 
    } 

} 

onCreateView에서 방법 : 아래

는 코드입니다. 이처럼

:

SwipeDetector swipeDetector = new SwipeDetector(); 
view.setOnTouchListener(swipeDetector); 

if (swipeDetector.getAction() == Action.LR) { 
    //Do some action 
} 
+2

이 시도했지만 내 청취자의 메서드를 적중되지 않습니다. –

관련 문제