Skip to content Skip to sidebar Skip to footer

Maps V2 With ViewPager

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

Solution 1:

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.


Post a Comment for "Maps V2 With ViewPager"