Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make swipe disable in a view pager in android [duplicate]

I am creating an application contains of view pager with swipe-able tabs i want to do some operations inside view pager and at the time of operations i want to disable the swipe view after operations done enable the swipe view can any one tell me how to do this and my activity was extending to the fragment activity thanks in advance

like image 677
lakshman sundeep Avatar asked Jun 23 '15 10:06

lakshman sundeep


People also ask

How to disable View Pager swipe in Android?

To enable / disable the swiping, just overide two methods: onTouchEvent and onInterceptTouchEvent . Both will return "false" if the paging was disabled. You just need to call the setPagingEnabled method with false and users won't be able to swipe to paginate.

How to make swipe screen in Android?

You can create swipe views using AndroidX's ViewPager widget. To use ViewPager and tabs, you need to add a dependency on ViewPager and on Material Components to your project. To insert child views that represent each page, you need to hook this layout to a PagerAdapter .

How to use TabLayout with ViewPager in Android?

Find the view pager that will allow the user to swipe between fragments. Create an adapter that knows which fragment should be shown on each page. Set the adapter onto the view pager. Give the TabLayout the ViewPager.

How ViewPager works in Android?

ViewPager - How It Works The idea is that ViewPager works with a PageAdapter to supply the Views that represent each page. The basic idea is that ViewPager keeps track of what page should be visible on the screen and then asks the PagerAdapter to get the View hierarchy that needs to be displayed.


1 Answers

Create a new class which named CustomViewPager. The class inherits from ViewPager and includes a new method called setPagingEnabled. To enable / disable the swiping, just overwrite two methods: onTouchEvent and onInterceptTouchEvent. Both return false if the paging is disabled.

Here is the full code:

package com.yourpackage;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

public class CustomViewPager extends ViewPager {

private boolean enabled;

public CustomViewPager(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.enabled = true;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (this.enabled) {
        return super.onTouchEvent(event);
    }

    return false;
}

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    if (this.enabled) {
        return super.onInterceptTouchEvent(event);
    }

    return false;
}

public void setPagingEnabled(boolean enabled) {
    this.enabled = enabled;
}
}

Then in your layout.xml file replace any <com.android.support.V4.ViewPager> tags with <com.yourpackage.CustomViewPager> tags like below.

<com.yourpackage.CustomViewPager 
android:id="@+id/photosViewPager" 
android:layout_height="match_parent" 
android:layout_width="match_parent" />

Now use CustomViewPager instead of ViewPager in your activity. And that's it. Now, anytime to disable the swiping, just need to call the setPagingEnabled method with false.

CustomViewPager mViewPager = (CustomViewPager) findViewById(R.id.pager);
mViewPager.setPagingEnabled(false);
like image 149
Priyank Patel Avatar answered Sep 30 '22 14:09

Priyank Patel