filipkowicz / HeaderItemDecorationExample

Simple Example how to use HeaderItemDecoration from gist https://gist.github.com/filipkowicz/1a769001fae407b8813ab4387c42fcbd
14 stars 5 forks source link

Headers don't stay when using the library #3

Open MiguelRossi opened 4 years ago

MiguelRossi commented 4 years ago

Tried the library today (v0.8.1 and v0.8.2), and the headers disappear just after a small scrolling.

MiguelRossi commented 4 years ago

Apparently how topChild is retrieved in onDrawOver() has something to do with it.

filipkowicz commented 4 years ago

can you provide example or movie/gif?

MiguelRossi commented 4 years ago

Here a .gif:

Recording

MiguelRossi commented 4 years ago

I found that in this piece of code:

val topChild = parent.findChildViewUnder(
    parent.paddingLeft.toFloat(),
    parent.paddingTop.toFloat()
) ?: return

there is nothing under parent.paddingLeft.toFloat() because the items in the list have margin.

When I delete all the margins the headers stick perfectly: Screen Recording 2020-01-06 at 12 15 33

When I delete lateral the margins headers blink: Screen Recording 2020-01-06 at 12 14 35

MiguelRossi commented 4 years ago

I've done a quick fix as a suggestion. Instead of:

val topChild = parent.findChildViewUnder(
    parent.paddingLeft.toFloat(),
    parent.paddingTop.toFloat()
) ?: return

this:

val currentTopChild = parent.findChildViewUnder(
    parent.width.toFloat() / 2,
    parent.paddingTop.toFloat()
)

val nextTopChild = parent.findChildViewUnder(
    parent.width.toFloat() / 2,
    parent.paddingTop.toFloat() + distanceBetweenItems
)

val topChild = currentTopChild ?: nextTopChild ?: return
getDistanceBetweenItems(topChild, parent)

parent.width.toFloat() / 2: it's more accurate to expect the list item to reach the centre of the RecyclerView than a side (you know: size, padding, margin, ...) nextTopChild: in case there is a gap between items just at the top of the parent, it finds the next item on the list.