Skip to content

Commit

Permalink
Fix pagination when the Calendar size is larger than the month size.
Browse files Browse the repository at this point in the history
  • Loading branch information
kizitonwose committed Apr 11, 2020
1 parent 65c56e7 commit dd2b05a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import android.view.View.MeasureSpec.UNSPECIFIED
import android.view.ViewGroup
import androidx.annotation.Px
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.PagerSnapHelper
import androidx.recyclerview.widget.RecyclerView
import com.kizitonwose.calendarview.model.*
import com.kizitonwose.calendarview.ui.*
Expand Down Expand Up @@ -563,7 +562,7 @@ open class CalendarView : RecyclerView {
}
}

private val pagerSnapHelper = PagerSnapHelper()
private val pagerSnapHelper = CalenderPageSnapHelper()

/**
* Setup the CalendarView. You can call this any time to change the
Expand Down
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
}
}

0 comments on commit dd2b05a

Please sign in to comment.