【问题标题】:RecyclerView with reverseLayout "jumps" upon notifyDataSetChanged带有 reverseLayout 的 RecyclerView 在 notifyDataSetChanged 上“跳跃”
【发布时间】:2017-12-05 20:41:00
【问题描述】:

我这样设置我的 recyclerView 的 layoutManager:

recyclerView.layoutManager = LinearLayoutManager(context, HORIZONTAL, true)

最后一个参数将reverseLayout 设置为true,这意味着它将从右到左加载。我需要这个,因为我实现了一种日历,其中最后一个日期是当前日期,但您可以无限滚动到过去。

问题是当我在我的适配器中调用notifyItemChangednotifyDataSetChanged 时,列表会向右滚动几个像素。 reverseLayout=false 不会发生这种情况。

只有在 RecyclerView 的大小为 MATCH_PARENT 时才会发生这种情况,当我手动将其设置为任何 dp 时,问题就消失了。

有什么想法会发生什么以及如何应对?

【问题讨论】:

  • 现在我只是手动设置 recyclerView 的大小,它可以作为一种解决方法,但如果能正确修复它会很棒。

标签: android android-recyclerview linearlayoutmanager


【解决方案1】:

也许为时已晚,但无论如何,我要把它留在这里。 在 recyclerview 上使用具有匹配约束的约束布局(具有水平约束的 0dp)解决了我的问题。

【讨论】:

  • 0dp 对我不起作用。 MATCH_PARENT 实际上修复了它,但我希望它在两个 WRAP_CONTENT 视图之间,所以不能使用它。
【解决方案2】:

以编程方式设置 RecyclerView 尺寸对我有用。

在科特林中-

constraintLayout.doOnLayout {
    recyclerview.layoutParams.height = bottomLayout.top - appBarLayout.bottom
}

xml -

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintBottom_toTopOf="@id/bottomLayout" />

我想要在bottomLayout和appBarLayout之间的recyclerView和constraintLayout是根布局。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-25
    • 1970-01-01
    • 1970-01-01
    • 2016-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多