Permalink
Cannot retrieve contributors at this time
InboxRecyclerView/inboxrecyclerview/src/main/java/me/saket/inboxrecyclerview/animation/SplitExpandAnimator.kt
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
70 lines (60 sloc)
2.3 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.saket.inboxrecyclerview.animation | |
import android.view.View | |
import me.saket.inboxrecyclerview.InboxRecyclerView | |
import me.saket.inboxrecyclerview.page.ExpandablePageLayout | |
/** | |
* [https://www.youtube.com/watch?v=WQGtweo-2dc] | |
*/ | |
internal class SplitExpandAnimator : ItemExpandAnimator() { | |
override fun onPageMove( | |
recyclerView: InboxRecyclerView, | |
page: ExpandablePageLayout, | |
anchorViewOverlay: View? | |
) { | |
if (!page.isMoving) { | |
// Reset everything. This is also useful when the content size | |
// changes, say as a result of the soft-keyboard getting dismissed. | |
resetAnimation(recyclerView, anchorViewOverlay) | |
return | |
} | |
val anchorIndex = recyclerView.expandedItemLoc.viewIndex | |
val anchorViewLocation = recyclerView.expandedItemLoc.locationOnScreen | |
val pageTop = page.locationOnScreen().top | |
val pageBottom = pageTop + page.clippedDimens.height() | |
// Move the RecyclerView rows with the page. | |
if (anchorViewOverlay != null) { | |
val distanceExpandedTowardsTop = pageTop - anchorViewLocation.top | |
val distanceExpandedTowardsBottom = pageBottom - anchorViewLocation.bottom | |
recyclerView.moveListItems(anchorIndex, distanceExpandedTowardsTop, distanceExpandedTowardsBottom) | |
} else { | |
// Anchor View can be null when the page was expanded from | |
// an arbitrary location. See InboxRecyclerView#expandFromTop(). | |
recyclerView.moveListItems(anchorIndex, 0, pageBottom - pageTop) | |
} | |
// Fade in the anchor row with the expanding/collapsing page. | |
anchorViewOverlay?.alpha = 1f - page.expandRatio(recyclerView) | |
} | |
private fun InboxRecyclerView.moveListItems( | |
anchorIndex: Int, | |
distanceExpandedTowardsTop: Int, | |
distanceExpandedTowardsBottom: Int | |
) { | |
for (childIndex in 0 until childCount) { | |
getChildAt(childIndex).translationY = when { | |
childIndex <= anchorIndex -> distanceExpandedTowardsTop.toFloat() | |
else -> distanceExpandedTowardsBottom.toFloat() | |
} | |
} | |
} | |
override fun resetAnimation( | |
recyclerView: InboxRecyclerView, | |
anchorViewOverlay: View? | |
) { | |
for (childIndex in 0 until recyclerView.childCount) { | |
val childView = recyclerView.getChildAt(childIndex) | |
childView.translationY = 0F | |
childView.alpha = 1F | |
} | |
anchorViewOverlay?.alpha = 0f | |
} | |
} |