mikepenz/FastAdapter

Click listener for children views of item that extends AbstractBindingItem

runo280 opened this issue · 8 comments

This is my Item class:

class ChannelItem : AbstractBindingItem<ChannelListItemBinding>() {
    lateinit var channel: ChannelEntity

    override val type: Int
        get() = R.id.channel_list_item

    override fun bindView(binding: ChannelListItemBinding, payloads: List<Any>) {
          ...
    }

    override fun createBinding(
        inflater: LayoutInflater,
        parent: ViewGroup?
    ): ChannelListItemBinding {
        return ChannelListItemBinding.inflate(inflater, parent, false)
    }
}

I want to set click listener for views inside of my item, I followed your sample but I can't access to ViewHolder of ChannelItem:
image

@runo280 it looks like you are mixing up Item vs. ViewHolder the ChannelItem is the construct holding the data and defining the binding logic, whereas the ViewHolder holds the views displayed in the RecyclerView

Screenshot 2021-04-22 at 09 26 39

You want to adjust the type check to the ViewHolder instead

It seems I should have asked my question in different way, how can I access to ViewHolder when we use AbstractBindingItem?

@runo280 this abstract item is not designed to access the viewHolder, as it abstracts its existence away.

You can look at it's implementation though and adjust it for your needs: https://github.com/mikepenz/FastAdapter/blob/develop/fastadapter-extensions-binding/src/main/java/com/mikepenz/fastadapter/binding/AbstractBindingItem.kt

@runo280 this abstract item is not designed to access the viewHolder, as it abstracts its existence away.

You can look at it's implementation though and adjust it for your needs: https://github.com/mikepenz/FastAdapter/blob/develop/fastadapter-extensions-binding/src/main/java/com/mikepenz/fastadapter/binding/AbstractBindingItem.kt

So it is not possible to use AbstractBindingItem with addEventHook?

Same problem here

how to access the ClickEventHook with a AbstractBindingItem ? the ViewHolder don't exist...

Thanks !

@sebastianperdomo there is a ViewHolder but it's of the type: BindingViewHolder<ViewBinding>

It's a bit late but maybe it will still help: I used this suggested extension to resolve this without switching item type to ViewHolder

class ClickEvent(private val listener: Listener) : ClickEventHook<FavoriteEmptyItem>() {

        override fun onBindMany(viewHolder: RecyclerView.ViewHolder): List<View> {
            val viewList = listOf(
                viewHolder.asBinding<ItemEmptyBinding> {
                    it.buttonItemEmptyDestination
                },
                viewHolder.asBinding<ItemEmptyBinding> {
                    it.imageViewItemEmptyRemove
                }
            )
            return viewList.filterNotNull()
        }

        override fun onClick(v: View, position: Int, fastAdapter: FastAdapter<FavoriteEmptyItem>, item: FavoriteEmptyItem) {
            when (v.id) {
                R.id.buttonItemEmptyDestination -> listener.onEmptyItemDestinationClick(item, position)
                R.id.imageViewItemEmptyRemove -> listener.onEmptyItemRemoveClick(item, position)
                else -> Timber.w("Unknown FavoriteEmptyItem view id")
            }
        }
    }

Not sure if it's perfect but it works 👌

fastAdapter.addEventHook(object : ClickEventHook<ChannelItem>(){

    override fun onBind(viewHolder: RecyclerView.ViewHolder): View {
        return ChannelListItemBinding.bind(viewHolder.itemView).xxx
    }

    override fun onClick(
        v: View,
        position: Int,
        fastAdapter: FastAdapter<ChannelItem>,
        item: ChannelItem
    ) {
        
    }
})