blob: 5b30e3df63aea788e513bd086e4301f589ddca9b [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 */
29public class BaseContainerRecyclerView extends RecyclerView
30 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;
37 private RecyclerView.OnScrollListener mScrollListenerProxy;
38
39 public BaseContainerRecyclerView(Context context) {
40 this(context, null);
41 }
42
43 public BaseContainerRecyclerView(Context context, AttributeSet attrs) {
44 this(context, attrs, 0);
45 }
46
47 public BaseContainerRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) {
48 super(context, attrs, defStyleAttr);
49 mDeltaThreshold = getResources().getDisplayMetrics().density * SCROLL_DELTA_THRESHOLD_DP;
50
51 ScrollListener listener = new ScrollListener();
52 setOnScrollListener(listener);
53 }
54
55 private class ScrollListener extends OnScrollListener {
56 public ScrollListener() {
57 // Do nothing
58 }
59
60 @Override
61 public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
62 mDy = dy;
63 if (mScrollListenerProxy != null) {
64 mScrollListenerProxy.onScrolled(recyclerView, dx, dy);
65 }
66 }
67 }
68
69 /**
70 * Sets an additional scroll listener, only needed for LMR1 version of the support lib.
71 */
72 public void setOnScrollListenerProxy(RecyclerView.OnScrollListener listener) {
73 mScrollListenerProxy = listener;
74 }
75
76 @Override
77 protected void onFinishInflate() {
78 super.onFinishInflate();
79 addOnItemTouchListener(this);
80 }
81
82 @Override
83 public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent ev) {
84 if (shouldStopScroll(ev)) {
85 stopScroll();
86 }
87 return false;
88 }
89
90 @Override
91 public void onTouchEvent(RecyclerView rv, MotionEvent ev) {
92 // Do nothing.
93 }
94
95 public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
96 // DO NOT REMOVE, NEEDED IMPLEMENTATION FOR M BUILDS
97 }
98
99 /**
100 * Returns whether this {@link MotionEvent} should trigger the scroll to be stopped.
101 */
102 protected boolean shouldStopScroll(MotionEvent ev) {
103 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
104 if ((Math.abs(mDy) < mDeltaThreshold &&
105 getScrollState() != RecyclerView.SCROLL_STATE_IDLE)) {
106 // now the touch events are being passed to the {@link WidgetCell} until the
107 // touch sequence goes over the touch slop.
108 return true;
109 }
110 }
111 return false;
112 }
113}