如何设置滚动视图内谷歌地图片段 [英] how to set google map fragment inside scroll view

查看:231
本文介绍了如何设置滚动视图内谷歌地图片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要设置垂直滚动视图内的谷歌地图的片段,当我做图不垂直缩放,我怎么能覆盖动人的地图视图的听众超过滚动视图的听众?

这是我的XML code:

 <滚动型机器人:layout_height =WRAP_CONTENT
    机器人:layout_width =FILL_PARENT
     机器人:layout_above =@ ID /页脚
    机器人:layout_below =@ ID / title_horizo​​ntalScrollView>   ///其他事情<片段
    机器人:ID =@ + ID /图
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =300dp    类=com.google.android.gms.maps.SupportMapFragment
    />  //其他事情


解决方案

创建一个自定义SupportMapFragmet这样我们就可以覆盖其触摸事件:

WorkaroundMapFragment.java

 进口android.content.Context;
进口android.os.Bundle;
进口android.widget.FrameLayout;进口android.view.LayoutInflater;
进口android.view.MotionEvent;
进口android.view.View;
进口android.view.ViewGroup;进口com.google.android.gms.maps.SupportMapFragment;公共类WorkaroundMapFragment扩展SupportMapFragment {
    私人OnTouchListener mListener;    @覆盖
    公共查看onCreateView(LayoutInflater layoutInflater,ViewGroup中ViewGroup中,捆绑savedInstance){
        查看布局= super.onCreateView(layoutInflater,ViewGroup中,savedInstance);        TouchableWrapper的FrameLayout =新TouchableWrapper(getActivity());        frameLayout.setBackgroundColor(getResources()的getColor(android.R.color.transparent));        ((ViewGroup中)布局).addView(的FrameLayout,
                新ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));        返回布局;
    }    公共无效使用setListener(OnTouchListener监听){
        mListener =侦听器;
    }    公共接口OnTouchListener {
        公共抽象无效onTouch();
    }    公共类TouchableWrapper扩展的FrameLayout {        公共TouchableWrapper(上下文的背景下){
            超级(上下文);
        }        @覆盖
        公共布尔dispatchTouchEvent(MotionEvent事件){
            开关(event.getAction()){
                案例MotionEvent.ACTION_DOWN:
                    mListener.onTouch();
                    打破;
                案例MotionEvent.ACTION_UP:
                    mListener.onTouch();
                    打破;
            }
            返回super.dispatchTouchEvent(事件);
        }
    }
}

在这上面的类,我们通过使用扩展的FrameLayout TouchableWrapper类拦截触摸事件。还有一个自定义的监听OnTouchListener触摸事件分派到处理该地图的主要活动MyMapActivity。当触摸事件发生后,dispatchTouchEvent将被调用,听者mListener将会处理它。

然后用这个类=packagename.WorkaroundMapFragment而不是 com.google.android.gms.maps.SupportMapFragment取代片段类XML

然后在你的活动初始化图如下:

 私人GoogleMap的MMAP;

里面的onCreate做到这一点:

  //检查是否我们已经拿到了GoogleMap的已
    如果(MMAP == NULL){
        MMAP =((WorkaroundMapFragment)getSupportFragmentManager()findFragmentById(R.id.map)。)的GetMap();
        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        mMap.getUiSettings()setZoomControlsEnabled(真)。
        mScrollView =(滚动型)findViewById(R.id.scrollMap); //在XML父滚动视图,给你的价值滚动视图的id        ((WorkaroundMapFragment)getSupportFragmentManager()。findFragmentById(R.id.map))
                .setListener(新WorkaroundMapFragment.OnTouchListener(){
                    @覆盖
                    公共无效onTouch(){
                        mScrollView.requestDisallowInterceptTouchEvent(真);
                    }
                });    }}

i want to set a google map fragment inside vertical scroll view, when i do the map does not zoom vertically, how can i override the listener of touching map view over the listener of scroll view?

this is my xml code:

  <ScrollView  android:layout_height="wrap_content"
    android:layout_width="fill_parent"
     android:layout_above="@id/footer"
    android:layout_below="@id/title_horizontalScrollView">

   ///other things



<fragment 
    android:id="@+id/map"
    android:layout_width="fill_parent"
    android:layout_height="300dp"

    class="com.google.android.gms.maps.SupportMapFragment"
    />

  //other things

解决方案

Create a custom SupportMapFragmet so that we can override its touch event:

WorkaroundMapFragment.java

import android.content.Context;
import android.os.Bundle;
import android.widget.FrameLayout;

import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.SupportMapFragment;

public class WorkaroundMapFragment 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());

        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);
        }
    }
}

In this above class, we intercept the touch event by using TouchableWrapper class that extends the FrameLayout. There is also a custom listener OnTouchListener to dispatch the touch event to the main activity MyMapActivity that handles the map. When touch event occurred, dispatchTouchEvent will be called and the listener mListener will handle it.

Then replace fragment class in xml with this class="packagename.WorkaroundMapFragment" instead of com.google.android.gms.maps.SupportMapFragment

Then in your activity initialise map as follows:

private GoogleMap mMap;

inside onCreate do this:

    // check if we have got the googleMap already
    if (mMap == null) {
        mMap = ((WorkaroundMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        mMap.getUiSettings().setZoomControlsEnabled(true);
        mScrollView = (ScrollView) findViewById(R.id.scrollMap); //parent scrollview in xml, give your scrollview id value

        ((WorkaroundMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .setListener(new WorkaroundMapFragment.OnTouchListener() {
                    @Override
                    public void onTouch() {
                        mScrollView.requestDisallowInterceptTouchEvent(true);
                    }
                });

    }

}

这篇关于如何设置滚动视图内谷歌地图片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆