package com.atmob.keyboard_android.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;

import androidx.viewpager.widget.ViewPager;

/**
 * 支持禁止滑动的ViewPager
 */
public class NoSwipeViewPager extends ViewPager {
    /**
     * 是否允许滑动，默认为支持
     */
    private boolean isSwipeEnabled = true;

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

    public NoSwipeViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        return isSwipeEnabled && super.onInterceptTouchEvent(event);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return isSwipeEnabled && super.onTouchEvent(event);
    }

    /**
     * 设置是否可以滑动
     */
    public void setSwipeEnabled(boolean enabled) {
        isSwipeEnabled = enabled;
    }
}