0

My app's MainActivity has two Fragments within a ViewPager, a MapFragment and a ListFragment. I'm trying to follow Arshu's guide on adding the MapFragment into a ViewPager but without much luck. If you want to see what I mean, here's a screenshot from my app: App screenshot

Here's a condensed form of the MainActivity:

@Bind(R.id.activity_main_tabs)
PagerSlidingTabStrip mainTabs;
@Bind(R.id.activity_main_pager)
ViewPager mainPager;
@Bind(R.id.activity_main_toolbar)
Toolbar activityToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setSupportActionBar(activityToolbar);
    ButterKnife.bind(this);
    fragManager = getSupportFragmentManager();
    MainTabPagerAdapter adapter = new MainTabPagerAdapter(fragManager, aircraftArrayList);
    mainPager.setAdapter(adapter);
    mainTabs.setViewPager(mainPager);
}

My ViewPager's TabPagerAdapter:

 public class MainTabPagerAdapter extends FragmentStatePagerAdapter {


 private final String[] TAB_TITLES = {"Map", "List of planes"};
 public final String AIR_KEY = "aircraftKey";
 public static FragmentManager fragMgr;
 public Bundle bundle;
 ArrayList<Aircraft> aircraftArrayList;

 public MainTabPagerAdapter(FragmentManager fm, ArrayList<Aircraft> aircraftArrayList) {
     super(fm);
     fragMgr = fm;
     this.aircraftArrayList = aircraftArrayList;
 }

 @Override
 public CharSequence getPageTitle(int position){
    return TAB_TITLES[position];
 }

 @Override
 public int getCount() {
     return TAB_TITLES.length;
 }

 @Override
 public Fragment getItem(int position){
     switch(position){
         case 0:
             return MainMapFragment.newInstance(aircraftArrayList);
         case 1:
             return AircraftListFragment.newInstance(1, aircraftArrayList);
     }
     return null;
 }
}

My MainActivity's layout file:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/activity_main"
    android:fitsSystemWindows="true"
    tools:context="com.example.se415017.maynoothskyradar.activities.MainActivity">
    <android.support.v7.widget.Toolbar
            android:id="@+id/activity_main_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    <android.support.v4.view.ViewPager
        android:id="@+id/activity_main_pager"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:background="#eeeeee">
        <com.astuetz.PagerSlidingTabStrip
            android:id="@+id/activity_main_tabs"
            android:layout_width="match_parent"
            android:layout_height="36dp"
            android:layout_gravity="top"
            android:background="#eeeeee"
            android:paddingBottom="4dp"
            android:paddingTop="4dp"
            app:pstsIndicatorColor="#33ccee"
            app:pstsIndicatorHeight="4dp"
            app:pstsUnderlineColor="#33ccee"
            app:pstsUnderlineHeight="2dp"
            app:pstsShouldExpand="true"/>
    </android.support.v4.view.ViewPager>
</android.support.design.widget.CoordinatorLayout>

And, if it helps, my MapFragment:

public class MainMapFragment extends Fragment implements OnMapReadyCallback {
    static View view;
    SupportMapFragment mainMapFrag;
    private GoogleMap googleMap;
    private CameraPosition camPos;
    public static MainMapFragment newInstance(ArrayList<Aircraft> aircraftArrayList) {
        MainMapFragment fragment = new MainMapFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(AIR_KEY, aircraftArrayList);
        fragment.setArguments(bundle);
        return fragment;
    }
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        if(container == null)
            return null;
        view = inflater.inflate(R.layout.fragment_main_map, container, false);
        ButterKnife.bind(this, view);

        mainMapFrag = (SupportMapFragment) this.getChildFragmentManager().findFragmentById(R.id.main_map);
        mainMapFrag.getMapAsync(this);
        setUpMapIfNeeded();
    }
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState){
        if(googleMap != null)
            setUpMap(googleMap);
        else { //It's null anyway
            ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.main_map)).getMapAsync(this);
            mainMapFrag.getMapAsync(this);
            if (googleMap != null)
                setUpMap(googleMap);
        }
    }
    @Override
    public void onPause() {
        super.onPause();
        if(googleMap != null)
            camPos = googleMap.getCameraPosition();
        googleMap = null;
    }
    @Override
    public void onResume() {
        super.onResume();
        setUpMapIfNeeded();
        //Wait until googleMap is re-initialised
        if(camPos != null & googleMap != null) {
            googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(camPos));
            camPos = null;
        }
    }

    //Sets up the map if it hasn't been set up already
    //Somehow it works just fine if I get rid of "static"
    protected void setUpMapIfNeeded() {
        if (googleMap == null){
            Log.d(TAG, "Map was null");
            ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.main_map)) //TODO: FIX THIS
                    .getMapAsync(this);
            //Check if the map was obtained successfully
            if (googleMap != null)
                setUpMap(googleMap);
        }
    }

    /** This is where markers, lines and listeners are added, and where the camera is moved.
     *  @param googleMap The GoogleMap object to be set up.
     */
    private void setUpMap(GoogleMap googleMap) {
        googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
        //googleMap.setMyLocationEnabled(true); //TODO: Maybe wait until I have the pointing function worked out
        googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)))
                .setTitle("My server is here");
        googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(53.5, -6.35), 8.0f));
    }
    @Override
    public void onMapReady(final GoogleMap gMap) {
        googleMap = gMap;
        setUpMap(googleMap);
    }

Its layout file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.se415017.maynoothskyradar.fragments.MainMapFragment">

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

Can someone please tell me what I'm doing wrong?

Community
  • 1
  • 1
CiaranC94
  • 176
  • 4
  • 20

1 Answers1

0

It turns out I messed up the layout file for the MainActivity by putting the PagerSlidingTabStrip within the ViewPager element. Here's my fixed file:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/activity_main"
android:fitsSystemWindows="true"
tools:context="com.example.se415017.maynoothskyradar.activities.MainActivity">
    <android.support.v7.widget.Toolbar
        android:id="@+id/activity_main_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimaryDark"
        app:popupTheme="@style/AppTheme.PopupOverlay"
    android:visibility="visible" />

    <android.support.v4.view.ViewPager
    android:id="@+id/activity_main_pager"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:background="#eeeeee"
    android:clickable="false"/>

<com.astuetz.PagerSlidingTabStrip
    android:id="@+id/activity_main_tabs"
    android:layout_below="@id/activity_main_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:background="#eeeeee"
    android:paddingBottom="4dp"
    android:paddingTop="4dp"
    app:pstsIndicatorColor="#33ccee"
    app:pstsIndicatorHeight="4dp"
    app:pstsUnderlineColor="#33ccee"
    app:pstsUnderlineHeight="2dp"
    app:pstsShouldExpand="true"
    android:visibility="visible"
    android:measureAllChildren="false"
    android:longClickable="true"
    android:clickable="true" />
</android.support.design.widget.CoordinatorLayout>
CiaranC94
  • 176
  • 4
  • 20