问题描述
我有 ViewPager,在它下面有 10 个按钮.通过单击按钮,例如 #4,寻呼机立即通过 mPager.setCurrentItem(3);
转到第 4 页.但是,我想通过用手指水平滑动来禁用分页.因此,分页是仅通过点击按钮完成的.那么,如何禁用手指滑动?
I have ViewPager and below it I have 10 buttons. By clicking on button, for example #4, the pager goes immediately to page #4 by mPager.setCurrentItem(3);
. But, I want to disable the paging by swiping with finger horizontally. Thus, the paging is done ONLY by clicking on the buttons. So, how I can disable the swiping with finger?
推荐答案
您需要对 ViewPager
进行子类化.onTouchEvent
有很多你不想改变的好东西,比如允许子视图获得触摸.onInterceptTouchEvent
是您想要更改的内容.如果您查看 ViewPager
的代码,您将看到注释:
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:
首先,将此类添加到您的 src
文件夹中:
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*/);
}
}
}
接下来,确保使用此类而不是常规的 ViewPager
,您可能将其指定为 android.support.v4.view.ViewPager
.在您的布局文件中,您需要使用以下内容指定它:
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" />
这个特殊的例子是在一个 LinearLayout
中,并打算占据整个屏幕,这就是为什么 layout_weight
是 1 而 layout_height
是0dp.
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.
和setMyScroller();
方法是为了平滑过渡
And setMyScroller();
method is for smooth transition
这篇关于如何通过在 ViewPager 中用手指滑动来禁用分页,但仍然能够以编程方式滑动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!