How do disable paging by swiping with finger in ViewPager but still be able to swipe programmatically?

Now we no need to create custom ViewPager

A new ViewPager2 name View available in android

Vertical orientation support

  • ViewPager2 supports vertical paging in addition to traditional horizontal paging. You can enable vertical paging for a ViewPager2 element by setting its android:orientation attribute

Using XML

<androidx.viewpager2.widget.ViewPager2
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:orientation="vertical" />

Using code

To disable swiping in viewpager2 use

viewPager2.setUserInputEnabled(false);

To enable swiping in viewpager2 Use

viewPager2.setUserInputEnabled(true);

for more information check this

  • ViewPager2
  • ViewPager2 under the Hood
  • Hands on With ViewPager2

UPDATE

  • Please check : Migrate from ViewPager to ViewPager2

  • For complete example please check this Use of ViewPager2 in Android

Leave a Comment

tech