Programmatically set google map fragment visibility (API2)

后端 未结 5 850
囚心锁ツ
囚心锁ツ 2020-12-01 18:20

xml:



        
5条回答
  •  情歌与酒
    2020-12-01 18:29

    Instead of creating a SupportMapFragment in xml, there could be another approach in which we can define container for SupportMapFragment in xml and then load map from class.

    In XML, let say container is FrameLayout-

        
    

    In my java class which is a Fragment, I have created two methods to show and hide map. I used detach and it depends on need. We can also use hide instead of detach.

       private void showMap() {
       mMapContainer.setVisibility(View.VISIBLE);
       mSupportMapFragment = SupportMapFragment.newInstance();
       getChildFragmentManager().beginTransaction()
                             .replace(R.id.mapContainer, mSupportMapFragment).commit();
       }
    
        private void hideMap() {
            mMapContainer.setVisibility(View.VISIBLE);
            if (mSupportMapFragment != null) {
                getChildFragmentManager().beginTransaction()
                                     .detach(mSupportMapFragment).commit();
            }
        }
    

提交回复
热议问题