bgogetap / StickyHeaders

Easily add Sticky Headers to your RecyclerView
Apache License 2.0
521 stars 88 forks source link

Wrong behavior after list got updated #65

Closed mobilekosmos closed 6 years ago

mobilekosmos commented 6 years ago

I detected now that when the items are removed from the list there is something wrong, if 1 item is removed or added then the item after/before the second header is detected as header, if 2 then the second item and so on. Is there a way to update the layoutManager? I suppose this should work automatically?

bgogetap commented 6 years ago

I'd need to see the code that's handling your changed dataset in the adapter.

I'm not able to reproduce this in the sample app.

mobilekosmos commented 6 years ago

I will try out the demo and try to find out the problem, but I am not doing something strange in my adapter, I use a normal List and call clear when updating it and add the new items again based on a cursor.

mobilekosmos commented 6 years ago

Ok, it was easily reproducible, use following in MainActivity and findViewById(R.id.visibility_button).setOnClickListener(v -> adapter.removeSomething());

and following in the adapter:

public void removeSomething() { data.remove(6); }

Now scroll down and up and you should see what I described.

bgogetap commented 6 years ago

You can't just remove data from the adapter's data source without calling notifyDataSetChanged or using DiffUtil and dispatching updates to the adapter.

This is true with or without this library. Your RecyclerView would not work as expected if you modify the dataset without letting the adapter know.

You can look at the adapter in the sample app to see an example of how to handle data set changes.

mobilekosmos commented 6 years ago

Hm, strrangely in my code I do am calling notifyDataSetChanged but it happens anyways, will dig into it.

mobilekosmos commented 6 years ago

Ok, problem was that I was using setAdapter(getAdapter()) and passing as parameter to new TopSnappedStickyLayoutManager also getAdapter, where getAdapter=return new Adapter... so I had 2 different instances.