Skip to content

Commit

Permalink
Fix: Clipped cells in the calendar view don't respond to click events.
Browse files Browse the repository at this point in the history
  • Loading branch information
kizitonwose committed Apr 9, 2020
1 parent f0de38b commit 614b57f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -583,9 +583,6 @@ open class CalendarView : RecyclerView {
this.endMonth = endMonth
this.firstDayOfWeek = firstDayOfWeek

clipToPadding = false
clipChildren = false //#ClipChildrenFix

// Remove the listener before adding again to prevent
// multiple additions if we already added it before.
removeOnScrollListener(scrollListenerInternal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ internal class CalendarAdapter(
val rootLayout = LinearLayout(context).apply {
orientation = LinearLayout.VERTICAL
id = rootViewId
clipChildren = false //#ClipChildrenFix
}

if (viewConfig.monthHeaderRes != 0) {
Expand All @@ -83,7 +82,6 @@ internal class CalendarAdapter(
layoutParams = LinearLayout.LayoutParams(LP.WRAP_CONTENT, LP.WRAP_CONTENT)
orientation = LinearLayout.VERTICAL
id = bodyViewId
clipChildren = false //#ClipChildrenFix
}
rootLayout.addView(monthBodyLayout)

Expand All @@ -99,7 +97,8 @@ internal class CalendarAdapter(
}

fun setupRoot(root: ViewGroup) {
ViewCompat.setPaddingRelative(root,
ViewCompat.setPaddingRelative(
root,
calView.monthPaddingStart, calView.monthPaddingTop,
calView.monthPaddingEnd, calView.monthPaddingBottom
)
Expand Down Expand Up @@ -204,17 +203,19 @@ internal class CalendarAdapter(
val visibleVH =
calView.findViewHolderForAdapterPosition(visibleItemPos) as? MonthViewHolder ?: return
val newHeight = visibleVH.headerView?.height.orZero() +
// For some reason `visibleVH.bodyLayout.height` does not give us the updated height.
// So we calculate it again by checking the number of visible(non-empty) rows.
// visibleVH.bodyLayout.height` won't not give us the right height as it differs
// depending on row count in the month. So we calculate the appropriate height
// by checking the number of visible(non-empty) rows.
visibleMonth.weekDays.size * calView.dayHeight +
visibleVH.footerView?.height.orZero()
if (calView.layoutParams.height != newHeight)
if (calView.layoutParams.height != newHeight) {
calView.layoutParams = calView.layoutParams.apply {
this.height = newHeight
// If we reset the calendar's height from a short item view's height(month with 5 rows)
// to a longer one(month with 6 rows), the row outside the old height is not drawn.
// This is fixed by setting `clipChildren = false` on all parents. #ClipChildrenFix
}
visibleVH.itemView.layoutParams = visibleVH.itemView.layoutParams.apply {
this.height = newHeight
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ internal class WeekHolder(dayConfig: DayConfig) {
)
orientation = LinearLayout.HORIZONTAL
weightSum = dayHolders.count().toFloat()
clipChildren = false //#ClipChildrenFix
for (holder in dayHolders) {
addView(holder.inflateDayView(this))
}
Expand Down
17 changes: 6 additions & 11 deletions sample/src/main/res/layout/example_1_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,38 +51,33 @@

</com.google.android.material.appbar.AppBarLayout>

<androidx.constraintlayout.widget.ConstraintLayout
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.kizitonwose.calendarview.CalendarView
android:id="@+id/exOneCalendar"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_height="wrap_content"
app:cv_dayViewResource="@layout/example_1_calendar_day"
app:cv_orientation="horizontal"
app:cv_outDateStyle="endOfRow"
app:cv_inDateStyle="allMonths"
app:cv_scrollMode="paged"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:cv_scrollMode="paged" />

<CheckBox
android:id="@+id/weekModeCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="40dp"
android:layout_marginBottom="20dp"
android:buttonTint="@color/example_1_white"
android:padding="2dp"
android:text="@string/week_mode"
android:textColor="@color/example_1_white"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
android:layout_gravity="bottom|center_horizontal" />

</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>


</LinearLayout>

0 comments on commit 614b57f

Please sign in to comment.