关于ViewPager禁止滑动和页面切换问题-笔记

此文参照:http://blog.sina.com.cn/s/blog_8a86f4dd0102vlx1.html

/**
 * Created by  Quentin on  2016/6/29 15:46
 **/
public class CustomViewPager extends ViewPager {

    private boolean isCanScroll = false;

    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public CustomViewPager(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }
   /**
     * 可以动态修改
     */
    public void setCanScroll(boolean isCanScroll){
        this.isCanScroll = isCanScroll;
    }
    @Override
    public boolean onTouchEvent(MotionEvent arg0) {
        // TODO Auto-generated method stub
        if(isCanScroll){
            return super.onTouchEvent(arg0);
        }else{
            return false;
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent arg0) {
        // TODO Auto-generated method stub
        if(isCanScroll){
            return super.onInterceptTouchEvent(arg0);
        }else{
            return false;
        }

    }
}
  //viewpage切换时,设置成false就是不需要滑动时间,即可解决出现切换闪屏的效果
  @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        //选择显示点击了的fragment
        switch (checkedId) {
            case R.id.rb_bottombar_home:
                vpMainViewpager.setCurrentItem(0,false);
                break;
            case R.id.rb_bottombar_message:
                vpMainViewpager.setCurrentItem(1,false);
                break;
            case R.id.rb_bottombar_logging:
                vpMainViewpager.setCurrentItem(2,false);
                break;
            case R.id.rb_bottombar_mine:
                vpMainViewpager.setCurrentItem(3,false);
                break;
        }
    }

你可能感兴趣的:(学习笔记)