Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
a simplified code example of how Discord's Android app maintains panel states through device rotations while applying view updates before onLayout
class OverlappingPanelsLayout: FrameLayout {
private lateinit var startPanel: View
private lateinit var centerPanel: View
private lateinit var endPanel: View
private var pendingUpdate: (() -> Unit)? = null
...
private fun openStartPanel() {
// This can get called before onLayout() where centerPanel
// gets initialized. If that happens, save the pendingUpdate
// for after centerPanel gets initialized.
if (!::centerPanel.isInitialized) {
pendingUpdate = { openStartPanel(isFling) }
return
}
updateCenterPanelX(x = startPanelOpenedCenterPanelX)
}
override fun onLayout(
changed: Boolean,
left: Int,
top: Int,
right: Int,
bottom: Int
) {
super.onLayout(changed, left, top, right, bottom)
// OverlappingPanelsLayout expects exactly three child
// views where each child view is a panel. If there are
// not exactly three child views, OverlappingPanelsLayout will
// throw an exception.
if (!::centerPanel.isInitialized) {
startPanel = getChildAt(0)
centerPanel = getChildAt(1)
endPanel = getChildAt(2)
pendingUpdate?.invoke()
pendingUpdate = null
...
}
}
}
@EnderDev

This comment has been minimized.

Copy link

@EnderDev EnderDev commented Jul 20, 2020

epic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.