Fragments in ViewPager2 does not respond to clicks if scroll position is 0

筅森魡賤 提交于 2020-07-19 05:25:12

问题


I am very glad that Google released ViewPager2 which is built on RecyclerView to solve a lot of issues that the old ViewPager has.

I quickly replaced my old ViewPager codes to ViewPager2:

  1. Replace ViewPager with ViewPager2 in xml

  2. Replace FragmentPagerAdapter(FragmentManager) with FragmentStateAdapter(Fragment)

  3. ViewPager setup is as below:

    viewPager.adapter = fragmentAdapter
    
    val mediator = TabLayoutMediator(tabLayout, viewPager, true) { tab, position ->
        tab.text = fragmentAdapter.tabNames[position]
    }
    mediator.attach()
    

No other changes have been made.

The problem

After performing the above change, I realized a problem -

Now my ViewPager is an ordinary horizontal pager, and each fragment in my fragmentAdapter has a vertical RecyclerView.

I observed that when ever the scroll position of the RecyclerView is 0, my items in that RecyclerView cannot receive any click nor long click events, but it can be scrolled. Once it is scrolled, it can receive clicks again.*

Knowing that ViewPager2 is a RecyclerView as well, is there something to do with nested RecyclerView?


回答1:


It turns out it's probably a bug in either ConstraintLayout or ViewPager2.

The container of ViewPager2 was originally a ConstraintLayout, and after I changed it to LinearLayout, it simply worked.

I tried to reproduce the issue in a sample project but I can't reproduce even if I used ConstraintLayout... So there must be some other conditions to make that happen.



来源:https://stackoverflow.com/questions/56590340/fragments-in-viewpager2-does-not-respond-to-clicks-if-scroll-position-is-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!