我有一個水平recyclerview(與一個LinearLayoutManager),它的子項目有一個imageview包裹在一個相關佈局中。 recyclerview有三個包含子項的水平行。當滾動回滾視圖時,我使用來自http調用的響應更新數據集,並在http響應回調中調用notifydatasetChanged。如果recyclerview子項的高度和寬度是固定值(例如150dp,84dp),recyclerview的行爲就像預期的那樣,即加載新項目並停留在同一個滾動位置,但如果我將recyclerview的子項的高度和寬度設置爲match_parent,並且wrap_content,recyclerview完全刷新並滾動到第一個位置。這是爲什麼發生? 注意:我已經檢查過其他類似的問題,但都沒有提供可行的解決方案。Recyclerview在調用notifydatasetchanged後滾動到頂部
main_layout
<android.support.v7.widget.RecyclerView
android:id="@+id/grid_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="110dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="80dp"
android:background="@color/transparent"
android:outlineProvider="bounds"
android:elevation="8dp"
android:padding="0dp"
android:scrollbars="none" />
....
recycler_view_child_item
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="0dp">
<ImageView
android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:maxWidth="126dp"
android:maxHeight="225dp"
android:padding="0dp"/>
</RelativeLayout>
刪除相對佈局。由於我將recyclerview的尺寸設置爲與父母的尺寸相匹配,因此回收站視圖的尺寸可能因設備而異。所以我無法爲子視圖設置固定大小。回收站視圖有三行,因此我設置了childview的高度以匹配父級和寬度以包裝內容,以避免回收站視圖中子視圖之間出現空間。 – Neanderthal
此外,我已將setHasFixedSize設置爲true。 – Neanderthal