• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 package android.support.car.ui;
17 
18 import android.content.Context;
19 import android.graphics.PointF;
20 import android.support.v7.widget.LinearLayoutManager;
21 import android.support.v7.widget.LinearSmoothScroller;
22 import android.support.v7.widget.RecyclerView;
23 import android.util.Log;
24 import android.view.View;
25 import android.view.animation.AnimationUtils;
26 import android.view.animation.Interpolator;
27 
28 /**
29  * An extension of {@link LinearLayoutManager} that adds some helper methods for paging
30  * such as whether or not it is at the top or bottom of a list and layout param checking.
31  * @hide
32  */
33 public class PagedLayoutManager extends LinearLayoutManager {
34     private static final String TAG = PagedLayoutManager.class.getSimpleName();
35 
36     private final LinearSmoothScroller mSmoothScrollerForDrag;
37     private final LinearSmoothScroller mSmoothScrollerForNonDrag;
38 
39     private int mLastScrollPosition = 0;
40     private boolean mScrollingEnabled = true;
41     public Runnable mItemsChangedRunnable;
42 
PagedLayoutManager(Context context)43     public PagedLayoutManager(Context context) {
44         super(context, VERTICAL, false);
45         mSmoothScrollerForDrag = new SnapToStartSmoothScroller(context, true);
46         mSmoothScrollerForNonDrag = new SnapToStartSmoothScroller(context, true);
47     }
48 
setItemsChangedListener(Runnable runnable)49     public void setItemsChangedListener(Runnable runnable) {
50         mItemsChangedRunnable = runnable;
51     }
52 
53     @Override
onItemsChanged(RecyclerView recyclerView)54     public void onItemsChanged(RecyclerView recyclerView) {
55         super.onItemsChanged(recyclerView);
56         if (mItemsChangedRunnable != null) {
57             mItemsChangedRunnable.run();
58         }
59     }
60 
61     @Override
getExtraLayoutSpace(RecyclerView.State state)62     protected int getExtraLayoutSpace(RecyclerView.State state) {
63         return getHeight() - getPaddingTop() - getPaddingBottom();
64     }
65 
66     @Override
smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position)67     public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state,
68             int position) {
69         boolean forDrag = recyclerView.getScrollState() == RecyclerView.SCROLL_STATE_SETTLING;
70         LinearSmoothScroller ss;
71         if (forDrag) {
72             ss = mSmoothScrollerForDrag;
73         } else {
74             ss = mSmoothScrollerForNonDrag;
75         }
76         ss.setTargetPosition(position);
77         startSmoothScroll(ss);
78     }
79 
getLastScrollPosition()80     public int getLastScrollPosition() {
81         return mLastScrollPosition;
82     }
83 
84     @Override
scrollToPosition(int position)85     public void scrollToPosition(int position) {
86         super.scrollToPosition(position);
87         mLastScrollPosition = position;
88     }
89 
90     @Override
scrollVerticallyBy(int dy, RecyclerView.Recycler r, RecyclerView.State s)91     public int scrollVerticallyBy(int dy, RecyclerView.Recycler r, RecyclerView.State s) {
92         // Our isAtBottom will return true if the view is on screen the the margin extends below
93         // the bottom. This will make it so that you can't scroll if only the margin is hanging
94         // off the bottom.
95         if (isAtBottom() && dy > 0) {
96             return 0;
97         }
98         return super.scrollVerticallyBy(dy, r, s);
99     }
100 
101     @Override
canScrollVertically()102     public boolean canScrollVertically() {
103         return mScrollingEnabled;
104     }
105 
setScrollingEnabled(boolean enabled)106     public void setScrollingEnabled(boolean enabled) {
107         mScrollingEnabled = enabled;
108     }
109 
isAtTop()110     public boolean isAtTop() {
111         if (getChildCount() == 0 || getItemCount() == 0) {
112             return true;
113         }
114         return findFirstCompletelyVisibleItemPosition() < 1;
115     }
116 
isAtBottom()117     public boolean isAtBottom() {
118         if (getChildCount() == 0 || getItemCount() == 0) {
119             return true;
120         }
121         return findLastCompletelyVisibleItemPosition() == getItemCount() - 1;
122     }
123 
124     private class SnapToStartSmoothScroller extends LinearSmoothScroller {
125         private static final int DURATION_MS = 500;
126         private final Interpolator mInterpolator;
127 
SnapToStartSmoothScroller(Context context, boolean forDrag)128         public SnapToStartSmoothScroller(Context context, boolean forDrag) {
129             super(context);
130             int interpolator = forDrag ? android.R.interpolator.decelerate_quint :
131                     android.R.interpolator.fast_out_slow_in;
132             mInterpolator = AnimationUtils.loadInterpolator(context, interpolator);
133         }
134 
135         @Override
onTargetFound(View targetView, RecyclerView.State state, Action action)136         protected void onTargetFound(View targetView, RecyclerView.State state, Action action) {
137             int dy = calculateDyToMakeVisible(targetView, SNAP_TO_START);
138             if (dy == 0) {
139                 Log.w(TAG, "Scroll distance is 0.");
140                 return;
141             }
142 
143             action.update(0, -dy, DURATION_MS, mInterpolator);
144         }
145 
146         @Override
computeScrollVectorForPosition(int targetPosition)147         public PointF computeScrollVectorForPosition(int targetPosition) {
148             return PagedLayoutManager.this
149                     .computeScrollVectorForPosition(targetPosition);
150         }
151     }
152 }
153