summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorŁukasz Paczos <lukas.paczos@gmail.com>2019-05-15 15:41:00 +0200
committerŁukasz Paczos <lukasz.paczos@mapbox.com>2019-05-15 16:58:13 +0200
commit0ecf45edbd496d1b73fc83c98f331ad120689753 (patch)
tree56e7e65ecd02a0e45bd70a46b3fd2522574d2091
parentc31bda732c52fa83406e50c92450786081235f57 (diff)
downloadqtlocation-mapboxgl-0ecf45edbd496d1b73fc83c98f331ad120689753.tar.gz
[android] refactor ViewPager example
-rw-r--r--platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/fragment/ViewPagerActivity.kt106
1 files changed, 49 insertions, 57 deletions
diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/fragment/ViewPagerActivity.kt b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/fragment/ViewPagerActivity.kt
index 77e2e1370d..8a944dc5eb 100644
--- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/fragment/ViewPagerActivity.kt
+++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/fragment/ViewPagerActivity.kt
@@ -26,33 +26,14 @@ class ViewPagerActivity : AppCompatActivity() {
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
super.onRestoreInstanceState(savedInstanceState)
-
val currentPosition = viewPager.currentItem
- var mapFragment: SupportMapFragment
-
- if (Math.abs(0 - currentPosition) <= 1) {
- mapFragment = viewPager.adapter?.instantiateItem(viewPager, 0) as SupportMapFragment
- mapFragment.getMapAsync { mapboxMap -> mapboxMap.setStyle(Style.MAPBOX_STREETS) }
- }
-
- if (Math.abs(1 - currentPosition) <= 1) {
- mapFragment = viewPager.adapter?.instantiateItem(viewPager, 1) as SupportMapFragment
- mapFragment.getMapAsync { mapboxMap -> mapboxMap.setStyle(Style.DARK) }
- }
-
- if (Math.abs(2 - currentPosition) <= 1) {
- mapFragment = viewPager.adapter?.instantiateItem(viewPager, 2) as SupportMapFragment
- mapFragment.getMapAsync { mapboxMap -> mapboxMap.setStyle(Style.SATELLITE) }
- }
-
- if (Math.abs(3 - currentPosition) <= 1) {
- mapFragment = viewPager.adapter?.instantiateItem(viewPager, 3) as SupportMapFragment
- mapFragment.getMapAsync { mapboxMap -> mapboxMap.setStyle(Style.SATELLITE) }
- }
-
- if (Math.abs(4 - currentPosition) <= 1) {
- mapFragment = viewPager.adapter?.instantiateItem(viewPager, 4) as SupportMapFragment
- mapFragment.getMapAsync { mapboxMap -> mapboxMap.setStyle(Style.SATELLITE) }
+ val offscreenLimit = viewPager.offscreenPageLimit
+ for (i in currentPosition - offscreenLimit..currentPosition + offscreenLimit) {
+ if (i < 0 || i > viewPager.adapter?.count ?: 0) {
+ continue
+ }
+ val mapFragment = viewPager.adapter?.instantiateItem(viewPager, i) as SupportMapFragment
+ mapFragment.getMapAsync(i)
}
}
@@ -63,37 +44,34 @@ class ViewPagerActivity : AppCompatActivity() {
}
override fun getItem(position: Int): Fragment? {
- var fragment: SupportMapFragment? = null
val options = MapboxMapOptions()
options.textureMode(true)
-
- when (position) {
- 0 -> {
- options.camera(CameraPosition.Builder().target(LatLng(34.920526, 102.634774)).zoom(3.0).build())
- fragment = SupportMapFragment.newInstance(options)
- fragment.getMapAsync { mapboxMap -> mapboxMap.setStyle(Style.MAPBOX_STREETS) }
- }
- 1 -> {
- options.camera(CameraPosition.Builder().target(LatLng(62.326440, 92.764913)).zoom(3.0).build())
- fragment = SupportMapFragment.newInstance(options)
- fragment.getMapAsync { mapboxMap -> mapboxMap.setStyle(Style.DARK) }
- }
- 2 -> {
- options.camera(CameraPosition.Builder().target(LatLng(-25.007786, 133.623852)).zoom(3.0).build())
- fragment = SupportMapFragment.newInstance(options)
- fragment.getMapAsync { mapboxMap -> mapboxMap.setStyle(Style.SATELLITE) }
- }
- 3 -> {
- options.camera(CameraPosition.Builder().target(LatLng(62.326440, 92.764913)).zoom(3.0).build())
- fragment = SupportMapFragment.newInstance(options)
- fragment.getMapAsync { mapboxMap -> mapboxMap.setStyle(Style.LIGHT) }
- }
- 4 -> {
- options.camera(CameraPosition.Builder().target(LatLng(34.920526, 102.634774)).zoom(3.0).build())
- fragment = SupportMapFragment.newInstance(options)
- fragment.getMapAsync { mapboxMap -> mapboxMap.setStyle(Style.TRAFFIC_NIGHT) }
- }
- }
+ options.camera(
+ CameraPosition.Builder()
+ .zoom(3.0)
+ .target(
+ when (position) {
+ 0 -> {
+ LatLng(34.920526, 102.634774)
+ }
+ 1 -> {
+ LatLng(62.326440, 92.764913)
+ }
+ 2 -> {
+ LatLng(-25.007786, 133.623852)
+ }
+ 3 -> {
+ LatLng(62.326440, 92.764913)
+ }
+ else -> {
+ LatLng(34.920526, 102.634774)
+ }
+ }
+ )
+ .build())
+
+ val fragment = SupportMapFragment.newInstance(options)
+ fragment.getMapAsync(position)
return fragment
}
@@ -102,8 +80,22 @@ class ViewPagerActivity : AppCompatActivity() {
}
companion object {
-
- private val NUM_ITEMS = 5
+ private const val NUM_ITEMS = 5
}
}
}
+
+fun SupportMapFragment.getMapAsync(index: Int) {
+ this.getMapAsync {
+ it.setStyle(
+ when (index) {
+ 0 -> Style.MAPBOX_STREETS
+ 1 -> Style.DARK
+ 2 -> Style.SATELLITE
+ 3 -> Style.LIGHT
+ 4 -> Style.TRAFFIC_NIGHT
+ else -> Style.MAPBOX_STREETS
+ }
+ )
+ }
+} \ No newline at end of file