Disable swiping between tabs
Asked Answered
S

5

52

I set up sliding tabs with two Fragments each Fragment has a Button which goes to a WebView. The problem with this is when the WebView Button is clicked the sliding tabs are still activated and when a user tries to navigate within the WebView you end up swiping to the other tab. Is there a way in an on click method to disable the swiping ability of the tabs? Any help would be hugely appreciated!

Here the code:

public class MyWebViewClass extends Fragment {

private WebView mWebView;
private Button mButton;

public MyWebViewClass() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_webview, container, false);

    mWebView = (WebView) view.findViewById(R.id.WebView);

    mButton = (Button) view.findViewById(R.id.Button1);
    mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mWebView.setVisibility(View.VISIBLE);
            mButton.setVisibility(View.GONE);
            mWebView.getSettings().setJavaScriptEnabled(true);
            mWebView.loadUrl("www.google.com");
        }
    });

    return view;
}
Sauveur answered 13/2, 2015 at 15:59 Comment(4)
i'm pretty sure that there is such functionality in whatever libs you are using, but question is how or rather when to enable it again ...Allbee
@Allbee yes i Thought that myself, I'm using MaterialTabs by neokree, but I couldn't find any way to disable the sliding tabs. Here's the Link github.com/neokree/MaterialTabsSiemens
ok instead using MaterialTab make your own class which extends this class and override onTouch(...) like this bool onTouch(params) {if(someFlagThatMeansThatSlidingIsDissable) { return false; } return super.onTouch(params)} add some getter and setter for this flag ... then use setter to dissable/enable sliding .... sounds pretty easy (but of course i will not write the code for you - well because it is almost aready written in this comment) ... edit: as i wrote before, problem is when to enable it again?Allbee
#13170135Willodeanwilloughby
V
87

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.

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

to

<my.package.CustomViewPager 
   ...
   />
Vyborg answered 13/2, 2015 at 16:28 Comment(6)
Great answer thanks very much just one problem im not really sure how to set up the adapter in the Main Activity? I have ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager()); what should i change this to?Siemens
You shouldn't change that.Vyborg
is there anything i should change?Siemens
you have to change the xml layout to your new custom ViewPager rather than the original one. and then get the variable like always but in a CustomViewPager object not just a ViewPager obj then call the mViewPager.setPagingEnabled(false); whenever you need.Vyborg
Sorry for the late reply great answer, took me a while to get my head around it. Thanks!Siemens
great but tab click also should be disableAnticlinal
P
28

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

Poncho answered 19/9, 2020 at 1:48 Comment(2)
The most actual and clear solution, thanks!Wearproof
Java: viewPager.setUserInputEnabled(false);Micromho
M
2

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

original answer: https://mcmap.net/q/64124/-how-do-disable-paging-by-swiping-with-finger-in-viewpager-but-still-be-able-to-swipe-programmatically

Monadelphous answered 27/2, 2017 at 11:33 Comment(0)
K
2

you can use this code.

viewpager2.setUserInputEnabled(false);
Kudva answered 24/7, 2022 at 10:6 Comment(2)
Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, can you edit your answer to include an explanation of what you're doing and why you believe it is the best approach?Mut
Please read How do I write a good answer?. While this code block may answer the OP's question, this answer would be much more useful if you explain how this code is different from the code in the question, what you've changed, why you've changed it and why that solves the problem without introducing others.Ingratiate
I
1

Accepted Answer's AndroidX and Kotlin Version:

ViewPager with paging disabled all the time

package your.packg.name //CustomViewPager's location actually

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) {
    override fun onTouchEvent(event: MotionEvent): Boolean {
        return false
    }

    override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
        return false
    }
}

ViewPager with paging switchable to on or off at anytime.

package your.packg.name //CustomViewPager's location actually

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 = false   //Modify this in code for switch

    override fun onTouchEvent(event: MotionEvent): Boolean {
        return isPagingEnabled && super.onTouchEvent(event)
    }

    override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
        return isPagingEnabled && super.onInterceptTouchEvent(event)
    }
}

Lastly, find ViewPager line in XML file and modify according to your CustomViewPager location, i.e.:

<your.packg.name.CustomViewPager ...

Inhabited answered 7/3, 2020 at 5:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.