Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recycleview scroll to a position not working inside Nestedscrollview

Tags:

android

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);
like image 622
Darshana Avatar asked May 22 '18 09:05

Darshana


3 Answers

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

like image 145
Darshana Avatar answered Oct 29 '22 05:10

Darshana


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)
like image 33
Gabor Avatar answered Oct 29 '22 05:10

Gabor


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

like image 34
Dino Sunny Avatar answered Oct 29 '22 06:10

Dino Sunny