简体   繁体   中英

What is the findViewHolderForAdapterPosition inside an adapter Equivelant?

I have a custom adapter with 16 different view holder types. I would like to obtain the view holder at a specific position when a method is called.

Now I can do this outside of the adapter with

recyclerView.findViewHolderForAdapterPosition(0)

Is it possible to create this effect inside the adapter?

Also to clarify, I do not mean anything involving "onBindViewHolder" as I wish to iterate through my list of items and obtain details from the various textfields and radio buttons inside my adapter.

Additional edit regarding my design: The reason for this feature is the last view holder is a "submit" button. When clicked, it calls a method to iterate through the various adapter's textfields and radio buttons to build a custom object. Then making a callback to the activity. Here the object with the iterated data is uploaded to the server

I'm not entirely sure what's your design but ViewHolder s should only present the data and not hold any valuable information that would be worth iterating over. Regardless:

RecyclerView.Adapter can be used by multiple RecyclerView s simultaneously so there's no 1:1 relationship.

If you're using it only for one you can just keep hard reference to your recyclerView and call findViewHolderForAdapterPosition on it:

// inside Adapter
private var currentRecyclerView : RecyclerView? = null

override fun onAttachedToRecyclerView(recyclerView : RecyclerView) {
    currentRecyclerView = recyclerView
}

override fun onDetachedFromRecyclerView(recyclerView : RecyclerView){
    currentRecyclerView = null
}

Then you can use currentRecyclerView?.findViewHolderForAdapterPosition(Int) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM