Recycleview scroll to a position not working inside Nestedscrollview
This is how I resolve this issue
first get recycle view position that need to scroll using below method
final float y = recyclerView.getChildAt(selectedItem.getPos()).getY();
Then scroll nested scroll view to that positionnestedScrollingView.post(new Runnable() {
@Override
public void run() {
nestedScrollingView.fling(0);
nestedScrollingView.smoothScrollTo(0, (int) y);
}
});
Don't forget to add android:fillViewport="true"
on nestedscrollview
The thing is that you need to scroll the NestedScrollView, not the RecyclerView. E.g:
final float y = recyclerView.getChildAt(selectedItem.getPos()).getY();
scrollView.smoothScrollTo(0, y)
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBar"
android:layout_width="match_parent"
android:layout_height="300dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsingToolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginStart="48dp"
app:expandedTitleMarginEnd="64dp"
app:title="Scroll">
<ImageView
android:id="@+id/toolbarImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:fitsSystemWindows="true"
android:src="@mipmap/ic_launcher"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:id="@+id/recycler_view"
android:scrollbars="vertical"
android:nestedScrollingEnabled="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Remove the NestedScrollView and add
app:layout_behavior="@string/appbar_scrolling_view_behavior"
to your Recyclerview.Then ScrollToPosition works fine.