Created
April 8, 2020 00:07
-
-
Save miguelhincapie/04d97483e38b213f196f1876d7756699 to your computer and use it in GitHub Desktop.
Key event delegate to navigate through the carousel using TalkBack
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 CarouselKeyEventDelegate : BaseKeyEventDelegate() { | |
init { | |
keyEventActionMap.let { | |
it.put( | |
createKey( | |
R.id.carousel_container, | |
KEYCODE_DPAD_DOWN | |
), | |
this::consumeDownKeyOnCarouselContainer | |
) | |
it.put( | |
createKey( | |
R.id.carousel_container, | |
KEYCODE_DPAD_UP | |
), | |
this::consumeUpKeyOnCarouselContainer | |
) | |
it.put( | |
createKey( | |
R.id.carousel_container, | |
KEYCODE_ENTER | |
), | |
this::consumeEnterKeyOnCarouselContainer | |
) | |
it.put( | |
createKey( | |
R.id.carousel_element_item, | |
KEYCODE_DPAD_DOWN | |
), | |
this::consumeDownKeyOnCarousel | |
) | |
it.put( | |
createKey( | |
R.id.carousel_element_item, | |
KEYCODE_DPAD_UP | |
), | |
this::consumeUpKeyOnCarousel | |
) | |
it.put( | |
createKey( | |
R.id.carousel_element_item, | |
KEYCODE_DPAD_LEFT | |
), | |
this::consumeLeftKeyOnCarousel | |
) | |
it.put( | |
createKey( | |
R.id.carousel_element_item, | |
KEYCODE_DPAD_RIGHT | |
), | |
this::consumeRightKeyOnCarousel | |
) | |
} | |
} | |
private fun consumeDownKeyOnCarouselContainer(currentFocus: View): Boolean { | |
currentFocus.nextFocusDownId = R.id.button_turn_on | |
return false | |
} | |
private fun consumeUpKeyOnCarouselContainer(currentFocus: View): Boolean { | |
currentFocus.rootView.findViewById<RecyclerView>(R.id.gridRV)?.sendFocusToLastElement() | |
return true | |
} | |
private fun consumeEnterKeyOnCarouselContainer(currentFocus: View): Boolean { | |
currentFocus.findViewById<RecyclerView>(R.id.carouselRV)?.sendFocusToFirstElement() | |
return false | |
} | |
private fun consumeDownKeyOnCarousel(currentFocus: View): Boolean = with(currentFocus) { | |
getNextElementPosition().let { nextElementPosition -> | |
if (isPositionInbound(nextElementPosition)) { | |
sendFocusToListItem(nextElementPosition) | |
} else { | |
rootView.findViewById<View>(R.id.carousel_container)?.sendAccessibilityFocus() | |
} | |
} | |
return true | |
} | |
private fun consumeUpKeyOnCarousel(currentFocus: View): Boolean = with(currentFocus) { | |
getPreviousElementPosition().let { previousElementPosition -> | |
if (isPositionInbound(previousElementPosition)) { | |
sendFocusToListItem(previousElementPosition) | |
} else { | |
rootView.findViewById<View>(R.id.carousel_container)?.sendAccessibilityFocus() | |
} | |
} | |
return true | |
} | |
private fun consumeLeftKeyOnCarousel(currentFocus: View): Boolean { | |
currentFocus.rootView.findViewById<View>(R.id.carousel_container)?.sendAccessibilityFocus() | |
return true | |
} | |
private fun consumeRightKeyOnCarousel(currentFocus: View): Boolean { | |
currentFocus.rootView.findViewById<View>(R.id.carousel_container)?.sendAccessibilityFocus() | |
return true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment