簡體   English   中英

Recyclerview不保留我的滾動位置

[英]Recyclerview don't retain my scrolling position

我正在使用StaggeredGridLayout構建應用程序。 但是一旦我打開了一個細節片段(由導航庫處理),然后回到主片段,我的滾動位置就會丟失。

我已經嘗試保存recyclerview的實例,以便在我們到達onResume后將其重新應用,但是此技術破壞了我的共享元素轉換。

recyclerview的XML

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/linear_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recentNotesList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingStart="20dp"
    android:paddingEnd="20dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>

<ProgressBar
    android:id="@+id/loading_ui"
    android:layout_width="75dp"
    android:layout_height="75dp"
    app:bindIsGone="@{thisVm.hasNotes}"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<androidx.appcompat.widget.AppCompatImageView
    android:id="@+id/nonethinking"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:paddingStart="20dp"
    android:paddingEnd="20dp"
    android:scaleType="fitCenter"
    android:src="@drawable/thinking"
    android:visibility="gone"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

HomeMainFragment

val lm = StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL)
lm.gapStrategy = StaggeredGridLayoutManager.GAP_HANDLING_NONE
binding.recentNotesList.layoutManager = lm

預期結果:打開項目時->共享元素過渡(這項工作)當關閉打開的項目時->共享元素過渡

實際結果:僅當用戶尚未滾動recyclerview時可見圖像時,關閉打開的項目-> sharedelement過渡工作。

我認為這與滾動位置保留過程有關。 但是我無法使這個東西正常工作。

嗨,當您將adapater設置為recyclerview時,請嘗試此操作。

val lastFirstVisiblePositon= recyclerview.layoutmanager.findFirstCompletelyVisibleItemPosition();
recyclerview.layoutmanager.scrollToPositionWithOffset(lastFirstVisiblePosition, 0);
listdapter.notifyDataSetChanged();
hRecyclerView_OpenOrderListing.setAdapter(listdapter);

我通過在我的recyclerview項目中使用ConstraintLayout修復了它。 只需將縱橫比設置為默認值1:1,然后使用onBindViewHolder中應用程序的縱橫比以編程方式更新此縱橫比。

像這樣:

商品RV的XML

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/parentContsraint"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/show_image_note"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:scaleType="centerCrop"
        android:contentDescription="@string/image_of_your_dedicated_note"
        android:transitionName="@{String.valueOf(note.id)}"
        app:imageFromFile="@{note.image}"
        app:srcCompat="@drawable/pool"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

並在您的適配器中

with(set) {
    val posterRatio = String.format("%d:%d", resource.width, resource.height)
    clone(binding.parentContsraint)
    setDimensionRatio(binding.showImageNote.id, posterRatio)
    applyTo(binding.parentContsraint)
}

暫無
暫無

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

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