mikepenz/MaterialDrawer

Clickable BadgeStyle

virovinrom opened this issue · 9 comments

Hello, is it possible to make somehow BadgeStyle clickable?

@virovinrom could you please elaborate more?

BadgeStyle has a different color if pressed: https://github.com/mikepenz/MaterialDrawer/blob/develop/materialdrawer/src/main/java/com/mikepenz/materialdrawer/holder/BadgeStyle.kt

also the text style is allowed to be a ColorStateList where pressed could also be handled

How can I set some ItemClickListener on BadgeStyle?

thank you for adding more details on your question.

This is not possible with the default item.

you'd have to implement your own custom drawer item or expand on one of the existing items to add a listener to it.

Sorry, one more question,
I need to change for example PrimaryDrawerItem or AbstractBadgeableDrawerItem and not BadgeStyle ?
I tied implement listener to my custom BadgeStyle and it didn't work.

You could for example try something like:

open class CustomPrimaryDrawerItem : AbstractBadgeableDrawerItem<CustomPrimaryDrawerItem>() {

    open var onBadgeClickListener: ((v: View?, item: IDrawerItem<*>) -> Unit)? = null
    
    override fun bindView(holder: ViewHolder, payloads: List<Any>) {
        super.bindView(holder, payloads)

        holder.badge.setOnClickListener {
            onBadgeClickListener?.invoke(it, this@CustomPrimaryDrawerItem)
        }
    }
}

There are surely more efficient way (this will attach a new listener each time bindView is called), but this should be fine for normal drawers without hundreds of hundreds of items.

Alternative a bit more efficient for longer lists

open class CustomPrimaryDrawerItem : AbstractBadgeableDrawerItem<CustomPrimaryDrawerItem>() {
    open class ViewHolder(view: View) : BaseViewHolder(view) {
        val badge: TextView = view.findViewById(R.id.material_drawer_badge)
    }
}
binding.slider.adapter.addClickListener({ vh: CustomPrimaryDrawerItem.ViewHolder -> vh.badge }) { v: View, position: Int, fastAdapter: FastAdapter<IDrawerItem<*>>, item: IDrawerItem<*> ->  
            // to what you'd like to achieve
}

Much obliged for your help, but I'm encounter with some additional problems in your examples.
In first example he can't see bindView, and holder.badge can't be available in the same module only.
But the second one, no errors, but no catch the click (.

@virovinrom oh for that you'd also need to expose the badge via the viewholder

open class CustomPrimaryDrawerItem : AbstractBadgeableDrawerItem<CustomPrimaryDrawerItem>() {

    open var onBadgeClickListener: ((v: View?, item: IDrawerItem<*>) -> Unit)? = null

    override fun bindView(holder: AbstractBadgeableDrawerItem.ViewHolder, payloads: List<Any>) {
        super.bindView(holder, payloads)

        holder.itemView.findViewById<View>(R.id.material_drawer_badge).setOnClickListener {
            onBadgeClickListener?.invoke(it, this@CustomPrimaryDrawerItem)
        }
    }
}

It works,
A huge thank you.