6
votes

I have implemented a recycleview inside a nested scroll view. But recycle view scroll to position methods are not working.

Below is my sample code

  <?xml version="1.0" encoding="utf-8"?>
  <android.support.v4.widget.NestedScrollView 
    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="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

 <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:orientation="vertical">


  <android.support.v7.widget.RecyclerView
      android:id="@+id/list_view"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

 </LinearLayout>

</android.support.v4.widget.NestedScrollView>

below is the method for scrolling

RecyclerView.SmoothScroller smoothScroller = new LinearSmoothScroller(this) {
                @Override
                protected int getVerticalSnapPreference() {
                    return LinearSmoothScroller.SNAP_TO_START;
                }
            };
            smoothScroller.setTargetPosition(pos);
            recyclerView.getLayoutManager().startSmoothScroll(smoothScroller);
5
but why you have used RecyclerView inside NestedScrollView?Bhavesh Rangani
because I have some other views outside recycleviewDarshana
@Darshana test try ViewCompat.setNestedScrollingEnabled(recyclerView,false); and than stackoverflow.com/a/45190337/7666442AskNilesh
@NileshRathod not working for meDarshana
@Darshana how to resolve this issue ? if you getting a solution post your answer Thank You!Hardik Vasani

5 Answers

9
votes

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 position
nestedScrollingView.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

6
votes

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)
1
votes
<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.

0
votes

Try this,

new Handler().postDelayed(new Runnable() {
@Override
public void run() {
    recyclerview.scrollToPosition(position);
}
}, 200);
0
votes

Use this:

yourRecyclerView.setNestedScrollingEnabled(false);

It may solve your problem.