协调器布局内的回收者视图未以编程方式滚动

问题描述 投票:4回答:1

我有以下XML布局。我在协调器布局内使用回收站。虽然当我手动滚动时,它可以按预期工作。但是当我打电话时mRecyclerView.getLayoutManager().scrollToPosition(selectedPosition);mRecyclerView.scrollToPosition(selectedPosition);以编程方式滚动浏览回收站视图,而不滚动。下面是我的XML代码

    <android.support.design.widget.AppBarLayout
        android:id="@+id/tab_appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/backgroundBlue">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|enterAlways">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">


                <android.support.v7.widget.CardView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/standard_layout_margin"
                    android:layout_marginRight="@dimen/standard_layout_margin"
                    app:cardElevation="@dimen/small_layout_margin"
                    app:cardUseCompatPadding="true">

                    <include
                        android:id="@+id/details"
                        layout="@layout/detail_layout" />

                </android.support.v7.widget.CardView>


                <android.support.v7.widget.CardView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/standard_layout_margin"
                    android:layout_marginRight="@dimen/standard_layout_margin"
                    app:cardElevation="@dimen/small_layout_margin"
                    app:cardUseCompatPadding="true">

                    <include layout="@layout/store_layout" />

                </android.support.v7.widget.CardView>

            </LinearLayout>

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

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


    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/collapsing_toolbar"
        android:layout_marginLeft="@dimen/standard_layout_margin"
        android:layout_marginRight="@dimen/standard_layout_margin"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />


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

在特定情况下,我希望回收者视图中的特定项目位于屏幕顶部。我有下面的Java代码,

if (selectedPosition != 0) {
                    AppBarLayout appBarLayout = (AppBarLayout) mView.findViewById(R.id.tab_appbar);
                    CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
                    AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();
                    if(behavior!=null) {
                        behavior.onNestedPreScroll(mCoordinatorLayout, appBarLayout, mRecyclerView, 0, (int) mRecyclerView.getY(), new int[]{0, 0});
                        mRecyclerView.getLayoutManager().scrollToPosition(selectedPosition);
                        mRecyclerView.scrollToPosition(selectedPosition);
                    }
     }

任何想法,如何将回收站视图项拉到屏幕顶部?

android scroll android-recyclerview android-coordinatorlayout
1个回答
0
投票

设置适配器后,在下面的行中写

mRecyclerView.startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL, ViewCompat.TYPE_NON_TOUCH)
mRecyclerView.smoothScrollBy(0,500)
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.