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

标签 java android listview

我已将 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/

相关文章:

java - Java对象的 "sameness"是怎么定义的?

java - Hibernate Elasticsearch 与 wildfly 14 兼容

java - 如何让 Android Volley 解析我对工作线程中 POJO 的 Json 响应

java - 无需 CustomAdapter 即可更改 ListView 特定行的背景颜色

android - 有没有办法找出 ListView 中显示了多少项?

java - 引用 World 时表达式 : IsLocked() == false, 崩溃?

java - 仅当第一个发出的事件不满足条件时才从 Flux 中发布下一个元素

安卓 map 框 : How to unregister IntentReceiver?

android - 使用 Retrofit 从服务器获取多个 View 项目来创建 RecyclerView

python - django ListView 指定可用于类内所有方法的变量