gpt4 book ai didi

android - GoogleMap fragment 阻止自定义触摸事件

转载 作者:行者123 更新时间:2023-11-30 01:26:27 25 4
gpt4 key购买 nike

我有一个 SupportMapFragment,我正在尝试绘制一条用户绘制的线,与 here 完全一样,我正在尝试使用替代方法 #2 使用此问题接受的答案,这似乎允许您将 SupportMapFragment 放在 FrameLayout 中。但是,我无法让它为我工作。似乎 MapFragment 的 View 已将 requestDisallowInterceptTouchEvent() 设置为 true,阻止我正确使用触摸事件,但我可以在不包含 map fragment 的屏幕部分使用触摸事件。我在这里看到的所有问题似乎都与我想做的相反。

这是我与 map 相关的 xml 代码(根线性布局包含其他 View 组,与此无关):

<LinearLayout>
<FrameLayout
android:id="@+id/fram_map"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/mapFrag" tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment" />


</FrameLayout>

</LinearLayout>

在 MainActivity.onCreate() 中设置 MapFragment:

SupportMapFragment mapFragment = (SupportMapFragment)    getSupportFragmentManager()
.findFragmentById(R.id.mapFrag);

mapFragment.getMapAsync(this);

我尝试使用触摸事件的地方:

FrameLayout fram_map = (FrameLayout) findViewById(R.id.fram_map);

fram_map.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, @NonNull MotionEvent event){

float startX;
float startY;
float endX;
float endY;
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
startX = event.getX();
startY = event.getY();
LatLng pt = mMap.getProjection().fromScreenLocation(new Point((int) startX,(int) startY));
System.out.println("Start: " + pt.toString());
break;
case MotionEvent.ACTION_MOVE:
//check for difference in prev once
endX = event.getX();
endY = event.getY();
System.out.println("Moving: " + endX);
break;
case MotionEvent.ACTION_UP:
endX = event.getX();
endY = event.getY();
System.out.println("Stop: " + endX);
break;
}
return true;
}});

当我触摸 map 区域(我禁用了 .setScrollGesturesEnabled())时,我在 logcat 中得到的所有信息:

D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN

最佳答案

问题是您的 map fragment 位于 FrameLayout 内。您需要更改布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<fragment
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/mapFrag"
tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment" />

<FrameLayout
android:id="@+id/fram_map"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

</FrameLayout>

此外,作为建议,不要使用 System.out.println(),而应使用 Log。例如:Log.e("MOTION", "Start: "+ pt.toString());

关于android - GoogleMap fragment 阻止自定义触摸事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36361521/

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