Reputation: 2259
I am just trying to get my view holder seen on screen, but the methods findViewHolderFor* is given null object almost all the time.
My Code from my Adapter:
fun getVisibleViewHolder(recyclerView: RecyclerView) {
for (i in 0 until recyclerView.childCount) {
val viewHolder = recyclerView.findViewHolderForLayoutPosition(i) // null: most of the time (specially when scroll performed)
val viewHolder = recyclerView.findViewHolderForAdapterPosition(i) // idem
}
}
Sometimes it's working, but it's rare.
Upvotes: 0
Views: 3151
Reputation: 2259
So I found the solution! And the explanation. recyclerView.childCount = Number of element in parent which is obviously different from the position!
for (i in 0 until recyclerView.childCount) {
val view = recyclerView.getChildAt(i)
val viewHolder = recyclerView.findContainingViewHolder(view)
... // the purpose, getting the data from the user here
}
Upvotes: 1