2013-04-16 44 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> 

我想執行swipe image_viewer_fragment片段。我正在尋找使用ViewPagerGestureListener的方法。對於沒有碎片的正常活動,我可以使用this的方法。

+0

什麼將在片段刷卡的主要目的是什麼? –

+0

@RaviSharma活動被分成兩部分,我想只在一個片段中檢測滑動手勢。如果用戶左右滑動,我會執行一些操作。 – Jaguar

+0

您可以添加'setOnTouchListener'在'Fragment'的'view'並檢測在'Fragment'在'onTouch'方法輕掃,然後調用父'Activity'改變'Fragment'。你也必須改變xml中的靜態片段來使用'FrameLayout',這樣你才能從父活動中改變片段。 – Naveen

回答

2

我想在我的應用程序來實現刷卡功能,併爲我創建了一個類SwipeDetector它實現OnTouchListener

下面是代碼:

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片段的方法,其中要添加刷卡的動作,通過在視setOnTouchListener方法設置它實現SwipeDetector

像這樣:

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

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

我試過了,但我的聽衆的方法沒有被擊中。 –