0

I have created a google map fragment:

MapFragment.xml

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.gms.maps.MapView
    android:id="@+id/soleViewMap"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

I then have the java class for this fragment MapFragment.Java

public class MapFragment extends Fragment implements OnMapReadyCallback {

  MapView gMapView;
  GoogleMap gMap = null;

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.sole_map, container, false);

    gMapView = (MapView) view.findViewById(R.id.soleViewMap);
    gMapView.getMapAsync(this);

    return view;
  }

  @Override
  public void onMapReady(GoogleMap map) {
    gMap = map;
    gMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new
            LatLng(49.39,-124.83), 20));
  }
}

Next I have my viewpager activity and layout

activty_layout_viewpager.xml:

<android.support.v4.view.ViewPager
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" >

My activity:

import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import android.widget.TextView;

import com.example.susan.myapplication.R;

public class ViewPagerActivity extends AppCompatActivity {


    private SectionsPagerAdapter mSectionsPagerAdapter;

    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_pager);

        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

    }



    public static class PlaceholderFragment extends Fragment {

        private static final String ARG_SECTION_NUMBER = "section_number";

        public PlaceholderFragment() {
        }

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_view_pager, container, false);
            TextView textView = (TextView) rootView.findViewById(R.id.section_label);
            textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
            return rootView;
        }
    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a PlaceholderFragment (defined as a static inner class below).
            return PlaceholderFragment.newInstance(position + 1);
        }

        @Override
        public int getCount() {
            // Show 5 total pages.
            return 4;
        }
    }
}

Right now I have an inner class which creates the fragments but I want to now create realy fragments. Usually what I would do to put a fragment inside of an activity is in the activity layout I would create a framelayout and put the fragment into this framelayout. Since I am using a viewpager I can't do this. How then do I put the google map fragment into one of the viewpager pages?

user2924127
  • 6,034
  • 16
  • 78
  • 136

0 Answers0