From 614b57f93c187f49dad62f51830857c3acef4460 Mon Sep 17 00:00:00 2001 From: Kizito Nwose Date: Thu, 9 Apr 2020 19:23:02 +0200 Subject: [PATCH] Fix: Clipped cells in the calendar view don't respond to click events. --- .../kizitonwose/calendarview/CalendarView.kt | 3 --- .../calendarview/ui/CalendarAdapter.kt | 19 ++++++++++--------- .../kizitonwose/calendarview/ui/WeekHolder.kt | 1 - .../main/res/layout/example_1_fragment.xml | 17 ++++++----------- 4 files changed, 16 insertions(+), 24 deletions(-) diff --git a/library/src/main/java/com/kizitonwose/calendarview/CalendarView.kt b/library/src/main/java/com/kizitonwose/calendarview/CalendarView.kt index a97ce5c9..90baf49e 100644 --- a/library/src/main/java/com/kizitonwose/calendarview/CalendarView.kt +++ b/library/src/main/java/com/kizitonwose/calendarview/CalendarView.kt @@ -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) diff --git a/library/src/main/java/com/kizitonwose/calendarview/ui/CalendarAdapter.kt b/library/src/main/java/com/kizitonwose/calendarview/ui/CalendarAdapter.kt index 3833ba0a..78513d80 100644 --- a/library/src/main/java/com/kizitonwose/calendarview/ui/CalendarAdapter.kt +++ b/library/src/main/java/com/kizitonwose/calendarview/ui/CalendarAdapter.kt @@ -65,7 +65,6 @@ internal class CalendarAdapter( val rootLayout = LinearLayout(context).apply { orientation = LinearLayout.VERTICAL id = rootViewId - clipChildren = false //#ClipChildrenFix } if (viewConfig.monthHeaderRes != 0) { @@ -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) @@ -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 ) @@ -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 + } + } } } } diff --git a/library/src/main/java/com/kizitonwose/calendarview/ui/WeekHolder.kt b/library/src/main/java/com/kizitonwose/calendarview/ui/WeekHolder.kt index 2ec996c1..145b6d9b 100644 --- a/library/src/main/java/com/kizitonwose/calendarview/ui/WeekHolder.kt +++ b/library/src/main/java/com/kizitonwose/calendarview/ui/WeekHolder.kt @@ -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)) } diff --git a/sample/src/main/res/layout/example_1_fragment.xml b/sample/src/main/res/layout/example_1_fragment.xml index 8a339dc1..d2bf480a 100644 --- a/sample/src/main/res/layout/example_1_fragment.xml +++ b/sample/src/main/res/layout/example_1_fragment.xml @@ -51,38 +51,33 @@ - + app:cv_scrollMode="paged" /> + android:layout_gravity="bottom|center_horizontal" /> - + \ No newline at end of file