Last active
June 5, 2021 16:52
-
-
Save Rohyme/868f8ea817d8f0f793df921057af2daf to your computer and use it in GitHub Desktop.
Using Map fragment in Recycler view using fastadapter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.fastadaptertest | |
import android.view.View | |
import com.google.android.gms.maps.GoogleMap | |
import com.mikepenz.fastadapter.FastAdapter | |
import com.mikepenz.fastadapter.items.AbstractItem | |
/** | |
* | |
* @author Rohyme | |
*/ | |
class CustomMapItem: AbstractItem<CustomMapItem.CustomMapVH>() { | |
var onItemReady : ((map :GoogleMap)->Unit)?=null | |
override val layoutRes: Int | |
get() = R.layout.activity_maps | |
override val type: Int | |
get() =R.id.fragment_map_view | |
override fun getViewHolder(v: View): CustomMapVH { | |
val customMap = v.findViewById<CustomMapView>(R.id.map) | |
customMap.onCreate(null) | |
customMap.onMapReady=onItemReady | |
return CustomMapVH(customMap) | |
} | |
inner class CustomMapVH(val map :CustomMapView): FastAdapter.ViewHolder<CustomMapItem>(map) { | |
override fun bindView(item: CustomMapItem, payloads: MutableList<Any>) { | |
map.onResume() | |
} | |
override fun unbindView(item: CustomMapItem) { | |
map.onStop() | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.fastadaptertest | |
import android.content.Context | |
import android.util.AttributeSet | |
import android.view.MotionEvent | |
import androidx.lifecycle.Lifecycle | |
import androidx.lifecycle.LifecycleObserver | |
import androidx.lifecycle.LifecycleOwner | |
import androidx.lifecycle.OnLifecycleEvent | |
import com.google.android.gms.maps.GoogleMap | |
import com.google.android.gms.maps.MapView | |
/** | |
* | |
* @author Rohyme | |
*/ | |
class CustomMapView : MapView, LifecycleObserver { | |
var onMapReady: ((map: GoogleMap) -> Unit)? = null | |
constructor(context: Context) : super(context) { | |
initMap() | |
} | |
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) { | |
initMap() | |
} | |
private fun initMap() { | |
if (context is LifecycleOwner) { | |
(context as LifecycleOwner).lifecycle.addObserver(this) | |
} | |
getMapAsync { | |
onMapReady?.invoke(it) | |
} | |
} | |
override fun dispatchTouchEvent(ev: MotionEvent?): Boolean { | |
when (ev?.action) { | |
MotionEvent.ACTION_UP -> { | |
this.parent.requestDisallowInterceptTouchEvent(false) | |
} | |
MotionEvent.ACTION_DOWN -> { | |
this.parent.requestDisallowInterceptTouchEvent(true) | |
} | |
} | |
return super.dispatchTouchEvent(ev) | |
} | |
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) | |
fun mapOnDestroy() { | |
this.onDestroy() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.fastadaptertest | |
import android.os.Bundle | |
import androidx.appcompat.app.AppCompatActivity | |
import androidx.recyclerview.widget.LinearLayoutManager | |
import com.google.android.gms.maps.CameraUpdateFactory | |
import com.google.android.gms.maps.GoogleMap | |
import com.google.android.gms.maps.model.LatLng | |
import com.google.android.gms.maps.model.MarkerOptions | |
import com.mikepenz.fastadapter.FastAdapter | |
import com.mikepenz.fastadapter.IItem | |
import com.mikepenz.fastadapter.adapters.ItemAdapter | |
import kotlinx.android.synthetic.main.activity_main.* | |
/** | |
* | |
* @author Rohyme | |
*/ | |
class MapsActivity : AppCompatActivity() { | |
private val adapterItem by lazy { ItemAdapter<IItem<*>>() } | |
private val fastAdapter by lazy { FastAdapter.with(adapterItem) } | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
listView.apply { | |
layoutManager = LinearLayoutManager(context) | |
setHasFixedSize(false) | |
isNestedScrollingEnabled = true | |
adapter = fastAdapter | |
} | |
for (i in 1..20) { | |
adapterItem.add(CustomMapItem().apply { | |
onItemReady = { | |
onMapReady(it) | |
} | |
}) | |
} | |
} | |
private fun onMapReady(googleMap: GoogleMap) { | |
// Add a marker in Sydney and move the camera | |
val sydney = LatLng(-34.0, 151.0) | |
googleMap.addMarker(MarkerOptions().position(sydney).title("Marker in Sydney")) | |
googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment