Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
518 views
in Technique[技术] by (71.8m points)

android - Recycleview scroll to a position not working inside Nestedscrollview

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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...