Maps V2 with viewPager

前端 未结 1 901
傲寒
傲寒 2020-12-30 13:16

I\'m currently developing for Android 4.3.
Background:
I\'m using pageViewer to scroll between three different fragments. My second fragment (tab 2)

相关标签:
1条回答
  • 2020-12-30 14:09

    Since Android 4.2 (also in the support library for pre-Jelly) you can use nested fragments.

    You can see how to create such fragment is:

    public class MyFragment extends Fragment {
    
        private SupportMapFragment fragment;
        private GoogleMap map;
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(R.layout.layout_with_map, container, false);
        }
    
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            FragmentManager fm = getChildFragmentManager();
            fragment = (SupportMapFragment) fm.findFragmentById(R.id.map_container);
            if (fragment == null) {
                fragment = SupportMapFragment.newInstance();
                fm.beginTransaction().replace(R.id.map_container, fragment).commit();
            }
        }
    
        @Override
        public void onResume() {
            super.onResume();
            if (map == null) {
                map = fragment.getMap();
                map.addMarker(new MarkerOptions().position(new LatLng(0, 0)));
            }
        }
    }
    

    Above code copied from here.

    Impoatant note: your custom Fragments's layout cannot contain <fragment> tag.

    0 讨论(0)
提交回复
热议问题