Disable swiping between tabs

后端 未结 4 492
不知归路
不知归路 2020-12-04 17:47

I set up sliding tabs with two Fragments each Fragment has a Button which goes to a WebView. The problem with this is whe

4条回答
  •  悲哀的现实
    2020-12-04 18:18

    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.SuppressLint
    import android.content.Context
    import android.util.AttributeSet
    import android.view.MotionEvent
    import androidx.viewpager.widget.ViewPager
    
    class 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.

    
    

    to

    
    

提交回复
热议问题