OnflingとSimpleongestureListenerのトラブル
-
12-11-2019 - |
質問
次の質問のように、onfling検出器を実装しています。 グリッドレイアウト
しかし、私はそれを仕事に入れることができない。
何らかの方法で私のアプリを設定している場合(多分asynctask?)、私のアプリが構造化されている方法:
(これを素敵にフォーマットされたリストにしようとしましたが、それは何らかの理由でそれを下のコードを隠しましたか?)
- マップアクティブマップビューを作成します。
--mapViewはURLからXMLをフェッチするためのasynctaskを作成します。
asynctaskの--onpostexecute()はXMLを解析し、取得されたデータを使用してItemizizeOverLayを追加します。
--ontap()ItemizedOverLayの機能は、LoaderImageView(http://www.anddev.org/novice-tutorials-f8/imageview-with-loading-spinner-t49439.htmlを使用してWebからイメージを取得します。 "rel=" nofollow noreferrer "> http://www.anddev.org/novice-tutorials-f8/imageview-with-load-spinner-t49439.html
--ontap()関数は次に下にリストされているPopuppanelクラスのshow()関数を呼び出します
以下のコードからわかるように、コメントアウトラインは機能します。ただし、MyMestureDectorクラスと一緒に使用すると、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);
}
}
}
. 解決
これが理由であるならば、私は View.SetOntouchListener GestureListenerのコード内。私はラインが予想されていただろう。
setOnTouchListener(gestureListener)
.
作成した後。
他のヒント
Try returning true
from your MyGestureDetector.onDown
. The superclass implementation is to return false
which then suppresses the delivery of future events until the touch is released.