Skip to content

Instantly share code, notes, and snippets.

@chenzhang2006
Last active August 1, 2022 20:01
Show Gist options
  • Save chenzhang2006/5d693c161db0b82972d06bbfc7d34a5b to your computer and use it in GitHub Desktop.
Save chenzhang2006/5d693c161db0b82972d06bbfc7d34a5b to your computer and use it in GitHub Desktop.
RecyclerView State Sync Skeleton
// Horizontle RecyclerView
val horizontalRecyclerView: RecyclerView = findViewById(R.id.horizontal_recycler_view)
val horizontalLayoutManager: LinearLayoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
horizontalRecyclerView.layoutManager = horizontalLayoutManager
// Vertical RecyclerView
val verticalRecyclerView: RecyclerView = findViewById(R.id.vertical_recycler_view)
val verticalLayoutManager: LinearLayoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
verticalRecyclerView.layoutManager = verticalLayoutManager
// ... When vertical list starts to render
// Transfer scroll position from horizontal list to vertical one
val horizontalScrollPosition = horizontalLayoutManager.findFirstVisibleItemPosition()
verticalRecyclerView.scrollToPosition(horizontalScrollPosition)
// ... When horizontal list starts to render
// Transfer scroll postion from vertical list to horizontal one
val verticalScrollPosition = verticalLayoutManager.findFirstVisibleItemPosition()
horizontalRecyclerView.scrollToPosition(verticalScrollPosition)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment