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

时间:2016-11-14 08:06:02

标签: java android listview

我已将地图片段添加到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(...),但它对我不起作用。

1 个答案:

答案 0 :(得分:1)

那是因为ListView已经捕捉到了触摸事件。你必须让你的地图片段抓住它们。为此,您可以扩展您正在使用的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时,当用户触摸片段时,您必须requestDisallowInterceptTouchEvent(true)

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