androidandroid-jetpack-composesticky

Implementaion of Double StickyHeader in Jetpack compose


I am new to Jetpack Compose and would like to stack double headers (like sticky headers but double). Here is a repository that I want to implement exactly the same as this: https://github.com/Kenber/DoubleStickyHeadersList.

In Jetpack Compose, there is a Google pre-made interface called stickyHeader. I would like to customize this to make it double-stackable. However, I could not find the relevant code from Google open source: https://cs.android.com/search?q=stickyheader&ss=androix&start=51, and I found it to be quite a significant amount of work.

As far as I know, in the view system, you can customize ScrollView, but it seems very difficult to implement this in Jetpack Compose. Do you have any ideas on how to achieve this?


Solution

  • So, one of the ideas I got from : https://github.com/BILLyTheLiTTle/LazyColumns/blob/main/Guidelines/Double_Header_LazyColumn.md

    is using stickyHeader {} with AnimatedVisibility. and the AnimatedVisibility is visible by its scroll state. whether it is over the first stickyHeader or not. the logic of checking the scroll state:

    if (listState.firstVisibleItemIndex < firstItemIndex.value) {...} // scrolling up
    else if (listState.firstVisibleItemIndex > firstItemIndex.value) {...} // scrolling down
    

    Save the current header key(the key of parent stickyHeader) and populate(set visible true) the current header if the type is the same.

    AnimatedVisibility(visible = header.value != type, ...