android - Recyclerview 不保留我的滚动位置

标签 android kotlin android-recyclerview

我正在构建一个带有 StaggeredGridLayout 的应用程序。 但是一旦我打开一个细节 fragment (由导航库处理)然后返回到主要 fragment ,我的滚动位置就会丢失。

我已经尝试保存 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

预期结果: 打开项目时 -> sharedelement 转换(这个有效) 关闭打开的item时-> sharedelement transition

实际结果: 关闭打开的项目时 -> sharedelement transition 只有在用户尚未滚动 recyclerview 时图像可见时才有效。

我觉得跟滚动位置保持过程有关系。但我无法让这个东西工作。

最佳答案

我通过在我的 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)
}

关于android - Recyclerview 不保留我的滚动位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54640305/

相关文章:

lambda - Kotlin:为什么函数不能存储在变量中?

gradle - TreeWalker 检查无法与 Kotlin/Gradle 配合使用?

android - 如何通过操作栏中的后退按钮转到上一个 fragment ? ( Kotlin )

android - 有没有办法在多个嵌套的 RecyclerView 之间共享同一个 LayoutManager

android - viewHolder 类中的 notifyDataSetChanged

java - 是否可以有多个android :id/list?

android - GLSL ES 局部变量崩溃?

android - 如何在协调器布局中添加拉动刷新

android - 带有自定义列表的进度条

android - 如果我们使用 WLResourceRequest 直接连接到休息服务器,IBM MobileFirst 是否捕获分析