diff options
-rw-r--r-- | platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/fragment/ViewPagerActivity.java | 41 |
1 files changed, 37 insertions, 4 deletions
diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/fragment/ViewPagerActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/fragment/ViewPagerActivity.java index 63c63b690e..c494842b14 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/fragment/ViewPagerActivity.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/fragment/ViewPagerActivity.java @@ -3,9 +3,10 @@ package com.mapbox.mapboxsdk.testapp.activity.fragment; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; -import android.support.v4.app.FragmentPagerAdapter; +import android.support.v4.app.FragmentStatePagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; + import com.mapbox.mapboxsdk.camera.CameraPosition; import com.mapbox.mapboxsdk.geometry.LatLng; import com.mapbox.mapboxsdk.maps.MapboxMapOptions; @@ -18,19 +19,51 @@ import com.mapbox.mapboxsdk.testapp.R; */ public class ViewPagerActivity extends AppCompatActivity { + private ViewPager viewPager; + private MapFragmentAdapter adapter; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_viewpager); - ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager); + viewPager = (ViewPager) findViewById(R.id.viewpager); if (viewPager != null) { - MapFragmentAdapter adapter = new MapFragmentAdapter(getSupportFragmentManager()); + adapter = new MapFragmentAdapter(getSupportFragmentManager()); viewPager.setAdapter(adapter); } } - static class MapFragmentAdapter extends FragmentPagerAdapter { + @Override + protected void onRestoreInstanceState(Bundle savedInstanceState) { + super.onRestoreInstanceState(savedInstanceState); + + int currentPosition = viewPager.getCurrentItem(); + SupportMapFragment mapFragment; + + if (Math.abs(0 - currentPosition) <= 1) { + mapFragment = (SupportMapFragment) adapter.instantiateItem(viewPager, 0); + mapFragment.getMapAsync(mapboxMap -> { + mapboxMap.setStyle(Style.MAPBOX_STREETS); + }); + } + + if (Math.abs(1 - currentPosition) <= 1) { + mapFragment = (SupportMapFragment) adapter.instantiateItem(viewPager, 1); + mapFragment.getMapAsync(mapboxMap -> { + mapboxMap.setStyle(Style.DARK); + }); + } + + if (Math.abs(2 - currentPosition) <= 1) { + mapFragment = (SupportMapFragment) adapter.instantiateItem(viewPager, 2); + mapFragment.getMapAsync(mapboxMap -> { + mapboxMap.setStyle(Style.SATELLITE); + }); + } + } + + static class MapFragmentAdapter extends FragmentStatePagerAdapter { private static int NUM_ITEMS = 3; |