本文共 783 字,大约阅读时间需要 2 分钟。
最近做项目要求某种情况下ViewPager不能滑动,在网上找了一圈。有说重写Ontouch的,各种。都试了试,基本都不可用。 自己看ViewPager源码,知道VIewPager移动全部都调用了scrollTo方法,这个是View的方法,那么我们只需要重写这个方法就可以禁止ViewPager滑动 - public class CustomViewPager extends ViewPager {
-
- private boolean isCanScroll = true;
-
- public CustomViewPager(Context context) {
- super(context);
- }
-
- public CustomViewPager(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
-
- public void setScanScroll(boolean isCanScroll){
- this.isCanScroll = isCanScroll;
- }
-
-
- @Override
- public void scrollTo(int x, int y){
- if (isCanScroll){
- super.scrollTo(x, y);
- }
- }
此时,ViewPager.setCurretItem方法也会失效,想通过点击Tab button 切换page也无法实现。
二、setOnTouchListener
ViewPager.setOnTouchListener(new View.OnTouchListener(){
on touch ( xx ){
return true;
}
});
这种方式可以实现只禁止滑动翻页,可以通过setCurrentItem,来切换页面。
转载地址:http://waaab.baihongyu.com/