Skip to content

Commit

Permalink
Fix snap listener not reporting values for vertical snaps correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensousa committed Aug 17, 2019
1 parent d9f001d commit 915b3f2
Showing 1 changed file with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import android.view.Gravity;
import android.view.View;

import java.util.Locale;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.text.TextUtilsCompat;
Expand All @@ -30,6 +28,8 @@
import androidx.recyclerview.widget.OrientationHelper;
import androidx.recyclerview.widget.RecyclerView;

import java.util.Locale;

class GravityDelegate {

private OrientationHelper verticalHelper;
Expand All @@ -38,19 +38,19 @@ class GravityDelegate {
private boolean isRtl;
private boolean snapLastItem;
private GravitySnapHelper.SnapListener listener;
private boolean snapping;
private int lastSnappedPosition;
private int currentSnappedPosition;
private boolean isScrolling = false;
private RecyclerView recyclerView;
private RecyclerView.OnScrollListener scrollListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == RecyclerView.SCROLL_STATE_IDLE && snapping && listener != null) {
if (lastSnappedPosition != RecyclerView.NO_POSITION) {
listener.onSnap(lastSnappedPosition);
if (newState == RecyclerView.SCROLL_STATE_IDLE && listener != null) {
if (currentSnappedPosition != RecyclerView.NO_POSITION && isScrolling) {
listener.onSnap(currentSnappedPosition);
}
snapping = false;
}
isScrolling = newState != RecyclerView.SCROLL_STATE_IDLE;
}
};

Expand Down Expand Up @@ -165,9 +165,10 @@ public View findSnapView(RecyclerView.LayoutManager layoutManager) {
snapView = findEdgeView(lm, getVerticalHelper(lm), false);
break;
}
snapping = snapView != null;
if (snapView != null) {
lastSnappedPosition = recyclerView.getChildAdapterPosition(snapView);
currentSnappedPosition = recyclerView.getChildAdapterPosition(snapView);
} else {
currentSnappedPosition = RecyclerView.NO_POSITION;
}
return snapView;
}
Expand Down Expand Up @@ -258,7 +259,9 @@ private View findEdgeView(LinearLayoutManager lm, OrientationHelper helper, bool

private boolean isAtEndOfList(LinearLayoutManager lm) {
if ((!lm.getReverseLayout() && gravity == Gravity.START)
|| (lm.getReverseLayout() && gravity == Gravity.END)) {
|| (lm.getReverseLayout() && gravity == Gravity.END)
|| (!lm.getReverseLayout() && gravity == Gravity.TOP)
|| (lm.getReverseLayout() && gravity == Gravity.BOTTOM)) {
return lm.findLastCompletelyVisibleItemPosition() == lm.getItemCount() - 1;
} else {
return lm.findFirstCompletelyVisibleItemPosition() == 0;
Expand Down

0 comments on commit 915b3f2

Please sign in to comment.