禁用特定项目的RecyclerView itemAnimator

时间:2016-07-24 10:19:26

标签: android animation android-recyclerview

在recyclerView itemAnimator android中,可以实时禁用/启用特定适配器位置的自动itemAnimation动画吗?

2 个答案:

答案 0 :(得分:1)

尝试以下代码段:

COPY yarn.lock /usr/src/app/

您应根据需要修改val ignoreList = listOf<Int>(5, 6, 7) recyclerView.itemAnimator = object : DefaultItemAnimator(){ override fun canReuseUpdatedViewHolder(viewHolder: RecyclerView.ViewHolder): Boolean { if(ignoreList.contains(viewHolder.adapterPosition)) return true; else return super.canReuseUpdatedViewHolder(viewHolder) } }

答案 1 :(得分:0)

我也找不到执行此操作的简单方法。似乎动画师是布局管理器的一部分,并且对其动画项目一无所知。因此,我唯一想到的就是当我不希望动画会刷新整个RecyclerView时调用{ "subject": { "chapter": { "topic": "value", "subtopic": "value" }, "chapter": { "topic": "value", "subtopic": "value" }, }, "subject": { "chapter": { "topic": "value", "subtopic": "value" }, "chapter": { "topic": "value", "subtopic": "value" }, }, }

我通过扩展notifyDataSetChanged并覆盖ListUpdateCallback方法来做到这一点。在示例代码中会更容易看到:

onMoved

您可以在setItems或SubmitList或适配器的任何方法中调用自定义ListUpdateCallback。

// my recyclerview is a list of events. You can click a checkbox to favourite an 
// event. When favourited, it moves to the top of the recyclerview. I want this move 
// animated but not vice versa. So when something is unfavourited, I don't want an 
// animation of the move when it goes back to its original position.
inner class CustomCallback(val newItems: List<Event>) : ListUpdateCallback {

    override fun onInserted(position: Int, count: Int) {
        notifyItemRangeInserted(position, count)
        Timber.d("NotificationCallback onInserted position: $position count: $count")
    }

    override fun onChanged(position: Int, count: Int, payload: Any?) {
        notifyItemRangeChanged(position, count)
        Timber.d("NotificationCallback onChanged position: $position (${newItems[position].eventName}) count: $count")
    }

    override fun onMoved(fromPosition: Int, toPosition: Int) {
        if (newItems[toPosition].isFavourite == true) {
            Timber.d("item starred so animation")
            notifyItemMoved(fromPosition, toPosition)
        } else if (newItems[toPosition].isFavourite == false) {
            Timber.d("item unstarred so no animation")
            notifyDataSetChanged()
        }
        Timber.d("NotificationCallback onMoved from: $fromPosition (${newItems[fromPosition].eventName}) to: $toPosition (${newItems[toPosition].eventName})")
    }

    override fun onRemoved(position: Int, count: Int) {
        notifyItemRangeRemoved(position, count)
        Timber.d("NotificationCallback onRemoved position: $position count: $count")
    }
}