仅作为背景时,我应该在哪里放置滑动管理器?

时间:2018-12-03 22:38:50

标签: java android android-studio

基本上,我现在仅在一个普通的背景下进行此活动,并且我一直在使用SwipeDetector来管理多个活动,但是现在我只是不知道将功能放置在何处以检测滑动的位置屏幕的 这是管理滑动的类:

    package com.example.user.mkpos;

    import android.app.Activity;
    import android.content.Context;
    import android.renderscript.Script;
    import android.util.Log;
    import android.view.MotionEvent;
    import android.view.View;
    import android.widget.Toast;


    public class ActivitySwipeDetector implements View.OnTouchListener {

        static final String logTag = "ActivitySwipeDetector";
        private Activity activity;
        static final int MIN_DISTANCE = 100;
        private float downX, downY, upX, upY;
        private SwipeCallback swipeCallback;

        public ActivitySwipeDetector(Activity activity, SwipeCallback callback){
            this.activity = activity;
            swipeCallback = callback;
        }

        public interface SwipeCallback {
            void onRightSwipe();
            void onLeftSwipe();
            void onDownSwipe();
            void onUpSwipe();
        }

        public void onRightSwipe(){
            swipeCallback.onRightSwipe();
        }

        public void onLeftSwipe(){
            swipeCallback.onLeftSwipe();
        }

        public void onDownSwipe(){
            swipeCallback.onDownSwipe();
        }

        public void onUpSwipe(){
            swipeCallback.onUpSwipe();
        }

        public boolean onTouch(View v, MotionEvent event) {
            switch(event.getAction()){
                case MotionEvent.ACTION_DOWN: {
                    downX = event.getX();
                    downY = event.getY();
                    return true;
                }
                case MotionEvent.ACTION_UP: {
                    upX = event.getX();
                    upY = event.getY();

                    float deltaX = downX - upX;
                    float deltaY = downY - upY;

                    // swipe horizontal?
                    if(Math.abs(deltaX) > Math.abs(deltaY))
                    {
                        if(Math.abs(deltaX) > MIN_DISTANCE){
                            // left or right
                            if(deltaX > 0) { this.onRightSwipe(); return true; }
                            if(deltaX < 0) { this.onLeftSwipe(); return true; }
                        }
                        else {
                            Log.i(logTag, "Horizontal Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE);
                            return false; // We don't consume the event
                        }
                    }
                    // swipe vertical?
                    else
                    {
                        if(Math.abs(deltaY) > MIN_DISTANCE){
                            // top or down
                            if(deltaY < 0) { this.onDownSwipe(); return true; }
                            if(deltaY > 0) { this.onUpSwipe(); return true; }
                        }
                        else {
                            Log.i(logTag, "Vertical Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE);
                            return false; // We don't consume the event
                        }
                    }
                    return true;
                }
            }
            return false;
        }

}

这就是我在“活动”中的称呼:

package com.example.user.mkpos;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class PaymentMethod extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,
                WindowManager.LayoutParams. FLAG_FULLSCREEN);
        setContentView(R.layout.activity_method);
    }

    //Actividades para los Swipes correspondientes
    ActivitySwipeDetector activitySwipeDetector = new ActivitySwipeDetector(
            PaymentMethod.this,
            new ActivitySwipeDetector.SwipeCallback() {
                //OnRightSwipe de Derecha a Izquierda, elimina el dato
                @Override
                public void onRightSwipe() {
                    Intent mcrIntent = new Intent(PaymentMethod.this, PaymentMCR.class);
                    startActivity(mcrIntent);
                    overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
                }

                @Override
                public void onLeftSwipe() {
                    Intent mcrIntent = new Intent(PaymentMethod.this, PaymentICC.class);
                    startActivity(mcrIntent);
                    overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
                }

                @Override
                public void onUpSwipe() {
                }

                @Override
                public void onDownSwipe() {
                }
            }
    );

}

它对我的代码没有任何作用,我尝试将其放置在OnCreate中,但仍然一无所获,我对此很生气。

0 个答案:

没有答案