Collapse appBarLayout on appList focus

We collapse the app bar when the focus is on the app list which only occurs while using a controller, this is required as the app bar will never be collapsed otherwise. It also removes the older code to work around the limitation on `View.FOCUS_DOWN` by collapsing only when the end of the list was reached.
This commit is contained in:
PixelyIon 2022-03-11 16:30:07 +05:30
parent 3e4ec7323b
commit 3ae62c7fcc

View File

@ -202,23 +202,16 @@ class MainActivity : AppCompatActivity() {
} }
} }
override fun onRequestChildFocus(parent : RecyclerView, state : RecyclerView.State, child : View, focused : View?) : Boolean {
binding.appBarLayout.setExpanded(false)
return super.onRequestChildFocus(parent, state, child, focused)
}
override fun onFocusSearchFailed(focused : View, focusDirection : Int, recycler : RecyclerView.Recycler, state : RecyclerView.State) : View? { override fun onFocusSearchFailed(focused : View, focusDirection : Int, recycler : RecyclerView.Recycler, state : RecyclerView.State) : View? {
val nextFocus = super.onFocusSearchFailed(focused, focusDirection, recycler, state) val nextFocus = super.onFocusSearchFailed(focused, focusDirection, recycler, state)
when (focusDirection) { when (focusDirection) {
View.FOCUS_DOWN -> { View.FOCUS_DOWN -> {
findContainingItemView(focused)?.let { focusedChild -> return null
val current = binding.appList.indexOfChild(focusedChild)
val currentSpanIndex = (focusedChild.layoutParams as LayoutParams).spanIndex
for (i in current + 1 until binding.appList.size) {
val candidate = getChildAt(i)!!
// Return candidate when span index matches
if (currentSpanIndex == (candidate.layoutParams as LayoutParams).spanIndex) return candidate
}
nextFocus?.let { if ((it.layoutParams as LayoutParams).spanIndex == currentSpanIndex) return nextFocus }
binding.appBarLayout.setExpanded(false) // End of list, hide app bar, so bottom row is fully visible
binding.appList.smoothScrollToPosition(adapter.itemCount)
}
} }
View.FOCUS_UP -> { View.FOCUS_UP -> {