Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fallback to findFirstVisibleItemPosition if findFirstCompletelyVisibleItemPosition is reporting NO_POSITION to handle recyclerviews that have enough padding to be able to snap all items  (rubensousa#49)
  • Loading branch information
jt-gilkeson authored Sep 27, 2018
1 parent 5c696da commit 553d2f0
Showing 1 changed file with 5 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,13 @@ private int getSnappedPosition(@NonNull RecyclerView recyclerView) {
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();

if (layoutManager instanceof LinearLayoutManager) {
LinearLayoutManager linearLayoutManager = (LinearLayoutManager)layoutManager;
if (gravity == Gravity.START || gravity == Gravity.TOP) {
return ((LinearLayoutManager) layoutManager).findFirstCompletelyVisibleItemPosition();
int pos = linearLayoutManager.findFirstCompletelyVisibleItemPosition();
return (pos != RecyclerView.NO_POSITION) ? pos : linearLayoutManager.findFirstVisibleItemPosition();
} else if (gravity == Gravity.END || gravity == Gravity.BOTTOM) {
return ((LinearLayoutManager) layoutManager).findLastCompletelyVisibleItemPosition();
int pos = linearLayoutManager.findLastCompletelyVisibleItemPosition();
return (pos != RecyclerView.NO_POSITION) ? pos : linearLayoutManager.findLastVisibleItemPosition();
}
}

Expand Down

0 comments on commit 553d2f0

Please sign in to comment.