在右侧滑动时打开所有活动的活动

时间:2017-10-27 04:04:50

标签: android android-activity layout scroll swipe

我有一个仪表板活动。我希望从我的应用程序的任何屏幕(从任何活动)打开此活动,直接在屏幕上滑动。如果某个屏幕有水平滚动,则滚动将完成,然后仪表板将打开。我不想实施滑动每一个活动。都会提出一些想法

1 个答案:

答案 0 :(得分:0)

您可以通过调用intent来实现。

把这个类全局包。

public class OnSwipeTouchListener implements OnTouchListener {

private final GestureDetector gestureDetector;

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

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

private final class GestureListener extends SimpleOnGestureListener {

    private static final int SWIPE_THRESHOLD = 1;
    private static final int SWIPE_VELOCITY_THRESHOLD = 1;

    @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)) {
                if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffX > 0) {
                        onSwipeRight();
                    } else {
                        onSwipeLeft();
                    }
                }
                result = true;
            } else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                if (diffY > 0) {
                    onSwipeBottom();
                } else {
                    onSwipeTop();
                }
            }
            result = true;

        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }
}

public void onSwipeRight() {
}

public void onSwipeLeft() {
}

public void onSwipeTop() {
}

public void onSwipeBottom() {
}}

并将该课程用于启用仪表板活动所需的每项活动

例如:sampleActivity.class

switch_action.setOnTouchListener(new OnSwipeTouchListener(Camera.this) {

        @Override
        public void onSwipeRight() {
            super.onSwipeRight();
            startActivity(new Intent(sampleActivity.this,Dashboard.class));
        }
    });

switchAction是透明的相对布局,您需要在示例活动约束中使用

<RelativeLayout
    android:id="@+id/switch_action"
    android:background="@android:color/transparent"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

所以一旦用户在屏幕上滑动,就会调用switchaction,你可以根据自己的需要启动start ..