kizitonwose / Calendar

A highly customizable calendar view and compose library for Android and Kotlin Multiplatform.
MIT License
4.72k stars 509 forks source link

Sticky headers #33

Closed acolombo11 closed 5 years ago

acolombo11 commented 5 years ago

In my vertical scrolling calendar (similar to example2) I would like to have sticky headers. I've tried using this solution making a custom RecyclerView.ItemDecoration but, with "CalendarAdapter" being internal and not using the "viewType" parameter, I find it really difficult to make it work . Do you have any suggestions in how to implement such a thing? Internal or external to the library I could implement it myself, I'm just seeking some advice.

kizitonwose commented 5 years ago

I played around with example 2 to achieve something like this. Since CalendarView extends from RecyclerView, I added a scroll listener and just updated a view which I put above the weekday texts on the toolbar using calendarView.findFirstVisibleMonth().

Here's the result:

Sticky header

Here's the code:

calendarView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
    override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
        calendarView.findFirstVisibleMonth()?.let { calendarMonth ->
            stickyHeaderTextView.text = "${calendarMonth.yearMonth.month.name.toLowerCase().capitalize()} ${calendarMonth.year}"
        }
    }
})
acolombo11 commented 5 years ago

It's not exactly what I wanted because using the previously mentioned RecyclerView.ItemDecoration the current header gets pushed away from the previous one. But still the result you helped me getting is totally acceptable, so thank you really much.

Here's the result: ScreenRecord_20190724-121133_1 .

kizitonwose commented 5 years ago

Glad I was able to help! Closing this now.