mikepenz/FastAdapter

Can't get Item from viewHolder

simoneraffaelli opened this issue · 5 comments

Can't get Item from viewHolder

  • I've set up a CustomEventHook (to register a listener for a switch), but in onAttach when I use the method FastAdapter.getHolderAdapterItem<*>(viewHolder) it returns always null. I noticed that getHolderAdapterPosition(holder) is always -1. If I use the method FastAdapter.getFromHolderTag(viewHolder) inside the adapter I see my item correctly. I think I'm missing something.
    I use view binding item, and I have not found a similar recent issue.
  • Here's the code of the EventHook: https://gist.github.com/Raffaa/7b61e2e4ba7be41278c3167cee25c324

Details

  • [5.3.5 ] Used library version

Checklist

@raffaa haven't checked into depth, but I believe when the onAttach is occurring, it's to early to get the item from the ViewHolder, would need to look into the internals from the RecyclerView when the information is set.

Could you maybe explain why you want to retrieve information in onAttach?

Setting the UI is meant to be done in the bindView call of the item instead

@mikepenz in onAttach I need informations of my item because in there I set the switch checkedlistener that need to modify the model. I used to set the listener inside bindView but I think is incorrect because I recreate a new listener every time bind view is called (which I think is a bit too much).

I think I'm dumb.... if I get the item inside the listener then all should work.... will do some tests

but why do you need the item in the onAttach? (I think you actually just identified that :D)

Instead you can get the item when the actual action happens. You want to have the listener on ViewHolder level which is != Item level.
the ViewHolder will be re-used many times, and so should the listener
-> that's actually the main reason for the EventHook API. Make the listeners be re-used and not re-bound every single time bindView is called -> results in far less objects being created when you scroll in lists and such :)

@mikepenz yeah you're right, I placed getHolderAdapterItem function inside listener and all is working flawlessly.
you can close this issue

thank you :)

You are welcome ☺️