RecyclerView - 如何在嵌套滚动视图内的某个位置平滑滚动到项目顶部?

时间:2018-02-02 16:06:52

标签: android android-recyclerview recycler-adapter android-nestedscrollview nestedscrollview

我实施了Recycler视图,当点击recyclerview时它会转到下一个活动但是当我回来时我应该回到我点击的相同位置,就像我需要根据项目的位置滚动到特定位置,我有位置,但由于嵌套的scrollview,我无法滚动。

如何解决此问题?

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="140dp"
        android:background="@android:color/transparent"

        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/theme_color"
            android:fitsSystemWindows="true"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
            app:title="@string/app_name"

            app:titleEnabled="false">

            <LinearLayout
                android:id="@+id/ll_report_container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="?attr/actionBarSize"
                android:orientation="vertical">
......

            </LinearLayout>

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/theme_color"
                app:contentInsetLeft="0dp"
                app:contentInsetStart="0dp"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    >
.....
                </RelativeLayout>
            </android.support.v7.widget.Toolbar>
        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/palegray"
        android:scrollbars="none"
        android:id="@+id/nested_scroll"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/palegray"
            android:orientation="vertical">

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@color/card_header" />

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

        </LinearLayout>


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

</android.support.design.widget.CoordinatorLayout>

在这个rec​​yclerview中,我需要根据位置滚动到特定项目我该如何制作?我用过:

            recyclerview.smoothScrollToPosition(position);

但是没有用,我也用过这个,但这个也适合我,

RecyclerView.SmoothScroller smoothScroller = new LinearSmoothScroller(RoleActivity.this) {
                            @Override protected int getVerticalSnapPreference() {
                                return LinearSmoothScroller.SNAP_TO_START;
                            }
                        };
                        smoothScroller.setTargetPosition(i);
                        mLayoutManager.startSmoothScroll(smoothScroller);

1 个答案:

答案 0 :(得分:-1)

private int mPosition = RecyclerView.NO_POSITION;

mPosition是您要滚动的特定位置。将其保存在onSaveInstanceState

@Override
public void onSaveInstanceState(Bundle outState) {
   outState.putInt(SELECTED_KEY, mPosition);
}
创建

if(savedInstanceState != null && savedInstanceState.containsKey(SELECTED_KEY))
{
    mPosition = savedInstanceState.getInt(SELECTED_KEY);
}

一旦要传递给reccylerview的数据准备就绪,即在adapter.notifyDataSetChanged()之后调用mRecylerView.smoothScrollToPosition(mPosition);