回收器视图 - 如何在嵌套滚动视图中的某个位置平滑滚动到项目顶部



我实现了回收器视图,当单击回收器视图时,它会转到下一个活动,但是当我回来时,我应该以单击的相同位置返回,就像我需要根据项目的位置滚动到特定位置一样,我有位置但由于嵌套滚动视图而无法滚动。

如何解决此问题?

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

在此回收视图中,我需要根据位置滚动到特定项目,如何制作?我使用过:

            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);
private int mPosition = RecyclerView.NO_POSITION;

m位置是您要滚动的特定位置。将其保存在 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)

最新更新