RecyclerView with reverseLayout" jumps"在notifyDataSetChanged上

时间:2017-12-05 20:41:01

标签: android android-recyclerview recycler-adapter linearlayoutmanager

我将recyclerView的layoutManager设置为:

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

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

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

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

任何想法会发生什么以及如何处理它?<​​/ p>

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

以编程方式设置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和ConstraintLayout之间的recyclerView是根布局。