158

Is it possible to enable-disable swiping in new android viewpager2 component?

6 Answers 6

340

Now it is possible to enable-disable swiping viewpager2 using Version 1.0.0-alpha02

Use implementation 'androidx.viewpager2:viewpager2:1.0.0-alpha02'

Version 1.0.0

New features

  • Ability to disable user input (setUserInputEnabled, isUserInputEnabled)

API changes

  • ViewPager2 class final

Bug fixes

  • FragmentStateAdapter stability fixes

SAMPLE CODE to disable swiping in viewpager2

myViewPager2.setUserInputEnabled(false);

SAMPLE CODE to enable swiping in viewpager2

myViewPager2.setUserInputEnabled(true);
4
  • 1
    Perfect. Now I can have a map fragment and scroll it and use swiping for the other fragments. Oct 19, 2020 at 11:19
  • 2
    Does this also disable tapping on the tabs to change the current tab to another specific tab? Nov 4, 2020 at 12:05
  • 2
    is there a way to do this using xml?
    – aLL
    Mar 18, 2021 at 6:03
  • @aLL there is no way to do this using XML at the moment
    – AskNilesh
    Mar 18, 2021 at 6:37
14

If you are using Android Data Binding you can simply disable it your layout xml file.

app:userInputEnabled="@{false}"
11
viewPager2.setUserInputEnabled(false);
6

Under the hood ViewPager2 works with RecyclerView for inflating the fragment views, but the RecyclerView is hidden so they make it more idiot proof.

 val rv : RecyclerView = viewPager.getChildAt(0) as RecyclerView
 rv.layoutManager = NonScrollingLayoutManager( rv.context, rv.layoutManager as LinearLayoutManager)

Hacky way is to get the child at position zero which is the RecyclerView and disable scrolling in the layout manager, by wrapping the layout manager:

inner class NonScrollingLayoutManager(context: Context, val layoutManager: LinearLayoutManager) :
    LinearLayoutManager(context, layoutManager.orientation, layoutManager.reverseLayout) {

    override fun canScrollVertically(): Boolean  = layoutManager.orientation == HORIZONTAL


    override fun canScrollHorizontally(): Boolean  =  layoutManager.orientation == VERTICAL

}

Please beware that if the API changes the layout manager used for the RecyclerView, i.e they move away from the LinearLayoutManager this wont work and it will need some methods overriden and ensure super methods are called.

Second approach is subclass the ViewPager2 which is ViewGroup and then do the magic in intercepting touch events, before they are dispatched to the child views (as you would guess the RecyclerView) and be careful not to prevent clicks.

3
  • 12
    subclass is impossible because viewPages2 is final.
    – Guy
    Jan 17, 2020 at 18:32
  • 5
    @Guy At the time my answer was posted ViewPager2 was open. Jan 17, 2020 at 19:28
  • 2
    Sorry but creating a separate class just this is bad practice. Kotlin and VP2 offer this short one-liner pager.userInputEnabled = false
    – RFM
    Jun 3, 2021 at 11:47
2

in kotlin just use viewPager2.isUserInputEnabled = false

0

Yes it is possible.

mViewPager2.setUserInputEnabled(false); //disables swipe

mViewPager2.setUserInputEnabled(true); //enables swipe

We can disable and enable based on tabs, as shown in below code

ViewPager2 mViewPager2 = findViewById(R.id.view_pager);
        mViewPager2.setOffscreenPageLimit(adapter.getItemCount() - 1);
        mViewPager2.setAdapter(adapter); 


        TabLayout tabLayout  = findViewById(R.id.sliding_tabs);
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //do stuff here
                if(tab.getPosition() == 0){
                    **//ToDisable**
                    mViewPager2.setUserInputEnabled(false);
                }else{
                    **//ToEnable**
                    mViewPager2.setUserInputEnabled(true);
                }

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.