簡體   English   中英

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

[英]scrollToPosition method not working when recyclerview is placed inside scrollview/nestedScrollView

在我的布局中, ScrollView是父級,它有Imageview (160dp 高度)和一個Recyclerview作為子級。 場景:有圖像和 10 個字符串項列表,可見列表項分別為 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_width="match_parent"
        android:layout_height="match_parent"
        android:background="@null">

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

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

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="140dp" />



                <View
                    android:id="@+id/grey_line"
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/light_grey"
                    android:visibility="gone" />


                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

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


                </RelativeLayout>

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

請幫忙,謝謝!

可能你正在使用

android:descendantFocusability="blocksDescendants" 

在您的布局內。

如果沒有,請嘗試使用

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

在您的Recyclerview

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM