Android 禁止ViewPager滑动



今天项目中要用到ViewPager禁止滑动,直接简单粗暴的上代码吧:

package com.tao.viewpagerdamo;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

/**
 * @author WangJintao
 * @date 2015-12-26 下午4:14:37 类说明
 */
public class MyViewPager extends ViewPager
{

    public MyViewPager(Context context)
    {
        super(context);
    }

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

    private boolean isCanScroll = false;

    public void setCanScroll(boolean isCanScroll)
    {
        this.isCanScroll = isCanScroll;
    }

    @Override
    public boolean onTouchEvent(MotionEvent arg0)
    {
        if (isCanScroll)
        {
            return super.onTouchEvent(arg0);
        }
        else
        {
            return false;
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent arg0)
    {
        if (isCanScroll)
        {
            return super.onInterceptTouchEvent(arg0);
        }
        else
        {
            return false;
        }
    }

}


你可能感兴趣的:(android)