gpt4 book ai didi

java - SimpleOnGestureListener 不工作

转载 作者:行者123 更新时间:2023-11-30 00:17:46 27 4
gpt4 key购买 nike

我想通过在我的 MainActivity 中滑动来切换 Activity 。我从 GestureDetectorCompat 声明了一个对象,并在 onCreate 方法中对其进行了初始化,如下所示:

gestureObject = new GestureDetectorCompat(MainActivity.this, new GestureListener());

另外,我像那样覆盖 onTouchEvent:

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

我的 MainActivity 扩展自 AppCompatActivity 并实现了 NavigationView.OnNavigationItemSelectedListener、OnMenuItemClickListener

GestureListener 代码为:

private class GestureListener extends GestureDetector.SimpleOnGestureListener {

@Override
public void onLongPress(MotionEvent e) {
Log.e(TAG, "long pressed");
super.onLongPress(e);
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

Log.e(TAG, "on Flig");

if (e2.getX() > e1.getX()) {

Log.e(TAG, "left to right");
}
return true;
}
}

但它不起作用。

navigationView有关系吗?

我能做什么?

最佳答案

试试这段代码..

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.GestureDetector;
import android.view.MotionEvent;

public class MainActivity extends AppCompatActivity {

private GestureDetectorCompat mDetector;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDetector = new GestureDetectorCompat(MainActivity.this, new SwipeGestureDetector());
}

@Override
public boolean onTouchEvent(MotionEvent motionEvent) {
this.mDetector.onTouchEvent(motionEvent);
return super.onTouchEvent(motionEvent);
}

@Override
public boolean dispatchTouchEvent(@NonNull MotionEvent ev) {
super.dispatchTouchEvent(ev);
return mDetector.onTouchEvent(ev);
}

/**
* Swipe Gesture Detector Class for swipe detector.
*/
public class SwipeGestureDetector extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if(e1 == null || e2 == null)
return false;
if(e1.getPointerCount() > 1 || e2.getPointerCount() > 1)
return false;
else {
try {
float diffX = e2.getX() - e1.getX();
float diffY = e2.getY() - e1.getY();
if(Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > 100 && Math.abs(velocityX) > 1000) {
if ((diffX > 0) || (diffX < 0)) {
// nextActivity();

return true;
}
}
}
} catch (Exception e) {
e.printStackTrace();

}
return false;
}
}
}

关于java - SimpleOnGestureListener 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46925851/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com