简体   繁体   English

如何找出第一次在回收站视图中创建的项目?

[英]How to find out item is created in recycler view for the first time?

I have a recycler view and I want to find out if an item is created in recycler view for the first time or not?我有一个回收站视图,我想知道一个项目是否是第一次在回收站视图中创建的?

Is there an event handler for it?有它的事件处理程序吗?

Note: I know how to implement it with flag, but I'm looking for another approach.注意:我知道如何使用标志来实现它,但我正在寻找另一种方法。

Why do you want this?你为什么要这个? To my mind it seems like there is not a good reason to know this information, a ViewHolder should be independent from that knowledge.在我看来,似乎没有充分的理由知道这些信息,ViewHolder 应该独立于这些知识。

Nothing is provided for this by RecyclerView.Adapter , the only callbacks are to manage the ViewHolder instances, which can be recycled and so any particular instance wouldn't know if it is the first instance or not. RecyclerView.Adapter没有为此提供任何内容,唯一的回调是管理ViewHolder实例,该实例可以回收,因此任何特定实例都不知道它是否是第一个实例。

You could store a flag in your data model and access it when you set up the ViewHolder in onBindViewHolder , as you seem to know.正如您似乎知道的onBindViewHolder ,您可以在数据模型中存储一个标志,并在您在 onBindViewHolder 中设置 ViewHolder 时访问它。

You can ovveride onViewAttachedToWindow(holder: ViewHolder) in your recycler adapter.您可以在回收器适配器中覆盖 onViewAttachedToWindow(holder: ViewHolder)。 sample code like this:示例代码如下:

private var isFirstChildAttached = false // single fire
override fun onViewAttachedToWindow(holder: ViewHolder) {
    super.onViewAttachedToWindow(holder)
    holder.pageView.registerTopLevelTouchListener()


    if (!isFirstChildAttached) {
        isFirstChildAttached = true
        checkAndFirePageDimen(holder.itemView)
    }
}

private fun checkAndFirePageDimen(itemView: View){
    itemView.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
        override fun onGlobalLayout() {
            itemView.viewTreeObserver.removeOnGlobalLayoutListener(this)
            pagerCallback.onFirstItemAttachedToPager(itemView)
        }
    })
}

"checkAndFirePageDimen" helps you for reliable itemView size. “checkAndFirePageDimen”帮助您获得可靠的 itemView 大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何找出回收者视图被捕捉到的项目? - How to find out which item the recycler view is snapped to? 查找回收者视图中的第一个可见项目是否为列表中的第一项 - Find if the first visible item in the recycler view is the first item of the list or not Espresso - 如何在回收站视图中找到特定项目(顺序是随机的) - Espresso - how to find a specific item in a recycler view (order is random) Java:每次删除项目时如何更新回收站视图? - Java: How to update the Recycler View every time I delete an item? android animate recycler查看第一项 - android animate recycler view first item 在第一个项目中添加图像,当创建另一个项目时,然后显示前一个项目的图像,而不是显示空的子回收器视图 - Added images in the first item, when created another item, then showing the images of previous item instead of showing empty child recycler view 回收者视图将第一项作为标题 - Recycler view making first item as a header 文本在回收站视图项中超出屏幕 - Text goes out of screen in recycler view item Recycler View Item Deletion index out of bound - Recycler View Item Deletion index out of bound 如何处理在“回收者视图”中创建的动态视图 - How to handle dynamic view created in Recycler View
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM