Skip to content

Commit bc5a9ad

Browse files
Merge pull request #6 from untitledstartup/master
Added option to specify direction to `canScrollVertically` method, bumped version to 1.6
2 parents a6b2f55 + 3ce390b commit bc5a9ad

2 files changed

Lines changed: 27 additions & 18 deletions

File tree

flowlayoutmanager/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ apply plugin: 'com.android.library'
44
apply plugin: 'com.jfrog.bintray'
55

66
project.ext {
7-
libVersion = '1.5'
8-
libVersionCode = 15
7+
libVersion = '1.6'
8+
libVersionCode = 16
99
libGroupId = 'com.xiaofeng.android'
1010
libArtifactId = 'flowlayoutmanager'
1111
gitUrl = 'git@github.com:xiaofeng-han/AndroidLibs.git'

flowlayoutmanager/src/main/java/com/xiaofeng/flowlayoutmanager/FlowLayoutManager.java

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@
2222
*/
2323
public class FlowLayoutManager extends RecyclerView.LayoutManager {
2424

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+
2630
private RecyclerView recyclerView;
2731
private int firstChildAdapterPosition = 0;
2832
private RecyclerView.Recycler recyclerRef;
@@ -195,23 +199,28 @@ public boolean canScrollVertically() {
195199
return false;
196200
}
197201

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+
}
208204

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()));
213223
}
214-
return !(topReached && bottomReached);
215224
}
216225

217226
@Override

0 commit comments

Comments
 (0)