Disable swiping between tabs Disable swiping between tabs android android

Disable swiping between tabs


This answer can be applied to any ViewPager actually no matter what is the library you are using to implement the tabs or even a normal ViewPager without tabs.

The library you are using neokree/MaterialTabs is backed with a ViewPager that is responsible for the swiping effect and you can disable that by using the new ViewPager2 or providing your own custom ViewPager.

import android.annotation.SuppressLintimport android.content.Contextimport android.util.AttributeSetimport android.view.MotionEventimport androidx.viewpager.widget.ViewPagerclass CustomViewPager(context: Context, attrs: AttributeSet) : ViewPager(context, attrs) {    var isPagingEnabled: Boolean = true    @SuppressLint("ClickableViewAccessibility")    override fun onTouchEvent(event: MotionEvent): Boolean {        return isPagingEnabled && super.onTouchEvent(event)    }    override fun onInterceptTouchEvent(event: MotionEvent): Boolean {        return isPagingEnabled && super.onInterceptTouchEvent(event)    }}

This class provides a ViewPager that is swiping enabled and you can turn it off by viewPager.isPagingEnabled = false

No to mention that you have to change the XML layout to your new custom ViewPager rather than the original one.

<androidx.viewpager.widget.ViewPager    ...   />

to

<my.package.CustomViewPager    ...   />


Kotlin:

Using ViewPager2...

viewPager.isUserInputEnabled = false

Swiping between tab is disabled and the user can still tap tabs at the top to go between tabs.

Documentation on isUserInputEnabled


The simplest way is to setOnTouchListener and return true for ViewPager.

original answer:https://stackoverflow.com/a/13392198/4293813