From 77560b32bfe7c6c0ca3aa68bb76701e0857a7f67 Mon Sep 17 00:00:00 2001 From: Tim van Vugt Date: Mon, 16 Apr 2018 13:28:33 +0200 Subject: [PATCH 1/2] Add option to disable calculateScrollForSelectingPosition() function --- .../CarouselLayoutManager.java | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/CarouselLayoutManager/carousel/src/main/java/com/azoft/carousellayoutmanager/CarouselLayoutManager.java b/CarouselLayoutManager/carousel/src/main/java/com/azoft/carousellayoutmanager/CarouselLayoutManager.java index 135985b..3084496 100644 --- a/CarouselLayoutManager/carousel/src/main/java/com/azoft/carousellayoutmanager/CarouselLayoutManager.java +++ b/CarouselLayoutManager/carousel/src/main/java/com/azoft/carousellayoutmanager/CarouselLayoutManager.java @@ -52,6 +52,7 @@ public class CarouselLayoutManager extends RecyclerView.LayoutManager implements private final int mOrientation; private final boolean mCircleLayout; + private final boolean mCalculateScroll; private int mPendingScrollPosition; @@ -71,7 +72,7 @@ public class CarouselLayoutManager extends RecyclerView.LayoutManager implements */ @SuppressWarnings("unused") public CarouselLayoutManager(final int orientation) { - this(orientation, CIRCLE_LAYOUT); + this(orientation, CIRCLE_LAYOUT, true); } /** @@ -82,11 +83,16 @@ public CarouselLayoutManager(final int orientation) { */ @SuppressWarnings("unused") public CarouselLayoutManager(final int orientation, final boolean circleLayout) { + this(orientation, circleLayout, true); + } + + public CarouselLayoutManager(final int orientation, final boolean circleLayout, final boolean calculateScroll) { if (HORIZONTAL != orientation && VERTICAL != orientation) { throw new IllegalArgumentException("orientation should be HORIZONTAL or VERTICAL"); } mOrientation = orientation; mCircleLayout = circleLayout; + mCalculateScroll = calculateScroll; mPendingScrollPosition = INVALID_POSITION; } @@ -358,14 +364,20 @@ public void onLayoutChildren(@NonNull final RecyclerView.Recycler recycler, @Non mPendingScrollPosition = 0 == itemsCount ? INVALID_POSITION : Math.max(0, Math.min(itemsCount - 1, mPendingScrollPosition)); } if (INVALID_POSITION != mPendingScrollPosition) { - mLayoutHelper.mScrollOffset = calculateScrollForSelectingPosition(mPendingScrollPosition, state); + if (mCalculateScroll) { + mLayoutHelper.mScrollOffset = calculateScrollForSelectingPosition(mPendingScrollPosition, state); + } mPendingScrollPosition = INVALID_POSITION; mPendingCarouselSavedState = null; } else if (null != mPendingCarouselSavedState) { - mLayoutHelper.mScrollOffset = calculateScrollForSelectingPosition(mPendingCarouselSavedState.mCenterItemPosition, state); + if (mCalculateScroll) { + mLayoutHelper.mScrollOffset = calculateScrollForSelectingPosition(mPendingCarouselSavedState.mCenterItemPosition, state); + } mPendingCarouselSavedState = null; } else if (state.didStructureChange() && INVALID_POSITION != mCenterItemPosition) { - mLayoutHelper.mScrollOffset = calculateScrollForSelectingPosition(mCenterItemPosition, state); + if (mCalculateScroll) { + mLayoutHelper.mScrollOffset = calculateScrollForSelectingPosition(mCenterItemPosition, state); + } } fillData(recycler, state, childMeasuringNeeded); From 1b1bcc1ff8b139c60e52902515d02ea2b5637e75 Mon Sep 17 00:00:00 2001 From: Tim van Vugt Date: Mon, 16 Apr 2018 14:29:33 +0200 Subject: [PATCH 2/2] Add javadoc --- .../azoft/carousellayoutmanager/CarouselLayoutManager.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CarouselLayoutManager/carousel/src/main/java/com/azoft/carousellayoutmanager/CarouselLayoutManager.java b/CarouselLayoutManager/carousel/src/main/java/com/azoft/carousellayoutmanager/CarouselLayoutManager.java index 3084496..de64117 100644 --- a/CarouselLayoutManager/carousel/src/main/java/com/azoft/carousellayoutmanager/CarouselLayoutManager.java +++ b/CarouselLayoutManager/carousel/src/main/java/com/azoft/carousellayoutmanager/CarouselLayoutManager.java @@ -86,6 +86,11 @@ public CarouselLayoutManager(final int orientation, final boolean circleLayout) this(orientation, circleLayout, true); } + /** + * @param orientation should be {@link #VERTICAL} or {@link #HORIZONTAL} + * @param circleLayout true for enabling circleLayout + * @param calculateScroll whether {@link CarouselLayoutManager#calculateScrollForSelectingPosition(int, RecyclerView.State)} should be called in {@link CarouselLayoutManager#onLayoutChildren(RecyclerView.Recycler, RecyclerView.State)} + */ public CarouselLayoutManager(final int orientation, final boolean circleLayout, final boolean calculateScroll) { if (HORIZONTAL != orientation && VERTICAL != orientation) { throw new IllegalArgumentException("orientation should be HORIZONTAL or VERTICAL");