chrisbanes / insetter

Insetter is a library to help apps handle WindowInsets more easily
https://chrisbanes.github.io/insetter
Apache License 2.0
1.13k stars 42 forks source link

Insets should be applied every time the view is attached to a window #90

Closed alexvanyo closed 3 years ago

alexvanyo commented 3 years ago

Currently, requestApplyInsets is called once when the view is attached to a window, via doOnAttach (either immediately or the next time the view is attached):

https://github.com/chrisbanes/insetter/blob/9b1eed571aa8c5c8112f1aa6c3cb5d57a1ab4dd8/library/src/main/java/dev/chrisbanes/insetter/Insetter.kt#L394-L396

This works well for most cases, but there are corner cases where this isn't enough.

For instance, suppose a view within a ViewHolder within a RecyclerView is has left/right insets applied to it from the systemBars insets. The consider the following sequence of events:

Since the new insets were dispatched while the ViewHolders were detached, these ViewHolders will now be incorrectly displaying the old insets, and since they were already attached once, requestApplyInsets won't be called again.

The following video shows an example of that case:

https://user-images.githubusercontent.com/4217560/105615109-abde9980-5d93-11eb-98e1-6aa5dc332def.mp4

My proposed fix is to replace doOnAttach with doOnEveryAttach, which will run the action immediately if the view is already attached, and then will run the action again upon every subsequent attach.