From 6ea511b65bc2aaf265684b01d91ea16692a95f7f Mon Sep 17 00:00:00 2001 From: Tobrun Date: Fri, 19 Oct 2018 12:21:58 +0200 Subject: [android] - add example of showing a MapView inside a ViewPager inside RecyclerView --- .../java/com/mapbox/mapboxsdk/maps/MapView.java | 10 +- .../src/main/AndroidManifest.xml | 12 +- .../activity/fragment/NestedViewPagerActivity.kt | 162 +++++++++++++++++++++ .../src/main/res/layout/item_viewpager.xml | 17 +++ .../src/main/res/values/descriptions.xml | 1 + .../src/main/res/values/titles.xml | 1 + platform/android/scripts/exclude-activity-gen.json | 3 +- 7 files changed, 199 insertions(+), 7 deletions(-) create mode 100644 platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/fragment/NestedViewPagerActivity.kt create mode 100644 platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/item_viewpager.xml diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java index 10ac5a1bbe..8d1592046f 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java @@ -89,7 +89,7 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback { private MapKeyListener mapKeyListener; @Nullable private Bundle savedInstanceState; - private boolean isActivated; + private boolean isStarted; @UiThread public MapView(@NonNull Context context) { @@ -346,10 +346,10 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback { */ @UiThread public void onStart() { - if (!isActivated) { + if (!isStarted) { ConnectivityReceiver.instance(getContext()).activate(); FileSource.getInstance(getContext()).activate(); - isActivated = true; + isStarted = true; } if (mapboxMap != null) { mapboxMap.onStart(); @@ -395,10 +395,10 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback { mapRenderer.onStop(); } - if (isActivated) { + if (isStarted) { ConnectivityReceiver.instance(getContext()).deactivate(); FileSource.getInstance(getContext()).deactivate(); - isActivated = false; + isStarted = false; } } diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml index 1ec0116047..d6dca8664a 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml @@ -872,7 +872,6 @@ android:name="android.support.PARENT_ACTIVITY" android:value=".activity.FeatureOverviewActivity" /> - + + + + + * It requires calling the correct lifecycle methods when detaching and attaching the View to + * the RecyclerView with onViewAttachedToWindow and onViewDetachedFromWindow. + *

+ */ +@SuppressLint("ClickableViewAccessibility") +class NestedViewPagerActivity : AppCompatActivity() { + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_recyclerview) + recyclerView.layoutManager = LinearLayoutManager(this) + recyclerView.adapter = ItemAdapter(LayoutInflater.from(this), supportFragmentManager) + } + + class ItemAdapter(private val inflater: LayoutInflater, private val fragmentManager: FragmentManager) : RecyclerView.Adapter() { + + private val items = listOf( + "one", "two", "three", ViewPagerItem(), "four", "five", "six", "seven", "eight", "nine", "ten", + "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", + "nineteen", "twenty", "twenty-one" + ) + + private var mapHolder: ViewPagerHolder? = null + + companion object { + const val TYPE_VIEWPAGER = 0 + const val TYPE_TEXT = 1 + } + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { + return if (viewType == TYPE_VIEWPAGER) { + val viewPager = inflater.inflate(R.layout.item_viewpager, parent, false) as ViewPager + mapHolder = ViewPagerHolder(viewPager, fragmentManager) + return mapHolder as ViewPagerHolder + } else { + TextHolder(inflater.inflate(android.R.layout.simple_list_item_1, parent, false) as TextView) + } + } + + override fun getItemCount(): Int { + return items.count() + } + + override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { + if (holder.itemViewType == TYPE_TEXT) { + val textHolder = holder as TextHolder + textHolder.bind(items[position] as String) + } + } + + override fun getItemViewType(position: Int): Int { + return if (items[position] is ViewPagerItem) { + TYPE_VIEWPAGER + } else { + TYPE_TEXT + } + } + + class TextHolder(val textView: TextView) : RecyclerView.ViewHolder(textView) { + fun bind(item: String) { + textView.text = item + } + } + + class ViewPagerItem + class ViewPagerHolder(private val viewPager: ViewPager, fragmentManager: FragmentManager) : RecyclerView.ViewHolder(viewPager) { + init { + viewPager.adapter = MapPagerAdapter(fragmentManager) + viewPager.setOnTouchListener { view, motionEvent -> + // Disallow the touch request for recyclerView scroll + view.parent.requestDisallowInterceptTouchEvent(true) + viewPager.onTouchEvent(motionEvent) + false + } + } + } + + class MapPagerAdapter(fm: FragmentManager?) : FragmentStatePagerAdapter(fm) { + + override fun getItem(position: Int): Fragment { + val options = MapboxMapOptions() + options.textureMode(true) + options.doubleTapGesturesEnabled(false) + options.rotateGesturesEnabled(false) + options.tiltGesturesEnabled(false) + options.scrollGesturesEnabled(false) + options.zoomGesturesEnabled(false) + when (position) { + 0 -> { + options.styleUrl(Style.MAPBOX_STREETS) + options.camera(CameraPosition.Builder().target(LatLng(34.920526, 102.634774)).zoom(3.0).build()) + return SupportMapFragment.newInstance(options) + } + 1 -> { + return EmptyFragment.newInstance() + } + 2 -> { + options.styleUrl(Style.DARK) + options.camera(CameraPosition.Builder().target(LatLng(62.326440, 92.764913)).zoom(3.0).build()) + return SupportMapFragment.newInstance(options) + } + 3 -> { + return EmptyFragment.newInstance() + } + 4 -> { + options.styleUrl(Style.SATELLITE) + options.camera(CameraPosition.Builder().target(LatLng(-25.007786, 133.623852)).zoom(3.0).build()) + return SupportMapFragment.newInstance(options) + } + 5 -> { + return EmptyFragment.newInstance() + } + } + throw IllegalAccessError() + } + + override fun getCount(): Int { + return 6 + } + } + + class EmptyFragment : Fragment() { + companion object { + fun newInstance(): EmptyFragment { + return EmptyFragment() + } + } + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { + val textView = TextView(inflater.context) + textView.text = "This is an empty Fragment" + return textView + } + } + } +} diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/item_viewpager.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/item_viewpager.xml new file mode 100644 index 0000000000..9a0c882602 --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/item_viewpager.xml @@ -0,0 +1,17 @@ + + + + + + + diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/descriptions.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/descriptions.xml index abd46d4c8c..1256e42756 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/descriptions.xml +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/descriptions.xml @@ -77,4 +77,5 @@ Uses LocationComponent in a Fragment Force location updates and don\'t rely on the engine Show a MapView as a recyclerView item + Show a MapView inside a viewpager inside a recyclerView diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/titles.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/titles.xml index dd7900478b..2408f7dcdb 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/titles.xml +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/titles.xml @@ -77,4 +77,5 @@ Location Fragment Manual Location updates RecyclerView + Nested ViewPager \ No newline at end of file diff --git a/platform/android/scripts/exclude-activity-gen.json b/platform/android/scripts/exclude-activity-gen.json index ad4e5b316d..2e46770f68 100644 --- a/platform/android/scripts/exclude-activity-gen.json +++ b/platform/android/scripts/exclude-activity-gen.json @@ -38,5 +38,6 @@ "RenderTestActivity", "SymbolLayerActivity", "LocationFragmentActivity", - "MergeOfflineRegionsActivity" + "MergeOfflineRegionsActivity", + "NestedViewPagerActivity" ] -- cgit v1.2.1