recycleview监听滑动状态,滑动位置,滑动方向

1,494 阅读1分钟
/**
 * 监听recycleview滑动到顶部,底部,向前滑,向下滑
 */
public class RecycleViewScrollListener extends RecyclerView.OnScrollListener {

    //滑动位置参数
    public static final int SCROLL_TOP = 1; //顶部
    public static final int SCROLL_CENTER = 0;  //中间
    public static final int SCROLL_BOTTOM = -1; //底部
    //RecycleView方向
    @Orientation
    private int orientation;

    private OnScrollStateListener mOnScrollStateListener;
    private OnScrollPositionListener mOnScrollPositionListener;
    private OnScrollDirectionListener mOnScrollDirectionListener;

    public RecycleViewScrollListener(@Orientation int orientation) {
        this.orientation = orientation;
    }

    public void setOnScrollStateListener(OnScrollStateListener listener) {
        this.mOnScrollStateListener = listener;
    }

    public void setOnScrollPositionListener(OnScrollPositionListener listener) {
        this.mOnScrollPositionListener = listener;
    }

    public void setOnScrollDirectionListener(OnScrollDirectionListener listener) {
        this.mOnScrollDirectionListener = listener;
    }

    @Override
    public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
        if (mOnScrollStateListener != null) {
            mOnScrollStateListener.scrollState(recyclerView.getScrollState() != 0);
        }

        if (mOnScrollPositionListener != null) {
            if (orientation == LinearLayoutManager.VERTICAL) {
                if (!recyclerView.canScrollVertically(-1)) {
                    mOnScrollPositionListener.scrollPosition(SCROLL_TOP);
                } else if (!recyclerView.canScrollVertically(1)) {
                    mOnScrollPositionListener.scrollPosition(SCROLL_BOTTOM);
                } else {
                    mOnScrollPositionListener.scrollPosition(SCROLL_CENTER);
                }
            } else {
                if (!recyclerView.canScrollHorizontally(-1)) {
                    mOnScrollPositionListener.scrollPosition(SCROLL_TOP);
                } else if (!recyclerView.canScrollHorizontally(1)) {
                    mOnScrollPositionListener.scrollPosition(SCROLL_BOTTOM);
                } else {
                    mOnScrollPositionListener.scrollPosition(SCROLL_CENTER);
                }
            }
        }

        if (mOnScrollDirectionListener != null) {
            if (dy < 0) {
                mOnScrollDirectionListener.scrollUp(dy);
            } else if (dy > 0) {
                mOnScrollDirectionListener.scrollDown(dy);
            }
        }
    }

    /**
     * 是否在滑动监听
     */
    public interface OnScrollStateListener {

        void scrollState(boolean isScrolling);
    }

    /**
     * 滑动位置监听
     */
    public interface OnScrollPositionListener {

        void scrollPosition(int position);
    }

    /**
     * 滑动方向监听
     */
    public interface OnScrollDirectionListener {
        //向前滑动
        void scrollUp(int dy);

        //向后滑动
        void scrollDown(int dy);
    }

    @IntDef({LinearLayoutManager.VERTICAL, LinearLayoutManager.HORIZONTAL})
    @Retention(RetentionPolicy.SOURCE)
    public @interface Orientation {

    }
}