blob: b63ef788a2a89482804326b62950be617aa224ae [file] [log] [blame]
Winson Chung94804152015-05-08 13:06:44 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher3;
18
19import android.content.Context;
20import android.support.v7.widget.RecyclerView;
21import android.util.AttributeSet;
22import android.view.MotionEvent;
23import com.android.launcher3.util.Thunk;
24
25/**
26 * A base {@link RecyclerView}, which will NOT intercept a touch sequence unless the scrolling
27 * velocity is below a predefined threshold.
28 */
Winson Chung5f4e0fd2015-05-22 11:12:27 -070029public class BaseRecyclerView extends RecyclerView
Winson Chung94804152015-05-08 13:06:44 -070030 implements RecyclerView.OnItemTouchListener {
31
32 private static final int SCROLL_DELTA_THRESHOLD_DP = 4;
33
34 /** Keeps the last known scrolling delta/velocity along y-axis. */
35 @Thunk int mDy = 0;
36 private float mDeltaThreshold;
Winson Chung94804152015-05-08 13:06:44 -070037
Winson Chung5f4e0fd2015-05-22 11:12:27 -070038 public BaseRecyclerView(Context context) {
Winson Chung94804152015-05-08 13:06:44 -070039 this(context, null);
40 }
41
Winson Chung5f4e0fd2015-05-22 11:12:27 -070042 public BaseRecyclerView(Context context, AttributeSet attrs) {
Winson Chung94804152015-05-08 13:06:44 -070043 this(context, attrs, 0);
44 }
45
Winson Chung5f4e0fd2015-05-22 11:12:27 -070046 public BaseRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) {
Winson Chung94804152015-05-08 13:06:44 -070047 super(context, attrs, defStyleAttr);
48 mDeltaThreshold = getResources().getDisplayMetrics().density * SCROLL_DELTA_THRESHOLD_DP;
49
50 ScrollListener listener = new ScrollListener();
51 setOnScrollListener(listener);
52 }
53
54 private class ScrollListener extends OnScrollListener {
55 public ScrollListener() {
56 // Do nothing
57 }
58
59 @Override
60 public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
61 mDy = dy;
Winson Chung94804152015-05-08 13:06:44 -070062 }
63 }
64
Winson Chung94804152015-05-08 13:06:44 -070065 @Override
66 protected void onFinishInflate() {
67 super.onFinishInflate();
68 addOnItemTouchListener(this);
69 }
70
71 @Override
72 public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent ev) {
73 if (shouldStopScroll(ev)) {
74 stopScroll();
75 }
76 return false;
77 }
78
79 @Override
80 public void onTouchEvent(RecyclerView rv, MotionEvent ev) {
81 // Do nothing.
82 }
83
84 public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
85 // DO NOT REMOVE, NEEDED IMPLEMENTATION FOR M BUILDS
86 }
87
88 /**
Winson Chung94804152015-05-08 13:06:44 -070089 * Returns whether this {@link MotionEvent} should trigger the scroll to be stopped.
90 */
91 protected boolean shouldStopScroll(MotionEvent ev) {
92 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
93 if ((Math.abs(mDy) < mDeltaThreshold &&
94 getScrollState() != RecyclerView.SCROLL_STATE_IDLE)) {
95 // now the touch events are being passed to the {@link WidgetCell} until the
96 // touch sequence goes over the touch slop.
97 return true;
98 }
99 }
100 return false;
101 }
102}