【问题标题】:RecyclerView notifyItemRangeRemoved no animationRecyclerView notifyItemRangeRemoved 没有动画
【发布时间】:2021-03-18 14:17:51
【问题描述】:

我将 recyclerView 与 notifyItemRangeInserted 和 notifyItemRangeRemoved 一起使用。

我屏幕中的 RecyclerView 使用了width="match_parent" and height="wrap_content"

我需要在我的 recylerView 中显示或隐藏 ALL 元素。我使用了这段代码,效果很好。更改visibility标志后元素隐藏和显示。

但由于某种原因,存在插入动画而没有删除动画。为什么?

class MyAdapter : RecyclerView.Adapter<ViewHolder>() {

var objects: List<MyData> = emptyList()
    set(value) {
        field = value
        notifyDataSetChanged()
    }

var visibility: Boolean = true
    set(value) {
        field = value

        when (field) {
            true  -> notifyItemRangeInserted(0, cards.size)
            false -> notifyItemRangeRemoved(0, cards.size)
        }
    }

override fun getItemCount(): Int = if (visibility) objects.size else 0

【问题讨论】:

  • 看起来问题是wrap_content recyclerview 的高度。删除所有项目后,recyclerviews 的高度变为 0(在下一次布局过程中),因此它无法运行任何消失的动画。在插入过程中不会发生这种情况,因为高度在动画之前已扩展,因此运行它们没有问题。
  • 我可以尝试使用 match_parent 吗?但问题是给定列表是主列表中的嵌套列表。我有一个全局列表,其项目由视图 + 附加视图列表表示(这是嵌套列表的适配器)
  • 我认为,我不能将 match_parent 用于内部列表(
  • 如果这个RecyclerView 是外部RecyclerView 中的一个项目/行,为什么不在父列表上使用notifyItemRemoved() 和notifyItemInserted()?
  • 因为项目/行在列表之外还包括一个视图。如果我这样做,我将失去在视图中的其他视图的显示

标签: android android-recyclerview adapter android-adapter expandablerecyclerview


【解决方案1】:

上网查了一下,发现RecyclerView在height = wrap_content时动画有问题。

在某些版本的回收库谷歌中 这已修复,但从人们的回答中我发现该问题与 androidx 库有关

【讨论】:

    猜你喜欢
    • 2015-03-01
    • 1970-01-01
    • 2015-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-24
    • 2021-07-03
    相关资源
    最近更新 更多