Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 右击“所有活动”打开一个“活动”_Android_Android Activity_Layout_Scroll_Swipe - Fatal编程技术网

Android 右击“所有活动”打开一个“活动”

Android 右击“所有活动”打开一个“活动”,android,android-activity,layout,scroll,swipe,Android,Android Activity,Layout,Scroll,Swipe,我有一个仪表盘活动。我希望此活动从我的应用程序的任何屏幕(从任何活动)在屏幕上右键滑动打开。如果某个屏幕有水平滚动,则滚动将完成,然后仪表板将打开。我不希望在每个活动中都执行滑动。请给出一些想法。您可以通过调用intent来完成 把这个类放在全局包中 public class OnSwipeTouchListener implements OnTouchListener { private final GestureDetector gestureDetector; public OnSwi

我有一个仪表盘活动。我希望此活动从我的应用程序的任何屏幕(从任何活动)在屏幕上右键滑动打开。如果某个屏幕有水平滚动,则滚动将完成,然后仪表板将打开。我不希望在每个活动中都执行滑动。请给出一些想法。

您可以通过调用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,您可以根据需要启动Tactivity。

Ashokkumar Adichili,正如我所说,我不能在每个活动中添加此代码,因为我有很多活动。有没有办法在我的应用程序屏幕的顶部窗口上获取滑动事件