当 recyclerview 放置在 scrollview/nestedScrollView 中时,scrollToPosition 方法不起作用

Posted

技术标签:

【中文标题】当 recyclerview 放置在 scrollview/nestedScrollView 中时,scrollToPosition 方法不起作用【英文标题】:scrollToPosition method not working when recyclerview is placed inside scrollview/nestedScrollView 【发布时间】:2018-04-08 17:03:01 【问题描述】:

在我的布局中,ScrollView 是父级,它有Imageview(160dp 高度)和一个Recyclerview 作为子级。 场景:有图片和10个String项列表,可见列表项为0,1,2,3,4。所以每当有一个新的listitem 时,我都会更新列表并调用scrollToPosition 方法(itemcount -1),但没有任何反应。我已经有setNestedScrollEnabled(false)

我尝试了scrollView.fullScroll(DOWN) 方法scrollView.scrollTo(0, adapter.itemcount),使用它滚动到旧列表的最后一项,但没有完全滚动以显示添加的新项目。这是我的布局

<android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_refresh_layout"
        android:layout_
        android:layout_
        android:background="@null">

        <android.support.v4.widget.NestedScrollView
            android:id="@+id/scrollView"
            android:layout_
            android:layout_>

            <LinearLayout
                android:layout_
                android:layout_
                android:orientation="vertical">

                <ImageView
                    android:layout_
                    android:layout_ />



                <View
                    android:id="@+id/grey_line"
                    android:layout_
                    android:layout_
                    android:background="@color/light_grey"
                    android:visibility="gone" />


                <RelativeLayout
                    android:layout_
                    android:layout_>

                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/comments_list"
                        android:focusable="true"
                        android:focusableInTouchMode="true"
                        android:layout_
                        android:layout_
                        android:scrollbars="vertical" />


                </RelativeLayout>

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

请帮忙,谢谢!

【问题讨论】:

你得到这个问题的答案了吗??我也面临同样的问题......如果你明白了,请告诉我。 【参考方案1】:

可能你正在使用

android:descendantFocusability="blocksDescendants" 

在您的布局中。

如果没有,请尝试使用

android:focusable="true"
android:focudeableInTouchMode="true"

在你的Recyclerview里面

【讨论】:

没用.. 我没有使用blocksDescendants 并尝试在 recyclerview 中使用其他两个选项。没有运气

以上是关于当 recyclerview 放置在 scrollview/nestedScrollView 中时,scrollToPosition 方法不起作用的主要内容,如果未能解决你的问题,请参考以下文章

将水平 RecyclerView 作为项目放置在垂直 RecyclerView 中时的自动滚动问题

如何将 textView 放置在 recyclerView 上方?

Recyclerview 在滚动期间更改项目

当另一个滚动时如何隐藏recyclerview

如何在 RecyclerView 中显示“重”图像?

当我将视频播放到 Recyclerview 内的 VideoView 时,当我滚动时,它会在 x 轴上旋转