美文网首页
SwipeRefreshLayout嵌套水平滑动的Recycle

SwipeRefreshLayout嵌套水平滑动的Recycle

作者: android难民 | 来源:发表于2017-01-03 11:50 被阅读254次

在使用水平滑动的RecycleView时,外面加了SwipeRefreshLayout,左右滑动会触发到下拉刷新事件,SwipeRefreshLayout对这个十分敏感好像,如果完全左右滑动是看不出,只有慢慢地滑动才会出现。所以得重写SwipeRefreshlayout,要认为我们在左右滑动时不出发刷新事件。

public class VerticalSwipeRefreshLayout extends SwipeRefreshLayout {

    private int mTouchSlop;
    // 上一次触摸时的X坐标
    private float mPrevX;

    public VerticalSwipeRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        // 触发移动事件的最短距离,如果小于这个距离就不触发移动控件
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mPrevX = event.getX();
                break;

            case MotionEvent.ACTION_MOVE:
                final float eventX = event.getX();
                float xDiff = Math.abs(eventX - mPrevX);
                // 增加60的容差,让下拉刷新在竖直滑动时就可以触发
                if (xDiff > mTouchSlop + 60) {
                    return false;
                }
        }

        return super.onInterceptTouchEvent(event);
    }
}

相关文章

网友评论

      本文标题:SwipeRefreshLayout嵌套水平滑动的Recycle

      本文链接:https://www.haomeiwen.com/subject/rtogvttx.html