从地图片段中捕获双击 [英] Capture double tap from map fragment
问题描述
我正在尝试使用手势检测器捕获双击事件.
I am trying to capture the double tap event using a gesture detector.
我有一个覆盖框放置在地图片段的顶部,并实现了ontouch和ongesture侦听器.问题是我似乎只能获得一层来捕获事件.如果我将onDown更改为true,则覆盖将消耗所有事件,并且该地图不可用.如果我返回false,则地图也将使用双击事件,并在我不希望它这样做时放大.
I have an overlay frame that is placed on top of my map fragment, and implements ontouch and ongesture listeners. The problem is I can only seem to get one layer to capture the events. If I change onDown to return true the overlay consumes all events and the map is not usable. If I have it return false the map instead also uses the double tap event, and zooms in when I do not want it to do that.
以下是叠加层的相关代码:
Here is the relevant code for the overlay:
@Override
public boolean onDown(MotionEvent e) {
return false;
}
@Override
public void onShowPress(MotionEvent e) {
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return false;
}
@Override
public void onLongPress(MotionEvent e) {
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return false;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
return mDetector.onTouchEvent(event);
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
return false;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
if(mEventListener != null){
mEventListener.onSelect();
}
return true;
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
return false;
}
public interface OnSelectListener{
void onSelect();
}
public void setEventListener(OnSelectListener mEventListener){
this.mEventListener = mEventListener;
}
这是我的布局设置方式:
And here is how my layout is set up:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:name="com.google.android.gms.maps.MapFragment"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/circle_options">
</fragment>
<com.derongan.ambiance.OverlayFrame
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frame_layout">
推荐答案
问题通过做两件事解决了:
The problem was solved by doing two things:
首先,我需要重写onInterceptTouchEvent和onTouchEvent.这样,我的手势检测器既收到了初始事件(未捕获的事件),又收到了后来的捕获事件.
First, I needed to override onInterceptTouchEvent as well as onTouchEvent. This way my gesturedetector was fed both the initial (uncaptured) events, and the later capture ones.
此外,由于事件在android中传播的方式不足以使mapfragment包含在Frame下方的片段中.取而代之的是,需要将帧设为片段的父级,以使拦截和触摸事件能够正常工作.
Additionally, because of the way events propagate in android it is not enough to have the mapfragment be contained in a fragment underneath the Frame. Instead the frame needed to be made into the parent of the fragment so that the intercept and touch events would work properly.
这篇关于从地图片段中捕获双击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!