对话框中的MapView [英] MapView in a Dialog

查看:50
本文介绍了对话框中的MapView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DialogFragment 显示为使用dialogFragment.show(this);

此片段的内容视图是ScrollView,底部是MapView.由于MapView的存在,因此onInterceptTouchEvent要特别注意ScrollView.当用作普通片段时,它可以正常工作.但是作为对话框,这是滚动时发生的情况.

The content view of this fragment is a ScrollView with a MapView at the bottom. onInterceptTouchEventis taken care for the ScrollView because of the presence of a MapView. And it works fine when used as a normal fragment. But as a dialog, this is what happens while scrolling.

MapViewDialog中消失.

这行不通:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="160dp"
    android:layout_below="@id/view_text" >

    <com.google.android.gms.maps.MapView
        android:id="@+id/view_map"
        android:layout_width="match_parent"
        android:layout_height="160dp"
        map:uiRotateGestures="true"
        map:uiScrollGestures="true"
        map:uiTiltGestures="false"
        map:uiZoomControls="false"
        map:uiZoomGestures="true" />

    <View
        android:layout_width="match_parent"
        android:layout_height="160dp" />
</RelativeLayout>

推荐答案

这些是当我必须将地图放入对话框或片段(或视图寻呼机)中时要遵循的步骤.

Well These are the steps i used to follow when i have to put a map in dialog or a fragment (or view pager).

先决条件::已经具有播放服务库,并添加了诸如INTERNET,WRITE_EXTERNAL_STORAGE之类的权限以及地图键和播放服务的元数据.

Prerequisite: Already have play services library, added permission like INTERNET, WRITE_EXTERNAL_STORAGE and metadata of map key and play services.

第1步:创建一个透明的地图类以使用地图 此类将以透明框架布局添加地图,以删除默认的黑色层(在某些设备中显示).

Step 1 : Create a transparent map class for using maps This class will add maps in a transparent frame layout for removing default black layer (appears in some devices).

public class TransparentMapFragment extends MapFragment {

    public TransparentMapFragment() {

    }

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

        FrameLayout frameLayout = new FrameLayout(getActivity());
        frameLayout.setBackgroundColor(getResources().getColor(
                android.R.color.transparent));
        ((ViewGroup) layout).addView(frameLayout, new ViewGroup.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        return layout;
    }

    public static TransparentMapFragment newInstance(String abc) {
        TransparentMapFragment tsf = new TransparentMapFragment();
        return tsf;
    }
}

第2步:更改对话框片段的对话框片段样式

    <style name="Theme.Default.Dialog" parent="@android:style/Theme.Dialog"></style>

    <style name="Theme.CustomDialog" parent="Theme.Default.Dialog">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:colorBackgroundCacheHint">@null</item>
    </style>

第3步:对话框的布局创建了一个全屏对话框,并根据您的需要添加了填充.在这里我正在使用60dip

Step 3: Layout of Dialog Created a full screen dialog and added padding according to your need. Here i am using 60dip

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="60dip" >

    <ScrollView [Use your lockable scroll view]
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#fff" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="8dip"
                android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&apos;s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Lorem Ipsum has been the industry&apos;s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
                android:textColor="#000"
                android:textSize="12sp" />


            <fragment
                android:id="@+id/map"
                android:name="com.app.widgets.TransparentMapFragment [Path of Transparent Map]"
                android:layout_width="match_parent"
                android:layout_height="100dip" />
        </LinearLayout>
    </ScrollView>


</FrameLayout>

第4步:设置对话框片段类 Map的常规实现

public class MapDialogFragment extends DialogFragment {

    private View view;

    private GoogleMap mMap;
    private double lat;
    private double lon;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Dialog dialog = new Dialog(getActivity(),
                R.style.Theme_CustomDialog);
        LayoutInflater inflater = getActivity().getLayoutInflater();
        view = inflater.inflate(R.layout.dialog_map, null);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(view);
        // Creating Full Screen
        dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);

        return dialog;
    }

    @Override
    public void onActivityCreated(Bundle bundle) {
        super.onActivityCreated(bundle);

        initializeViews();

    }

    private void initializeViews() {

        setUpMapIfNeeded();
    }

    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the
        // map.

        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((TransparentMapFragment) getActivity().getFragmentManager()
                    .findFragmentById(R.id.map)).getMap();
            mMap.getUiSettings().setZoomControlsEnabled(false);

            if (isGoogleMapsInstalled()) {
                if (mMap != null) {
                    setUpMap();
                }
            } else {
                Builder builder = new AlertDialog.Builder(getActivity());
                builder.setMessage("installGoogleMaps");
                builder.setCancelable(false);
                builder.setPositiveButton("install", getGoogleMapsListener());
                AlertDialog dialog = builder.create();
                dialog.show();
            }
        }
    }

    private void setUpMap() {

        lat = 28.6100;
        lon = 77.2300;

        final LatLng position = new LatLng(lon, lat);
        mMap.clear();
        mMap.getUiSettings().setAllGesturesEnabled(false);
        mMap.addMarker(new MarkerOptions().position(position).snippet(""));
    }

    public boolean isGoogleMapsInstalled() {
        try {
            getActivity().getPackageManager().getApplicationInfo(
                    "com.google.android.apps.maps", 0);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }

    public android.content.DialogInterface.OnClickListener getGoogleMapsListener() {
        return new android.content.DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(
                        Intent.ACTION_VIEW,
                        Uri.parse("market://details?id=com.google.android.apps.maps"));
                startActivity(intent);

                // Finish the fragment so they can't circumvent the check
                if (getActivity() != null) {
                    Fragment fragment = (getActivity().getFragmentManager()
                            .findFragmentByTag(MapFragment.class.getName()));
                    FragmentTransaction ft = getActivity().getFragmentManager()
                            .beginTransaction();
                    ft.remove(fragment);
                    ft.commitAllowingStateLoss();
                }
            }

        };
    }

    @Override
    public void onDestroyView() {

        super.onDestroyView();
        if (getActivity() != null) {
            try {
                Fragment fragment = (getActivity().getFragmentManager()
                        .findFragmentById(R.id.map));
                FragmentTransaction ft = getActivity().getFragmentManager()
                        .beginTransaction();
                ft.remove(fragment);
                ft.commitAllowingStateLoss();
            } catch (Exception e) {

            }
        }

    }

}

结果:

希望它会有所帮助. :)

Hope it will help thanks. :)

这篇关于对话框中的MapView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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