Created
August 5, 2017 07:16
-
-
Save TakuSemba/803ba3aeca691ede414a8cd5c72c6f1d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
View findSnapView(RecyclerView.LayoutManager layoutManager) { | |
OrientationHelper helper = layoutManager.canScrollHorizontally() | |
? OrientationHelper.createHorizontalHelper(layoutManager) | |
: OrientationHelper.createVerticalHelper(layoutManager); | |
int childCount = layoutManager.getChildCount(); | |
View closestChild = null; | |
int containerStart = helper.getStartAfterPadding(); | |
int absClosest = Integer.MAX_VALUE; | |
for (int i = 0; i < childCount; i++) { | |
int childStart = helper.getDecoratedStart(targetView); | |
int absDistance = Math.abs(childCenter - containerCenter); | |
if (absDistance < absClosest) { | |
absClosest = absDistance; | |
closestChild = child; | |
} | |
} | |
return closestChild; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment