Skip to content

Instantly share code, notes, and snippets.

@chris-horner
Created December 21, 2017 12:38
Show Gist options
  • Save chris-horner/747dba2cf30194b58881f182c1373b87 to your computer and use it in GitHub Desktop.
Save chris-horner/747dba2cf30194b58881f182c1373b87 to your computer and use it in GitHub Desktop.
Extension function forcing a shared element to run in a ViewGroup overlay.
fun Transition.sharedElementsInOverlay(container: ViewGroup, start: View, end: View): Transition =
addListener(object : Transition.TransitionListener {
val endParent = end.parent as ViewGroup
val endOriginalPosition = endParent.indexOfChild(end)
override fun onTransitionStart(transition: Transition) {
start.visibility = View.GONE
container.overlay.add(start)
container.overlay.add(end)
}
override fun onTransitionEnd(transition: Transition) {
container.overlay.remove(start)
container.overlay.remove(end)
if (endParent.isAttachedToWindow) {
if (endOriginalPosition <= endParent.childCount) {
endParent.addView(end, endOriginalPosition)
} else {
endParent.addView(end)
}
}
}
override fun onTransitionResume(transition: Transition) {}
override fun onTransitionPause(transition: Transition) {}
override fun onTransitionCancel(transition: Transition) {}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment