Daniel Souza Bertoldi
01/24/2024, 8:33 PMscrollToPosition of RecyclerView from an AndroidView? More info in the 🧵Daniel Souza Bertoldi
01/24/2024, 8:33 PMColumn that contains some other composables + an AndroidView in which I'm inflating a RecyclerView. One of the Composables is clickable, and I should scroll the RecyclerView to a certain position. Not only the Recycler should scroll, but also entire screen as well. Here's an overview of my Composables:
val scrollState = rememberScrollState()
fun BaseComposable(){
Column(modifier = Modifier.verticalScroll(scrollState)) {
ClickableComposable()
AndroidView()
}
}
What I've already tried so far:
1. I tried to create a reference to my RecyclerView by instantiating it and then passing it to the factory lambda of the AndroidView. Also, when I click one of the Composables I call:
private fun MyComposable(
modifier = Modifier.clickable { recyclerViewRef.scrollToPosition(5) }
)
I already guaranteed that my recyclerViewRef actually references my RecyclerView, however, the scroll doesn't happen. I tried to add some logs to verify if the OnScrollListener is being called. It is, except that it is only being called when I inflate my RecyclerView.
2. By reading the Scroll doc, I changed the Column modifier from verticalScroll to a nestedScroll passing a NestedScrollConnection. This kinda worked. The only caveat is that the only part that actually scrolls now is the Recycler itself, not the entire screen. But here's the catch: calling scrollToPosition worked and both the RecyclerView OnScrollListener logs + the nested scroll connection logs that I've added are actually being printed.
To sum it up, how can make my screen scroll entirely and also be able to call scrollToPosition of my RecyclerView to scroll not only itself but also the screen?Daniel Souza Bertoldi
02/05/2024, 6:34 PMRecyclerView inside a Scaffold with a LargeTopAppBar with a scroll behavior of type ExitUntilCollapsed works, the top bar is successfully expanding/collapsing via NestedScrollConnection and if I force a navigation icon that calls the scrollToPosition it also scrolls the RecyclerView!
However, that's not the case if I put my RecyclerView inside another Column 😕 like:
Scaffold(
topBar = { LargeTopAppBar() },
scrollBehavior = TopAppBarDefaults.exitUntilCollapsedScrollBehavior()
) { innerPadding ->
// the entire Column should scroll
Column(modifier = Modifier.padding(innerPadding)) {
OtherComposable()
AndroidView(factory = { recyclerView })
}
}
Does anyone knows how to achieve this?