gpt4 book ai didi

java - 无法在 Listview android 中拖动或缩放谷歌地图

转载 作者:行者123 更新时间:2023-11-30 00:53:18 24 4
gpt4 key购买 nike

我已将 map fragment 添加到 ListView。它工作正常,但不能拖动或缩放。

MainActivity.java

mapviewheader = getActivity().getLayoutInflater().inflate(R.layout.view_header, mListView, false);
FrameLayout map_container = (FrameLayout) mapviewheader.findViewById(R.id.map_container);


FragmentTransaction mTransaction = this.getChildFragmentManager().beginTransaction();
mapFragment = new SupportMapFragment().newInstance();

//mapFragment.setOnTouchListener
mTransaction.add(map_container.getId(), mapFragment);
mTransaction.commit();

try {
MapsInitializer.initialize(getActivity());
} catch (Exception e) {
e.printStackTrace();
}

mListView.setParallaxView(mapviewheader);

view_header.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical">

<FrameLayout
android:id="@+id/map_container"
android:layout_width="match_parent"
android:layout_height="400dp"
/>

</LinearLayout>

我尝试过使用 onTouchListener(...) 但它对我不起作用。

最佳答案

那是因为 ListView 已经在捕获触摸事件。你必须让你的 map fragment 捕捉到它们。为此,您可以扩展正在使用的 MapSupportFragment

public class TouchableGoogleMapFragment extends SupportMapFragment {
private OnTouchListener mListener;

@Override
public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle savedInstance) {
View layout = super.onCreateView(layoutInflater, viewGroup, savedInstance);

TouchableWrapper frameLayout = new TouchableWrapper(getActivity());

// make it invisible
frameLayout.setBackgroundColor(getResources().getColor(android.R.color.transparent));

((ViewGroup) layout).addView(frameLayout,
new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

return layout;
}

public void setListener(OnTouchListener listener) {
mListener = listener;
}

public interface OnTouchListener {
public abstract void onTouch();
}

public class TouchableWrapper extends FrameLayout {

public TouchableWrapper(Context context) {
super(context);
}

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mListener.onTouch();
break;
case MotionEvent.ACTION_UP:
mListener.onTouch();
break;
}
return super.dispatchTouchEvent(event);
}
}
}

然后,在您的 MainActivity 中,您必须在用户触摸 Fragment 时 requestDisallowInterceptTouchEvent(true):

// Intercepting touch events
mapFragment.setListener(new TouchableGoogleMapFragment.OnTouchListener() {
@Override
public void onTouch() {
mScrollView.requestDisallowInterceptTouchEvent(true);
}
});

关于java - 无法在 Listview android 中拖动或缩放谷歌地图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40584254/

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