【问题标题】:RecyclerView - How to smooth scroll to top of item on a certain position inside Nested scrollview?RecyclerView - 如何平滑滚动到嵌套滚动视图内某个位置的项目顶部?
【发布时间】:2018-07-13 04:25:15
【问题描述】:

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

我该如何解决这个问题?

<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 中,我需要根据位置滚动到特定项目,我该怎么做?我用过:

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

【问题讨论】:

    标签: android android-recyclerview android-nestedscrollview nestedscrollview


    【解决方案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);
    }
    

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

    【讨论】:

    • 根本不工作我需要为嵌套滚动视图内的回收视图进行整理;(
    猜你喜欢
    • 1970-01-01
    • 2020-05-17
    • 2019-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-27
    • 1970-01-01
    相关资源
    最近更新 更多