Is it possible to disable scrolling on a ViewPager
A simple solution is to create your own subclass of ViewPager
that has a private boolean
flag, isPagingEnabled
. Then override the onTouchEvent
and onInterceptTouchEvent
methods. If isPagingEnabled
equals true invoke the super
method, otherwise return
.
public class CustomViewPager extends ViewPager {
private boolean isPagingEnabled = true;
public CustomViewPager(Context context) {
super(context);
}
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return this.isPagingEnabled && super.onTouchEvent(event);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
return this.isPagingEnabled && super.onInterceptTouchEvent(event);
}
public void setPagingEnabled(boolean b) {
this.isPagingEnabled = b;
}
}
Then in your Layout.XML
file replace any <com.android.support.V4.ViewPager>
tags with <com.yourpackage.CustomViewPager>
tags.
This code was adapted from this blog post.
How do disable paging by swiping with finger in ViewPager but still be able to swipe programmatically?
You need to subclass ViewPager
. onTouchEvent
has a lot of good things in it that you don't want to change like allowing child views to get touches. onInterceptTouchEvent
is what you want to change. If you look at the code for ViewPager
, you'll see the comment:
/*
* This method JUST determines whether we want to intercept the motion.
* If we return true, onMotionEvent will be called and we do the actual
* scrolling there.
*/
Here's a complete solution:
First, add this class to your src
folder:
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.animation.DecelerateInterpolator;
import android.widget.Scroller;
import java.lang.reflect.Field;
public class NonSwipeableViewPager extends ViewPager {
public NonSwipeableViewPager(Context context) {
super(context);
setMyScroller();
}
public NonSwipeableViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
setMyScroller();
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
// Never allow swiping to switch between pages
return false;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// Never allow swiping to switch between pages
return false;
}
//down one is added for smooth scrolling
private void setMyScroller() {
try {
Class<?> viewpager = ViewPager.class;
Field scroller = viewpager.getDeclaredField("mScroller");
scroller.setAccessible(true);
scroller.set(this, new MyScroller(getContext()));
} catch (Exception e) {
e.printStackTrace();
}
}
public class MyScroller extends Scroller {
public MyScroller(Context context) {
super(context, new DecelerateInterpolator());
}
@Override
public void startScroll(int startX, int startY, int dx, int dy, int duration) {
super.startScroll(startX, startY, dx, dy, 350 /*1 secs*/);
}
}
}
Next, make sure to use this class instead of the regular ViewPager
, which you probably specified as android.support.v4.view.ViewPager
. In your layout file, you'll want to specify it with something like:
<com.yourcompany.NonSwipeableViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
This particular example is in a LinearLayout
and is meant to take up the entire screen, which is why layout_weight
is 1 and layout_height
is 0dp.
And setMyScroller();
method is for smooth transition
How to disable or enable viewpager swiping in android
Disable swipe progmatically by-
final View touchView = findViewById(R.id.Pager);
touchView.setOnTouchListener(new View.OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
return true;
}
});
and use this to swipe manually
touchView.setCurrentItem(int index);
How to disable swiping in ViewPager2?
Now it is possible to enable-disable swiping viewpager2
using Version 1.0.0-alpha02
Use implementation 'androidx.viewpager2:viewpager2:1.0.0-alpha02'
Version 1.0.0
New features
- Ability to disable user input (
setUserInputEnabled
,isUserInputEnabled
)
API changes
ViewPager2
class final
Bug fixes
FragmentStateAdapter
stability fixes
SAMPLE CODE to disable swiping in viewpager2
myViewPager2.setUserInputEnabled(false);
SAMPLE CODE to enable swiping in viewpager2
myViewPager2.setUserInputEnabled(true);
Disable ViewPager scrolling animation
I do not know if there is a clean solution. But you can use a trick and undo the standard page transformer with another transformer. The NoPageTransformer
would look like this:
private static class NoPageTransformer implements ViewPager.PageTransformer {
public void transformPage(View view, float position) {
if (position < 0) {
view.setScrollX((int)((float)(view.getWidth()) * position));
} else if (position > 0) {
view.setScrollX(-(int) ((float) (view.getWidth()) * -position));
} else {
view.setScrollX(0);
}
}
}
To add it to your ViewPager, call:
mViewPager.setPageTransformer(false, new NoPageTransformer());
Will work in SDK version 16 and above.
How to disable scrolling ViewPager while keyboard is shown?
Well, the problem was solved the next way: I just created a little function to hide the keyboard
public static void hideKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
//Find the currently focused view, so we can grab the correct window token from it.
View view = activity.getCurrentFocus();
//If no view currently has focus, create a new one, just so we can grab a window token from it
if (view == null) {
view = new View(activity);
}
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
And then used the listener of ViewPager:
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
hideKeyboard(activity);
}
@Override
public void onPageSelected(int position) {
if (prevMenuItem != null)
prevMenuItem.setChecked(false);
else
navigation.getMenu().getItem(0).setChecked(false);
navigation.getMenu().getItem(position).setChecked(true);
prevMenuItem = navigation.getMenu().getItem(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
Related Topics
Error:(6, 0) Gradle Dsl Method Not Found: 'Google()'
Using the Android Recognizerintent with a Bluetooth Headset
Data Binding Class Not Generated
How to Implement Recyclerview with Cardview Rows in a Fragment with Tablayout
Android Service Stops When App Is Closed
How to Provide Shadow to Button
What Exactly Does the Post Method Do
Actionbar Not Shown with Appcompat
How to Set a Gradient Background in a Material Button from Material Components
Streaming Video from Android Camera to Server
How to Create a Circular Progressbar in Android Which Rotates on It
Border for an Image View in Android
Recommended Way/Order to Read Data from a Webservice, Parse That Data and Insert It in a SQLite Db
Android Studio 0.4 Duplicate Files Copied in APK Meta-Inf/License.Txt
Show Alertdialog in Any Position of the Screen