有人处理ViewPager2 + Fragments + GoogleMap相关问题吗?
我遇到了一个很有趣的问题,在我的viewpager中,我有两个选项卡,每个选项卡都会渲染片段。
其中一个片段会生成一个google地图。
当我尝试在地图上移动时,会出现viewpager2试图向左或向右滑动的效果,这真的很烦人,所以我解决它的方法不太好。
我通过构造函数将viewpager暴露给一个片段,并向顶层viewpager活动触发回调。
在那里,我使用逻辑执行了一个简单的isUserInputEnabled
, 当地图处于活动状态时禁用滑动。
一次片段附加后,此回调注册并调用逻辑。
一旦片段分离,该回调就调用逻辑以重新启用。
这是一个解决方案,但我认为它不太好。还有更好的想法吗?
我认为滑动与视图重叠似乎是一个错误。
/* 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)
}
}
transparentImageView.setOnTouchListener(new OnSwipeTouchListener(mMain) { @Override public boolean onTouch(View v, MotionEvent event) { v.getParent().requestDisallowInterceptTouchEvent(true); mapView.dispatchTouchEvent(event); return super.onTouch(v, event); } });
- LM_IT