需求:去掉ViewPager的滑动功能
方法:
(1)找到网上别人重写好的一个ViewPager,叫做NoScollViewPager,代码如下:
public class NoScrollViewPager extends ViewPager {
private boolean noScroll = false;
public NoScrollViewPager(Context context) {
super(context);
}
public NoScrollViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setNoScroll(boolean noScroll) {
this.noScroll = noScroll;
}
@Override
public void scrollTo(int x, int y) {
super.scrollTo(x, y);
}
@Override
public boolean onTouchEvent(MotionEvent arg0) {
/* return false;//super.onTouchEvent(arg0); */
if (noScroll)
return false;
else
return super.onTouchEvent(arg0);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent arg0) {
if (noScroll)
return false;
else
return super.onInterceptTouchEvent(arg0);
}
@Override
public void setCurrentItem(int item, boolean smoothScroll) {
super.setCurrentItem(item, smoothScroll);
}
@Override
public void setCurrentItem(int item) {
super.setCurrentItem(item);
}
}
(2)将其复制进入我的代码
修改XML文件为:
android:id="@+id/ViewPager_UserActivity"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
(3)修改JAVA文件:
private NoScrollViewPager ViewPager_UserActivity;
ViewPager_UserActivity = (NoScrollViewPager) findViewById(R.id.ViewPager_UserActivity);
ViewPager_UserActivity.setNoScroll(true);
剩下的与以前都一样,但是运行时会报错:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bjutsport.bjutsport/com.bjutsport.bjutsport.UserActivity}: android.view.InflateException: Binary XML file line #16: Error inflating class com.bjutsport.bjutsport.UserActivity.NoScrollViewPager
Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class com.bjutsport.bjutsport.UserActivity.NoScrollViewPager
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.bjutsport.bjutsport.UserActivity.NoScrollViewPager" on path: DexPathList[[zip file "/data/app/com.bjutsport.bjutsport-2/base.apk"],nativeLibraryDirectories=[/data/app/com.bjutsport.bjutsport-2/lib/arm64, /vendor/lib64, /system/lib64]]