Last active
March 2, 2022 19:22
-
-
Save discord-gists/e2912805c77ba320785b56e64188398f to your computer and use it in GitHub Desktop.
a simplified code example of how Discord's Android app selectively allows child views of OverlappingPanelsLayout to handle their own horizontal scrolls
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
class PanelsChildGestureRegionObserver : View.OnLayoutChangeListener { | |
... | |
override fun onLayoutChange( | |
view: View?, | |
left: Int, | |
top: Int, | |
right: Int, | |
bottom: Int, | |
oldLeft: Int, | |
oldTop: Int, | |
oldRight: Int, | |
oldBottom: Int | |
) { | |
if (view != null) { | |
val coordinates = intArrayOf(0, 0) | |
view.getLocationOnScreen(coordinates) | |
val x = coordinates[0] | |
val y = coordinates[1] | |
val absoluteLeft = x + left | |
val absoluteTop = y + top | |
val absoluteRight = x + right | |
val absoluteBottom = y + bottom | |
viewIdToGestureRegionMap[view.id] = Rect( | |
absoluteLeft, | |
absoluteTop, | |
absoluteRight, | |
absoluteBottom | |
) | |
publishGestureRegionsUpdate() | |
} | |
} | |
private fun publishGestureRegionsUpdate() { | |
val gestureRegions = viewIdToGestureRegionMap.values.toList() | |
gestureRegionsListeners.forEach { gestureRegionsListener -> | |
gestureRegionsListener.onGestureRegionsUpdate(gestureRegions) | |
} | |
} | |
} | |
class ChatInputFragment: Fragment() { | |
private val container: View by bindView(R.id.chat_input_container) | |
override fun onViewCreated( | |
view: View, | |
savedInstanceState: Bundle? | |
) { | |
super.onViewCreated(view, savedInstanceState) | |
// Provider.get() returns a lazily instantiated | |
// activity-scoped singleton. | |
container.addOnLayoutChangeListener( | |
PanelsChildGestureRegionObserver.Provider.get() | |
) | |
} | |
} | |
class PanelsFragment: Fragment(), | |
PanelsChildGestureRegionObserver.GestureRegionsListener { | |
private val panels: OverlappingPanelsLayout by bindView(R.id.overlapping_panels) | |
... | |
override fun onGestureRegionsUpdate(gestureRegions: List<Rect>) { | |
panels.setChildGestureRegions(gestureRegions) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment