gpt4 book ai didi

android - onFling 和 SimpleOnGestureListener 的问题

转载 作者:太空狗 更新时间:2023-10-29 15:49:03 24 4
gpt4 key购买 nike

我正在实现一个 onFling 检测器,很像下面的问题: Fling gesture detection on grid layout

但是,我无法让它工作。

如果我设置我的应用程序的某种方式导致了这个问题(也许是异步任务?),我的应用程序的结构如下:

(试图使它成为一个格式良好的列表,但出于某种原因它搞砸了它下面的代码?)

--MapActivity 创建一个 MapView。

--MapView 创建一个 ASyncTask 以从 URL 获取 XML。

--onPostExecute() 的 ASyncTask 解析 XML 并使用获取的数据添加 ItemizedOverlay

ItemizedOverlay 的 --onTap() 函数使用 LoaderImageView ( http://www.anddev.org/novice-tutorials-f8/imageview-with-loading-spinner-t49439.html ) 从网络上获取图像

--onTap() 函数然后调用下面列出的 PopupPanel 类的 show() 函数

从下面的代码中可以看出,注释掉的行有效。但是,当与 MyGestureDetector 类一起使用时,永远不会触发 onTouch 和 onFling 事件。

class PopupPanel {
View popup;
boolean isVisible = false;
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;
public View.OnTouchListener gestureListener;

PopupPanel(Context context, int layout) {
ViewGroup parent = (ViewGroup) map.getParent();

popup = ((MapActivity) context).getLayoutInflater().inflate(layout, parent, false);

SelectFilterActivity selectFilterActivity = new SelectFilterActivity();
popup.setOnClickListener(selectFilterActivity);

// This works!
// popup.setOnTouchListener(new View.OnTouchListener() {
// public boolean onTouch(View v, MotionEvent event) {
// Toast.makeText(map.getContext(), "Touched", Toast.LENGTH_SHORT).show();
// return false;
// }
// });

// Gesture detection
gestureDetector = new GestureDetector(new MyGestureDetector());
gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
};
}

class MyGestureDetector extends 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;
// right to left swipe
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(map.getContext(), "Left Swipe", Toast.LENGTH_SHORT).show();
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(map.getContext(), "Right Swipe", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
// nothing
}
return false;
}

@Override
public boolean onDown(MotionEvent e) {
return true;
}
}

View getView() {
return (popup);
}

void show(boolean alignTop) {
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);

if (alignTop) {
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp.setMargins(0, 20, 0, 0);
} else {
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
lp.setMargins(0, 0, 0, 60);
}

hide();

((ViewGroup) map.getParent()).addView(popup, lp);
isVisible = true;
}

void hide() {
if (isVisible) {
isVisible = false;
((ViewGroup) popup.getParent()).removeView(popup);
}
}
}

最佳答案

尝试从您的 MyGestureDetector.onDown 返回 true。父类(super class)实现将返回 false,然后在触摸被释放之前抑制 future 事件的传递。

关于android - onFling 和 SimpleOnGestureListener 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5973884/

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