-
Notifications
You must be signed in to change notification settings - Fork 529
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix pagination when the Calendar size is larger than the month size.
- Loading branch information
1 parent
65c56e7
commit dd2b05a
Showing
2 changed files
with
51 additions
and
2 deletions.
There are no files selected for viewing
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
50 changes: 50 additions & 0 deletions
50
library/src/main/java/com/kizitonwose/calendarview/ui/CalenderPageSnapHelper.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.kizitonwose.calendarview.ui | ||
|
||
import android.view.View | ||
import androidx.recyclerview.widget.OrientationHelper | ||
import androidx.recyclerview.widget.PagerSnapHelper | ||
import androidx.recyclerview.widget.RecyclerView | ||
|
||
class CalenderPageSnapHelper : PagerSnapHelper() { | ||
|
||
/** | ||
* The default implementation of this method in [PagerSnapHelper.calculateDistanceToFinalSnap] uses the distance | ||
* between the target view center vs RecyclerView center as final snap distance. This does not always give the | ||
* desired result for calendar usage. For example in a vertical calendar when the RecyclerView is taller than | ||
* the item view(e.g two or more visible months), we don't actually want the item view's center to be at the | ||
* center of the RecyclerView when it snaps but instead we want the item view and RecyclerView top(in vertical) | ||
* or left(in horizontal) to match at the end of the snap. | ||
*/ | ||
override fun calculateDistanceToFinalSnap(layoutManager: RecyclerView.LayoutManager, targetView: View): IntArray { | ||
return IntArray(2).apply { | ||
this[0] = if (layoutManager.canScrollHorizontally()) | ||
distanceToStart(targetView, getHorizontalHelper(layoutManager)) else 0 | ||
|
||
this[1] = if (layoutManager.canScrollVertically()) | ||
distanceToStart(targetView, getVerticalHelper(layoutManager)) else 0 | ||
} | ||
} | ||
|
||
private fun distanceToStart(targetView: View, helper: OrientationHelper): Int { | ||
val childStart = (helper.getDecoratedStart(targetView)) | ||
val containerStart = helper.startAfterPadding | ||
return childStart - containerStart | ||
} | ||
|
||
private lateinit var verticalHelper: OrientationHelper | ||
private lateinit var horizontalHelper: OrientationHelper | ||
|
||
private fun getVerticalHelper(layoutManager: RecyclerView.LayoutManager): OrientationHelper { | ||
if (!::verticalHelper.isInitialized || verticalHelper.layoutManager != layoutManager) { | ||
verticalHelper = OrientationHelper.createVerticalHelper(layoutManager) | ||
} | ||
return verticalHelper | ||
} | ||
|
||
private fun getHorizontalHelper(layoutManager: RecyclerView.LayoutManager): OrientationHelper { | ||
if (!::horizontalHelper.isInitialized || horizontalHelper.layoutManager != layoutManager) { | ||
horizontalHelper = OrientationHelper.createHorizontalHelper(layoutManager) | ||
} | ||
return horizontalHelper | ||
} | ||
} |