如何从 class 创建界面

How to create an interface from a class

我是 Android 编程新手/ Java 我想知道当我已经 class 扩展到 MainActvity 时我应该如何添加手势指示?

    package #####app.myapplication;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.view.MotionEvent;
import android.view.GestureDetector;
import android.support.v4.view.GestureDetectorCompat;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener,GestureDetector.OnDoubleTapListener{

    private GestureDetectorCompat mygesture;
    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;

    @Override
    public boolean onSingleTapConfirmed(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public boolean onDoubleTap(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public boolean onDown(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent motionEvent) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent motionEvent) {

    }



       @Override
        public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
            }
        @Override
        public void onPointerCaptureChanged(boolean hasCapture) {

        }


        @Override
        public boolean onTouchEvent(MotionEvent event) {
            this.mygesture.onTouchEvent(event);
            return super.onTouchEvent(event);
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            Context context = getApplicationContext();
            CharSequence text = "Welcome to ####";
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, text, duration);
            toast.show();

            WebView vedant = (WebView) findViewById(R.id.vedant);
            vedant.loadUrl("http://##.ml");
            WebSettings webSettings = vedant.getSettings();
            webSettings.setJavaScriptEnabled(true);
            vedant.setWebViewClient(new WebViewClient());
            this.mygesture = new GestureDetectorCompat(this,this);
            mygesture.setOnDoubleTapListener(this);



        }
    }

我知道要使用手势方向,我需要:

@Override
    public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
        float velocityY) {
            try {
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH){
                    return false;
                }
                // right to left swipe
                if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    onLeftSwipe();
                }
                // left to right swipe
                else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    onRightSwipe();
                }
            } catch (Exception e) {

            }
            return false;
        }
        return false;
    }

但为此我需要扩展另一个 class SimpleOnGestureListener。 由于我无法扩展到 SimpleOnGestureListener class 我想我可以创建一个 interface 然后实现它。那可能吗。如果有怎么办?

如果我没理解错的话,你有一个带有 2 个参数的 GestureDetectorCompat

  • 背景
  • 一位听众 (GestureDetector.OnGestureListener)

目前您 mygesture = new GestureDetectorCompat(this,this);,其中 this 是您的 Activity。但是为什么你想让你的 Activity 成为倾听者呢?您可以在单独的 class.

中创建侦听器

所以你可以这样做:

主要Activity

this.mygesture = new GestureDetectorCompat(this, new MyGestureListener());

MyGestureListener

public class MyGestureListener extends SimpleOnGestureListener {

    @Override
    public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
       // ... 
       // Your code
    }
}