Android 如何在片段内实现水平滑动?

Android 如何在片段内实现水平滑动?,android,android-viewpager,android-gesture,Android,Android Viewpager,Android Gesture,我在一个活动中有两个片段。我想在其中一个片段中实现滑动。我的布局是: <?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:bac

我在一个活动中有两个片段。我想在其中一个片段中实现滑动。我的布局是:

<?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\u viewer\u fragment
fragment中实现滑动。我正在寻找一种使用
ViewPager
GestureListener
的方法。对于没有片段的正常活动,我可以使用这种方法。

我尝试在我的应用程序中实现滑动功能,为此我创建了一个类
swipedtector
,它实现了
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;
    }

}

在片段中滑动的主要目的是什么?@RaviSharma该活动由片段分为两部分,我只想在一个片段中检测滑动手势。如果用户向左/向右滑动,我将执行一些操作。您可以在
Fragment
中的
view
中添加
setOnTouchListener
,并在
Fragment
中的
onTouch
方法中检测滑动,然后调用父级
活动
来更改
片段
。此外,您还必须更改xml中的静态片段,以使用
FrameLayout
,以便您可以更改父活动中的片段。我尝试了此操作,但我的侦听器的方法没有受到影响。
SwipeDetector swipeDetector = new SwipeDetector();
view.setOnTouchListener(swipeDetector);

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