Skip to content

Instantly share code, notes, and snippets.

@nikartx
Created October 30, 2020 13:47
Show Gist options
  • Save nikartx/45f9d5e86f2181f01a0cefc5b37ed685 to your computer and use it in GitHub Desktop.
Save nikartx/45f9d5e86f2181f01a0cefc5b37ed685 to your computer and use it in GitHub Desktop.
Scroll to position when RecyclerView place into NestedScrollView. Using LinearLayoutManagersmoothScrollTo and rvBlocks.scrollToPosition will not work when RecyclerView placed into NestedScrollView but this solution worked perfectly.
<androidx.core.widget.NestedScrollView
android:id="@+id/nswContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvBlocks"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"/>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
// Solution
fun navigateToBlock(position: Int) {
val scrollY = rvBlocks.getChildAt(position).y
nswContent.smoothScrollTo(0, scrollY.toInt())
}
@anilkt87
Copy link

HI i have posted question related scrollview can you please help me in this question https://stackoverflow.com/questions/70527432/how-to-scroll-vertically-for-specific-view-using-nested-scrollview-in-android

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment