com.duy.pascal.ui.view.BaseRecycleView.java Source code

Java tutorial

Introduction

Here is the source code for com.duy.pascal.ui.view.BaseRecycleView.java

Source

/*
 *  Copyright (c) 2017 Tran Le Duy
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.duy.pascal.ui.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.view.MotionEventCompat;
import android.support.v4.view.ViewConfigurationCompat;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;

public class BaseRecycleView extends RecyclerView {
    private static final int INVALID_POINTER = -1;
    private int mScrollPointerId = INVALID_POINTER;
    private int mInitialTouchX, mInitialTouchY;
    private int mTouchSlop;

    public BaseRecycleView(Context context) {
        this(context, null);
    }

    public BaseRecycleView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BaseRecycleView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        final ViewConfiguration vc = ViewConfiguration.get(getContext());
        mTouchSlop = vc.getScaledTouchSlop();
    }

    @Override
    public void setScrollingTouchSlop(int slopConstant) {
        super.setScrollingTouchSlop(slopConstant);
        final ViewConfiguration vc = ViewConfiguration.get(getContext());
        switch (slopConstant) {
        case TOUCH_SLOP_DEFAULT:
            mTouchSlop = vc.getScaledTouchSlop();
            break;
        case TOUCH_SLOP_PAGING:
            mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(vc);
            break;
        default:
            break;
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent e) {
        final int action = MotionEventCompat.getActionMasked(e);
        final int actionIndex = MotionEventCompat.getActionIndex(e);

        switch (action) {
        case MotionEvent.ACTION_DOWN:
            mScrollPointerId = MotionEventCompat.getPointerId(e, 0);
            mInitialTouchX = (int) (e.getX() + 0.5f);
            mInitialTouchY = (int) (e.getY() + 0.5f);
            return super.onInterceptTouchEvent(e);

        case MotionEventCompat.ACTION_POINTER_DOWN:
            mScrollPointerId = MotionEventCompat.getPointerId(e, actionIndex);
            mInitialTouchX = (int) (MotionEventCompat.getX(e, actionIndex) + 0.5f);
            mInitialTouchY = (int) (MotionEventCompat.getY(e, actionIndex) + 0.5f);
            return super.onInterceptTouchEvent(e);

        case MotionEvent.ACTION_MOVE: {
            final int index = MotionEventCompat.findPointerIndex(e, mScrollPointerId);
            if (index < 0) {
                return false;
            }

            final int x = (int) (MotionEventCompat.getX(e, index) + 0.5f);
            final int y = (int) (MotionEventCompat.getY(e, index) + 0.5f);
            if (getScrollState() != SCROLL_STATE_DRAGGING) {
                final int dx = x - mInitialTouchX;
                final int dy = y - mInitialTouchY;
                final boolean canScrollHorizontally = getLayoutManager().canScrollHorizontally();
                final boolean canScrollVertically = getLayoutManager().canScrollVertically();
                boolean startScroll = false;
                if (canScrollHorizontally && Math.abs(dx) > mTouchSlop
                        && (Math.abs(dx) >= Math.abs(dy) || canScrollVertically)) {
                    startScroll = true;
                }
                if (canScrollVertically && Math.abs(dy) > mTouchSlop
                        && (Math.abs(dy) >= Math.abs(dx) || canScrollHorizontally)) {
                    startScroll = true;
                }
                return startScroll && super.onInterceptTouchEvent(e);
            }
            return super.onInterceptTouchEvent(e);
        }

        default:
            return super.onInterceptTouchEvent(e);
        }
    }
}