ViewPager禁用滑动

时间:2016-03-07 20:54:06

标签: java android listview android-fragments

我有ViewPager,有3个片段。片段A在左边,B在中间,C在右边。片段C有一个ListView,它填满了屏幕的整个宽度。我使用以下代码在ListView项目上实现了一个滑动侦听器:

SWIPE DETECTOR:

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 // when no action was detected
}



private static final String logTag = "SwipeDetector";
private static final int MIN_DISTANCE = 100;
private static final int VERTICAL_MIN_DISTANCE = 80;
private static final int HORIZONTAL_MIN_DISTANCE = 80;
private float downX, downY, upX, upY;
private Action mSwipeDetected = Action.None;

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

public Action getAction() {
    return mSwipeDetected;
}

@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) {
                   // Log.i(logTag, "Swipe Left to Right");
                    mSwipeDetected = Action.LR;
                    return true;
                }
                if (deltaX > 0) {
                   // Log.i(logTag, "Swipe Right to Left");
                    mSwipeDetected = Action.RL;
                    return true;
                }
            } else

                // vertical swipe detection
                if (Math.abs(deltaY) > VERTICAL_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;
                    }
                }
            return true;
        }
    }
    return false;
}}

我以下列方式使用它:

        listView.setOnTouchListener(swipeDetector);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                                final int position, long id) {
            Log.d("CLICKED", "CLICKED");
            if (swipeDetector.swipeDetected()) {
                Log.d("SWIPING", "SWIPING");
                Log.d("ACTION", swipeDetector.getAction().toString());
                final Button del = (Button) view.findViewById(R.id.delete_button);
                if (swipeDetector.getAction() == SwipeDetector.Action.LR) {
                    Log.d("LEFT TO RIGHT", "Left to right");

这与活动完美搭配。但是,现在的问题是,当我滑动时,它假设我在ViewPager中滑动并将我带回中间片段。有没有办法在此ListView上禁用ViewPager滑动或更改焦点以使其有效?

0 个答案:

没有答案