简体   繁体   English

方法内的手势检测

[英]Gesture Detection within a method

Hey all so I'm running into an issue where I need to be able to use a gesture detector within a method so I can call upon a method within said method I'll paste the code but basically it can't reach my inner statement therefore it's throwing an error! 嘿,我遇到了一个问题,我需要能够在方法中使用手势检测器,因此我可以在上述方法中调用一个方法,我将粘贴代码,但基本上无法到达我的内部语句因此会引发错误!

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;
View.OnTouchListener gestureListener;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    gestureDetector = new GestureDetector(this, new MyGestureDetector());
    gestureListener = new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            return gestureDetector.onTouchEvent(event);
        }
    };

Then when I go to call the class to use the gesture detector: 然后,当我去调用类以使用手势检测器时:

public void notification() {
    final AchievementUnlocked achievementUnlocked = new AchievementUnlocked(MainActivity.this)
    achievementUnlocked.getAchievementView().setOnTouchListener(gestureListener);
    class MyGestureDetector extends GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            try {
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;
                // left to right swipe
                if(e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                achievementUnlocked.dismiss();
                }
                // right to left swipe
                else if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {

                }
            } catch (Exception e) {
                // nothing
            }
            return false;
        }

If I do this i get an error in this part of the code Cannot Resolve Symbol 'MyGestureDetector': 如果执行此操作,则会在代码无法解析符号“ MyGestureDetector”的这一部分出现错误:

 gestureDetector = new GestureDetector(this, new MyGestureDetector());

You cannot declare a regular Java class inside of a method. 您不能在方法内部声明常规Java类。 Move MyGestureDetector outside of the notification() method. MyGestureDetector notification()方法之外。

MyGestureDetector外部,并将在其中调用的方法更改为静态(例如achievementUnlocked.dismiss();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM