summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorŁukasz Paczos <lukas.paczos@gmail.com>2019-05-17 17:09:38 +0200
committerŁukasz Paczos <lukasz.paczos@mapbox.com>2019-05-20 12:47:52 +0200
commit30d4a55716e8d23127b8a1e40c7e1afb85f53c82 (patch)
treed89c97069820709f00b7a0663804d3b7b65f31d5
parent521ac5c9938fa9a8c14823cd421ed0a13cf8ff40 (diff)
downloadqtlocation-mapboxgl-30d4a55716e8d23127b8a1e40c7e1afb85f53c82.tar.gz
[android] show multiple maps in a recycler view example
-rw-r--r--platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/maplayout/GLSurfaceRecyclerViewActivity.kt61
-rw-r--r--platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/maplayout/TextureRecyclerViewActivity.kt6
-rw-r--r--platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/descriptions.xml4
3 files changed, 35 insertions, 36 deletions
diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/maplayout/GLSurfaceRecyclerViewActivity.kt b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/maplayout/GLSurfaceRecyclerViewActivity.kt
index 5acf356696..3acb7aa382 100644
--- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/maplayout/GLSurfaceRecyclerViewActivity.kt
+++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/maplayout/GLSurfaceRecyclerViewActivity.kt
@@ -14,7 +14,7 @@ import com.mapbox.mapboxsdk.testapp.R
import kotlinx.android.synthetic.main.activity_recyclerview.*
/**
- * TestActivity showcasing how to integrate a GLSurfaceView MapView in a RecyclerView.
+ * TestActivity showcasing how to integrate multiple GLSurfaceView MapViews in a RecyclerView.
* <p>
* It requires calling the correct lifecycle methods when detaching and attaching the View to
* the RecyclerView with onViewAttachedToWindow and onViewDetachedFromWindow.
@@ -27,13 +27,7 @@ open class GLSurfaceRecyclerViewActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_recyclerview)
recyclerView.layoutManager = LinearLayoutManager(this)
- recyclerView.adapter = ItemAdapter(this, LayoutInflater.from(this), savedInstanceState)
- }
-
- override fun onSaveInstanceState(outState: Bundle?) {
- super.onSaveInstanceState(outState)
- // to save state, we need to call MapView#onSaveInstanceState
- (recyclerView.adapter as ItemAdapter).onSaveInstanceState(outState)
+ recyclerView.adapter = ItemAdapter(this, LayoutInflater.from(this))
}
override fun onLowMemory() {
@@ -52,15 +46,15 @@ open class GLSurfaceRecyclerViewActivity : AppCompatActivity() {
return R.layout.item_map_gl
}
- class ItemAdapter(private val activity: GLSurfaceRecyclerViewActivity, private val inflater: LayoutInflater, val savedInstanceState: Bundle?) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
+ class ItemAdapter(private val activity: GLSurfaceRecyclerViewActivity, private val inflater: LayoutInflater) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private val items = listOf(
- "one", "two", "three", MapItem(), "four", "five", "six", "seven", "eight", "nine", "ten",
+ "one", "two", "three", MapItem(Style.MAPBOX_STREETS), "four", "five", MapItem(Style.DARK), "seven", "eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen",
"nineteen", "twenty", "twenty-one"
)
- private var mapHolder: MapHolder? = null
+ private var mapHolders: MutableList<MapHolder> = mutableListOf()
companion object {
const val TYPE_MAP = 0
@@ -70,9 +64,9 @@ open class GLSurfaceRecyclerViewActivity : AppCompatActivity() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return if (viewType == TYPE_MAP) {
val mapView = inflater.inflate(activity.getMapItemLayoutId(), parent, false) as MapView
- mapView.getMapAsync { mapboxMap -> mapboxMap.setStyle(Style.MAPBOX_STREETS) }
- mapHolder = MapHolder(mapView, savedInstanceState)
- return mapHolder as MapHolder
+ val mapHolder = MapHolder(mapView)
+ mapHolders.add(mapHolder)
+ return mapHolder
} else {
TextHolder(inflater.inflate(android.R.layout.simple_list_item_1, parent, false) as TextView)
}
@@ -101,9 +95,10 @@ open class GLSurfaceRecyclerViewActivity : AppCompatActivity() {
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
- if (holder.itemViewType == TYPE_TEXT) {
- val textHolder = holder as TextHolder
- textHolder.bind(items[position] as String)
+ if (holder is TextHolder) {
+ holder.bind(items[position] as String)
+ } else if (holder is MapHolder) {
+ holder.bind(items[position] as MapItem)
}
}
@@ -115,28 +110,28 @@ open class GLSurfaceRecyclerViewActivity : AppCompatActivity() {
}
}
- fun onSaveInstanceState(savedInstanceState: Bundle?) {
- savedInstanceState?.let {
- mapHolder?.mapView?.onSaveInstanceState(it)
- }
- }
-
fun onLowMemory() {
- mapHolder?.mapView?.onLowMemory()
+ for (mapHolder in mapHolders) {
+ mapHolder.mapView.onLowMemory()
+ }
}
fun onDestroy() {
- mapHolder?.mapView?.let {
- it.onPause()
- it.onStop()
- it.onDestroy()
+ for (mapHolder in mapHolders) {
+ mapHolder.mapView.let {
+ it.onPause()
+ it.onStop()
+ it.onDestroy()
+ }
}
}
- class MapItem
- class MapHolder(val mapView: MapView, bundle: Bundle?) : RecyclerView.ViewHolder(mapView) {
+ data class MapItem(val style: String)
+ class MapHolder(val mapView: MapView) : RecyclerView.ViewHolder(mapView) {
+
init {
- mapView.onCreate(bundle)
+ // unfortunately, if there are multiple maps hosted in one activity, state saving is not possible
+ mapView.onCreate(null)
mapView.setOnTouchListener { view, motionEvent ->
// Disallow the touch request for recyclerView scroll
view.parent.requestDisallowInterceptTouchEvent(true)
@@ -144,6 +139,10 @@ open class GLSurfaceRecyclerViewActivity : AppCompatActivity() {
true
}
}
+
+ fun bind(mapItem: MapItem) {
+ mapView.getMapAsync { mapboxMap -> mapboxMap.setStyle(mapItem.style) }
+ }
}
class TextHolder(val textView: TextView) : RecyclerView.ViewHolder(textView) {
diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/maplayout/TextureRecyclerViewActivity.kt b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/maplayout/TextureRecyclerViewActivity.kt
index 895389bc1e..940d4b4f19 100644
--- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/maplayout/TextureRecyclerViewActivity.kt
+++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/maplayout/TextureRecyclerViewActivity.kt
@@ -4,12 +4,12 @@ import android.annotation.SuppressLint
import com.mapbox.mapboxsdk.testapp.R
/**
- * TestActivity showcasing how to integrate a TexureView MapView in a RecyclerView.
+ * TestActivity showcasing how to integrate multiple TexureView MapViews in a RecyclerView.
*/
@SuppressLint("ClickableViewAccessibility")
class TextureRecyclerViewActivity : GLSurfaceRecyclerViewActivity() {
- override fun getMapItemLayoutId() : Int{
- return R.layout.item_map_texture
+ override fun getMapItemLayoutId(): Int {
+ return R.layout.item_map_texture
}
}
diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/descriptions.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/descriptions.xml
index be8dae57a0..684220f2c4 100644
--- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/descriptions.xml
+++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/descriptions.xml
@@ -79,8 +79,8 @@
<string name="description_location_fragment">Uses LocationComponent in a Fragment</string>
<string name="description_location_manual">Force location updates and don\'t rely on the engine</string>
<string name="description_location_activation_builder">Use LocationComponentActivationOptions to set options</string>
- <string name="description_recyclerview_textureview">Show a TextureView MapView as a recyclerView item</string>
- <string name="description_recyclerview_glsurfaceview">Show a GLSurfaceView MapView as a recyclerView item</string>
+ <string name="description_recyclerview_textureview">Show multiple TextureView MapViews as recyclerView items</string>
+ <string name="description_recyclerview_glsurfaceview">Show multiple GLSurfaceView MapViews as recyclerView items</string>
<string name="description_nested_viewpager">Show a MapView inside a viewpager inside a recyclerView</string>
<string name="description_performance_measurement">Show the use PerformanceEvent for performance measurements</string>
<string name="description_physical_circle">Use TurfTransformation#circle() to show a Cirlce expressed in physical units</string>