Google Map + ViewPager2滑动冲突 [英] Google Map + ViewPager2 swiping conflict

查看:165
本文介绍了Google Map + ViewPager2滑动冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人在处理ViewPager2 +片段+ GoogleMap吗?

Anyone dealing with ViewPager2 + Fragments + GoogleMap?

我遇到了一个非常有趣的问题,在viewpager中,我有两个选项卡,每个选项卡都呈现片段.

I've ran into a really interesting issue, inside my viewpager i have two tabs that each render fragments.

其中一个片段会生成一张Google地图.

One of the fragments spawns a google map.

当我尝试通过该地图进行操作时,我得到了viewpager2尝试左右滑动的效果,这确实很烦人,所以我解决该问题的方法并不那么好.

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.

我通过构造函数将viewpager暴露给其中一个片段,并触发了对top viewpager活动的回调.

I exposed the viewpager to one of the fragments through the constructor and fired a callback to the top viewpager activity.

在该地图处于活动状态时,我在其中执行了一个简单的 isUserInputEnabled 逻辑操作.

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.

这是一个解决方案,但imo不太好.还有更好的主意吗?

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)
 }
}

推荐答案

尝试覆盖MapView以抑制触摸事件

Try to override MapView to supress touch events

public class MapViewInScroll extends MapView {
public MapViewInScroll(Context context) {
    super(context);
}

public MapViewInScroll(Context context, AttributeSet attributeSet) {
    super(context, attributeSet);
}

public MapViewInScroll(Context context, AttributeSet attributeSet, int i) {
    super(context, attributeSet, i);
}

public MapViewInScroll(Context context, GoogleMapOptions googleMapOptions) {
    super(context, googleMapOptions);
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    getParent().requestDisallowInterceptTouchEvent(true);
    return super.dispatchTouchEvent(ev);
}

}

并在您的xml布局中使用它,而不是原始的MapView

and use it in your xml layout instead of original MapView

                    <YOUR_PACKAGE.SOME_PATH_TO_VIEW.MapViewInScroll
                    android:id="@+id/map"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />

这篇关于Google Map + ViewPager2滑动冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆