Anyone dealing with ViewPager2 + Fragments + GoogleMap?
I've ran into a really interesting issue, inside my viewpager i have two tabs that each render fragments.
One of the fragments spawns a google map.
When i try maneuvering through that map i get the effect of viewpager2 trying to swipe left and right, it's really annoying, so what i did to solve it was not so nice.
I exposed the viewpager to one of the fragments through the constructor and fired a callback to the top viewpager activity.
In there i performed a simple isUserInputEnabled
with logic, while the map is active disable swipe.
Once the fragment attaches this callback registers and invokes the logic.
Once the fragment detaches this callback invokes the logic to re-enable.
It's a solution but imo it's not nice. Any better ideas?
Swiping overlapping with views seems like a bug to me.
/* top activity that hosts viewpager2 and it's tabs(fragments) */
class ViewTap extends AppCompatActivity
all the good stuff about viewpager2: ViewPager2
fun registerUi(){
...TapPagerAdapter(...this)
}
override fun fireSensitivityResolver(fragment: Fragment, flag: Boolean){
if(fragment is ViewMapLoader){
this.mViewPager2.isUserInputEnabled = !flag
}
}
/* callback defintion fo handling events about Tab Capale thingies */
interface TabCapableIf {
fun fireSensitivityResolver(fragment: Fragment, flag: Boolean)
}
/* the adapter for viewpager2 */
class TapPagerAdapter(...private val vt: ViewTap) : FragmentStateAdapter(fm,lc){
override fun createFragment(position: Int): Fragment {
return when(position) {
....
CROWD_FRAGMENT -> { ViewCrowd(vmf, vt) }
}
}
/* the fragment where the recycler view shows */
class ViewCrowd(...,val vt: ViewTap) : Fragment(){
fun subscribeUi(){
some recycler adapter = ItemViewCrowdsAdapter(...,this)
}
}
/* the card adapter for the recycler view , when a card is clicked transition to map view */
class ItemViewCrowdsAdapter(...,private val vc: ViewCrowd) : AdapterFragmentRecyclerView(vm) {
override fun onBindViewHolder(holder: ItemHolder, position: Int){
...holder.itemView.setOnClickListener{
... fragmentTransaction.replace(R.id.layout_view_crowd_root, ViewMapLoader(...,vc.vt))
}
}
}
/* the map loader context that shows the map and handles adjusting the sensitivity so that viewpager2 swipe doesn't overlap with map swipe functionality. otherwise as i try swiping on the map the viewpager2 also swipes. */
class class ViewMapLoader(...,private val vt: ViewTap) : Fragment() {
private lateinit var mTabCapableIf: TabCapableIf
override fun onAttach(...){
this.mTabCapableIf = this.vt
mTabCapableIf.fireSensitivityResolver(this,true)
}
override fun onDetach(){
mTabCapableIf.fireSensitivityResolver(this,false)
}
}