|
22 | 22 | */ |
23 | 23 | public class FlowLayoutManager extends RecyclerView.LayoutManager { |
24 | 24 |
|
25 | | - private static final String LOG_TAG = "FlowLayoutManager"; |
| 25 | + private static final String LOG_TAG = "FlowLayoutManager"; |
| 26 | + |
| 27 | + private static final int SCROLL_UP = -1; |
| 28 | + private static final int SCROLL_DOWN = 1; |
| 29 | + |
26 | 30 | private RecyclerView recyclerView; |
27 | 31 | private int firstChildAdapterPosition = 0; |
28 | 32 | private RecyclerView.Recycler recyclerRef; |
@@ -195,23 +199,28 @@ public boolean canScrollVertically() { |
195 | 199 | return false; |
196 | 200 | } |
197 | 201 |
|
198 | | - View firstChild = getChildAt(0); |
199 | | - View lastChild = getChildAt(getChildCount() - 1); |
200 | | - View topChild = getChildAt(getMaxHeightLayoutPositionInLine(0)); |
201 | | - View bottomChild = getChildAt(getMaxHeightLayoutPositionInLine(getChildCount() - 1)); |
202 | | - boolean topReached = false, bottomReached = false; |
203 | | - if (getChildAdapterPosition(firstChild) == 0) { |
204 | | - if (getDecoratedTop(topChild) >= topVisibleEdge()) { |
205 | | - topReached = true; |
206 | | - } |
207 | | - } |
| 202 | + return canScrollVertically(SCROLL_UP) || canScrollVertically(SCROLL_DOWN); |
| 203 | + } |
208 | 204 |
|
209 | | - if (getChildAdapterPosition(lastChild) == recyclerView.getAdapter().getItemCount() - 1) { |
210 | | - if (getDecoratedBottom(bottomChild) <= bottomVisibleEdge()) { |
211 | | - bottomReached = true; |
212 | | - } |
| 205 | + /** |
| 206 | + * Check if this view can be scrolled vertically in a certain direction. |
| 207 | + * |
| 208 | + * @param direction Negative to check scrolling up, positive to check scrolling down. |
| 209 | + * @return true if this view can be scrolled in the specified direction, false otherwise. |
| 210 | + */ |
| 211 | + public boolean canScrollVertically(final int direction) { |
| 212 | + if (direction < 0) { |
| 213 | + final View firstChild = getChildAt(0); |
| 214 | + final View topChild = getChildAt(getMaxHeightLayoutPositionInLine(0)); |
| 215 | + |
| 216 | + return !(getChildAdapterPosition(firstChild) == 0 && getDecoratedTop(topChild) >= topVisibleEdge()); |
| 217 | + } else { |
| 218 | + View lastChild = getChildAt(getChildCount() - 1); |
| 219 | + View bottomChild = getChildAt(getMaxHeightLayoutPositionInLine(getChildCount() - 1)); |
| 220 | + |
| 221 | + return !(getChildAdapterPosition(lastChild) == recyclerView.getAdapter().getItemCount() - 1 |
| 222 | + && (bottomChild != null && getDecoratedBottom(bottomChild) <= bottomVisibleEdge())); |
213 | 223 | } |
214 | | - return !(topReached && bottomReached); |
215 | 224 | } |
216 | 225 |
|
217 | 226 | @Override |
|
0 commit comments