Android向左和向右交换以更改活动/片段

时间:2015-10-09 07:56:29

标签: android

我正在构建一个具有以下导航逻辑的自定义Home Launcher: enter image description here

向右和向左滑动我认为可以使用ViewPager轻松实现,因此中心Activity是ViewPager,当用户向左或向右滑动时,将加载新的Fragment。但是我怎样才能在同一个Activity中实现Swipe Up和Down? 实际上我需要的是一个多方向的ViewPager。它存在吗?

2 个答案:

答案 0 :(得分:1)

有一天,我在互联网上找到了一个允许这样做的刷卡听众:

public class OnSwipeTouchListener implements OnTouchListener {
    // Fields:
    /** Whether a swipe motion has been detected */
    protected boolean isSwipeDetected = false;
    private final GestureDetector gestureDetector;


    // Constructors:
    public OnSwipeTouchListener (Context ctx) {
        gestureDetector = new GestureDetector(ctx, new GestureListener());
    }

    private final class GestureListener extends SimpleOnGestureListener {
        private static final int HORIZONTAL_SWIPE_THRESHOLD = 0;
        private static final int HORIZONTAL_SWIPE_VELOCITY_THRESHOLD = 0;
        private static final int VERTICAL_SWIPE_THRESHOLD = 100;
        private static final int VERTICAL_SWIPE_VELOCITY_THRESHOLD = 100;

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            boolean result = false;
            try {
                float diffY = e2.getY() - e1.getY();
                float diffX = e2.getX() - e1.getX();
                if (Math.abs(diffX) > Math.abs(diffY)) {    // More of a horizontal movement
                    if (Math.abs(diffX) > HORIZONTAL_SWIPE_THRESHOLD && Math.abs(velocityX) > HORIZONTAL_SWIPE_VELOCITY_THRESHOLD) {
                        if (diffX > 0) {
                            isSwipeDetected = true;
                            onSwipeRight();
                        } else {
                            isSwipeDetected = true;
                            onSwipeLeft();
                        }
                    }
                    result = true;
                } else {    // Vertical movement
                    if (Math.abs(diffY) > VERTICAL_SWIPE_THRESHOLD && Math.abs(velocityY) > VERTICAL_SWIPE_VELOCITY_THRESHOLD) {
                        if (diffY > 0) {
                            isSwipeDetected = true;
                            onSwipeBottom();
                        } else {
                            isSwipeDetected = true;
                            onSwipeTop();
                        }
                    }
                    result = true;
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }

            return result;
        }
    }

    public void onSwipeRight() {
    }

    public void onSwipeLeft() {
    }

    public void onSwipeTop() {
    }

    public void onSwipeBottom() {
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        boolean isSwipe = gestureDetector.onTouchEvent(event);

        return isSwipe;
    }
}

将其设置为主要活动布局的OnTouchListener,并在onSwipe *方法中实现您的逻辑。

答案 1 :(得分:0)

I found a Solution you can add onTouchListener in your View:-


footerView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_UP) {
                     Intent intent = new Intent(this, OtherActivity.class);
                    intent.putExtra(AppConstants.EXTRAS.MODE, Mode.ONLINE);
                    intent.putExtra(AppConstants.EXTRAS.NOTES_DATA, notesDataModel);
                    intent.putExtra(AppConstants.EXTRAS.CONTENT_DATA, contentListModel);
                    startActivity(intent);


                    }
                    return true;
                }
                return false;
            }
        });