在刷卡时结束活动吧?

Ste*_*lla 24 android swipe ontouchlistener android-activity

Activity当用户在屏幕的任何位置提供正确的滑动时,我必须完成.我已经尝试过,GestureDetector如果在其他视图中既不存在ScrollView又不RescyclerView存在也不允许检测滑动它们,那就可以正常工作.所以我尝试了一种不同的方式,通过以编程方式将视图叠加到所有这些视图的顶部,然后尝试检测其上的滑动事件.ActivityonClickListener

private void swipeOverToExit(ViewGroup rootView) {

        OverlayLayout child = new OverlayLayout(this);

        ViewGroup.LayoutParams layoutParams =
                new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        child.setLayoutParams(layoutParams);

        rootView.addView(child);

}
Run Code Online (Sandbox Code Playgroud)

OverlayLayout

public class OverlayLayout extends RelativeLayout {

    private float x1, x2;
    private final int MIN_DISTANCE = 150;

    public OverlayLayout(Context context) {
        super(context);
    }

    public OverlayLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public OverlayLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public OverlayLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        /*
         * This method JUST determines whether we want to intercept the motion.
         * If we return true, onTouchEvent will be called and we do the actual
         * logic there.
         */

        final int action = MotionEventCompat.getActionMasked(event);

        Logger.logD("Intercept===", action + "");


        // Always handle the case of the touch gesture being complete.
        if (action == MotionEvent.ACTION_DOWN) {
            return true; // Intercept touch event, let the parent handle swipe
        }

        Logger.logD("===", "Out side" + action + "");


        // In general, we don't want to intercept touch events. They should be
        // handled by the child view.
        return false;
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                x1 = event.getX();
                break;
            case MotionEvent.ACTION_UP:

                x2 = event.getX();
                float deltaX = x2 - x1;

                if (Math.abs(deltaX) > MIN_DISTANCE) {

                    Logger.logD("Swipe Right===", MIN_DISTANCE + "");
                    return true;

                } else {

                    Logger.logD("Tap===", "Tap===");
                    return super.onTouchEvent(event);
                }
        }

        return true;

    }
}
Run Code Online (Sandbox Code Playgroud)

如果滑动动作执行超过OverlayLayout然后进一步结束,则逻辑是拦截触摸事件到其他视图Activity.但是,现在我可以检测到滑动事件,OverlayLayout但是其他视图无法响应,即使我已经返回 return super.onTouchEvent(event);了其他条件,onTouchEvent因为你可以在我的代码中找到它.任何人请帮助我做到.我寄托在这里,非常兴奋地学习这个技巧:)

Hit*_*ahu 17

您尝试做的基本上是Android Wear中的默认行为,它被视为Android Watches退出应用程序的标准做法.在Android中,DismissOverlayView为您提供所有繁重的服务.

智能手机有后退按钮,而Wear则依靠长按或滑动消除模式来退出屏幕.你应该在背压时解雇活动,Android智能手机中的混合磨损模式会让用户感到困惑.至少显示一个警告对话框,以避免意外退出.

我看到这个问题是用Android标记的,Activity我建议你制作一个基本活动,它将处理finish()从左到右滑动的滑动手势和自身.

基本活动类应如下所示: -

   public abstract class SwipeDismissBaseActivity extends AppCompatActivity {
    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;
    private GestureDetector gestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        gestureDetector = new GestureDetector(new SwipeDetector());
    }

    private class SwipeDetector extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

            // Check movement along the Y-axis. If it exceeds SWIPE_MAX_OFF_PATH,
            // then dismiss the swipe.
            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                return false;

            // Swipe from left to right.
            // The swipe needs to exceed a certain distance (SWIPE_MIN_DISTANCE)
            // and a certain velocity (SWIPE_THRESHOLD_VELOCITY).
            if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                finish();
                return true;
            }

            return false;
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        // TouchEvent dispatcher.
        if (gestureDetector != null) {
            if (gestureDetector.onTouchEvent(ev))
                // If the gestureDetector handles the event, a swipe has been
                // executed and no more needs to be done.
                return true;
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }
}
Run Code Online (Sandbox Code Playgroud)

现在你可以让其他活动扩展这个基本活动,他们Inheritance将自动使他们采用滑动来解除行为.

public class SomeActivity extends SwipeDismissBaseActivity {
Run Code Online (Sandbox Code Playgroud)

这种方式的优点

  • 纯粹的OOPS方法
  • 清洁代码 - 无需在项目中使用的每种类型的布局中编写滑动侦听器(相对,线性等)
  • 在ScrollView中完美地工作

  • 随着投掷工作完美.任何想法如何拖动活动与手指和解雇? (3认同)