我想展示另一个谷歌地图片段v2

时间:2013-05-29 07:26:15

标签: android google-maps android-fragments

我正在开发Android平板电脑项目。有双窗格式。在左侧有leftscrollfrg片段,它也执行右侧,mainmapfragment,它打开地图。在左侧,我单击createproject按钮,当我单击按钮时,它会隐藏mainmapfragment并添加另一个片段,即viewpager片段。在第一页中有CreateProjectFrg1片段,里面有一个按钮,它添加了另一个地图片段(CreateProjectMapFrg)。

问题是当第二张地图打开时,第一张地图显示为冻结。但我可以看到新打开的地图,因为旧的地图从左右填充。而当我关闭屏幕并重新打开时,旧的地图已经消失了。

我该如何解决这个问题?

注意:在层次结构视图中,我看不到冻结的地图。

Frozen Map

public class LeftScrollFrg extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View vw = inflater.inflate(R.layout.frg_left_behind, container, false);

            frg_map = (MainMapFragment) getActivity()
                        .getSupportFragmentManager().findFragmentByTag(
                                MAP_FRAGMENT_TAG);
            if (frg_map == null) {

                    frg_map = new MainMapFragment();

                    FragmentTransaction ft = getFragmentManager()
                            .beginTransaction();

                    ft.replace(R.id.above_framelayout, frg_map, MAP_FRAGMENT_TAG);
                    ft.addToBackStack(MAP_FRAGMENT_TAG);
                    ft.commit();
            }

            imgbtn_createproject.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    hideMainMapFrg();

                    openCreateProjectFrg();
                }
            });

            imgbtn_map.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    hideCreateProjectFrg();

                    openMapFrg();

                }
            });
            return vw;
    }

        private void hideMainMapFrg() {

            fm = getActivity().getSupportFragmentManager();

            frg_map = (MainMapFragment) fm
                    .findFragmentByTag(MAP_FRAGMENT_TAG);

            if (fm != null) {
                ft = fm.beginTransaction();

                if (frg_map != null) {
                    if (frg_map.isVisible()) {
                        ft.hide(frg_map);
                    }
                }
            }
            ft.commit();

        }

        private void openMapFrg() {
            fm = getActivity().getSupportFragmentManager();

            frg_map = (MainMapFragment) fm
                    .findFragmentByTag(MAP_FRAGMENT_TAG);

            if (fm != null) {
                ft = fm.beginTransaction();


                if (frg_createproject != null) {
                    if (frg_createproject.isVisible()) {
                        ft.hide(frg_createproject);
                    }
                }

                if (frg_map == null) {
                    frg_map = new MainMapFragment();
                    ft.add(R.id.above_framelayout, frg_map, MAP_FRAGMENT_TAG);
                } else {
                    if (frg_map.isHidden()) {
                        ft.show(frg_map);
                    }
                }
                ft.addToBackStack(MAP_FRAGMENT_TAG);
                ft.commit();
            }

        }
    }

    public class MainMapFragment extends SupportMapFragment implements
            LocationListener, LocationSource, OnInfoWindowClickListener,
            OnMapClickListener {
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            super.onCreateView(inflater, container, savedInstanceState);
            view = inflater.inflate(R.layout.frg_mainproject, container, false);


            return view;
            }


    }

    //frg_mainproject.xml
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="@dimen/rightpanel_margin" >

        <fragment
            android:id="@+id/main_map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            class="com.google.android.gms.maps.SupportMapFragment" 
            />
    </RelativeLayout>





    public class CreateProjectFrg1 extends Fragment{
            @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View vw = inflater.inflate(R.layout.frg_createproject1, container,
                    false);

            imgvw_createprojectmap.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    openCreateMap();
                }
            });

                    return vw;
            }

                private void openCreateMap() {
            fm = getActivity().getSupportFragmentManager();

            frg_map = (CreateProjectMapFrg) fm
                    .findFragmentByTag(CREATEPROJECTMAP_FRAGMENT_TAG);



            if (fm != null) {
                ft = fm.beginTransaction();
                if (frg_map == null) {

                    frg_map = new CreateProjectMapFrg();
                    ft.add(R.id.above_framelayout, frg_map, CREATEPROJECTMAP_FRAGMENT_TAG);
                }
                ft.addToBackStack(CREATEPROJECTMAP_FRAGMENT_TAG);
                ft.commit();


            }

        }


    }







    public class CreateProjectMapFrg extends SupportMapFragment implements
            OnMapClickListener, OnMarkerClickListener {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            super.onCreateView(inflater, container, savedInstanceState);

            if (view != null) {
                ViewGroup parent = (ViewGroup) view.getParent();
                if (parent != null)
                    parent.removeView(view);
            }

            try {
                view = inflater.inflate(R.layout.frg_createprojectmap, container,
                        false);
            } catch (InflateException e) {

            }
            setViewsResource(view);
            setUpMapIfNeeded();

            return view;
        }

            private void setUpMapIfNeeded() {

            if (mMap == null) {

                mMap = ((SupportMapFragment) getActivity()
                        .getSupportFragmentManager().findFragmentById(
                                R.id.frg_createproject_map)).getMap();

                if (mMap != null) {
                    setUpMap();
                }
            }

        }

    }

    //frg_createprojectmap.xml
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <fragment
            android:id="@+id/frg_createproject_map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            class="com.google.android.gms.maps.SupportMapFragment" />

     </RelativeLayout>

更新了代码

1 个答案:

答案 0 :(得分:0)

这是https://code.google.com/p/gmaps-api-issues/issues/detail?id=5027

作为一种解决方法,您可以明确地从片段管理器中删除/替换MapFragment,而不是仅隐藏它。如果您不想整体删除片段,您还可以实现自己的片段,它只使用MapView并在片段隐藏之前分离(可能需要更多自定义代码)。