首先自定义一个 继承自 ViewPager的自定义 类
package com.yourcompany; import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent; public class NonSwipeableViewPager extends ViewPager { public NonSwipeableViewPager(Context context) {
super(context);
} public NonSwipeableViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
} @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;
}
}
然后在布局文件中引入这个自定义ViewPager的XML标签
<com.yourcompany.NonSwipeableViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
搞定啦