basshelal / BoardView

🚧Work In Progress🚧 Android library for easy to use Kanban Boards
3 stars 0 forks source link

Use a custom ItemAnimator that is drag swap aware #2

Closed basshelal closed 4 years ago

basshelal commented 4 years ago

Swapping ViewHolders across lists using the default ItemAnimator makes the alpha of the newly added VH be 1.0, thus making it visible for a few frames before we can force it to be transparent again.

We need to solve this using a custom ItemAnimator that will be aware of when swaps are happening in order to efficiently and correctly animate things as expected.

The main goal is to trick the user into thinking they are dragging the ViewHolder. They are actually dragging a Shadow and the ViewHolder has become transparent but must remain that way until the drag has ended no matter where it is dragged.

A custom ItemAnimator that can be notified of swaps and behave correctly would be the ideal solution to this.

basshelal commented 4 years ago

This is now done! The new BoardListItemAnimator can correctly react to dragging when informed about critical drag related events. The two methods added to the API for this are prepareForDrop() and draggingItemInserted(...).

prepareForDrop() is done to account for LinearLayoutManager's weirdnesses with the 0th child and any swaps made with it, see issue #3. Essentially if LinearLayoutManager.prepareForDrop(...) is called, then a subsequent BoardListItemAnimator.prepareForDrop() call must be done to account for that first call's weird behavior.

draggingItemInserted(...) allows for the correct appearance when a swap is made across lists. Before, BoardList would animate it as if it was a regular insertion because the default ItemAnimator does this and doesn't know any better. BoardListItemAnimator can now be told that the next add will be the dragging item and thus must behave correctly to make it appear as though the user is dragging the ViewHolder.

These have been implemented in ways as to not cause other issues, any animation that is meant to be ignored or removed is actually just swapped for a "useless" animation, meaning an animation that does nothing and terminates instantly but still functions as the typical animations in the ItemAnimator, calling the correct methods when appropriate. Basically, the only thing that has changed is that the animations are not visible.