0

I'm using Groupie as a recycle view adapter and I was able to call a activity method inside the class. the code that call the method as show below.

(it.context as <activity_name>).<function_name>()

but the problem is i wonder how to call a method from a fragment inside the class? Here is my code

    class BindCartItemList(val cartItem: CartList) : Item<GroupieViewHolder>() {

    override fun getLayout() = R.layout.ryr_cartlist_with_select

    override fun bind(viewHolder: GroupieViewHolder, position: Int) {

        viewHolder.itemView.ryr_product_name.text = cartItem.product?.productName
        viewHolder.itemView.ryr_quantity.setText(cartItem.quantity.toString())
        viewHolder.itemView.ryr_product_barcode.text = cartItem.product?.productBarcode

        //compact conversion
        val mConvert = FormatConversion()

        //default for display price and amount
        viewHolder.itemView.ryr_unit_price.text = mConvert.compactAmount(cartItem.product?.price!!)

        cartItem.amount = cartItem.quantity * cartItem.product?.price!!
        viewHolder.itemView.ryr_amount.text = mConvert.compactAmount(cartItem.amount)

        //quantity decrease
        viewHolder.itemView.ryr_btn_decrease.setOnClickListener {
            cartItem.quantity -= 1
            //not allow user set qty < 1
            if (cartItem.quantity < 1) {
                cartItem.quantity = 1
            } else {
                //once button press qty and amount will be change
                viewHolder.itemView.ryr_quantity.setText(cartItem.quantity.toString())

                cartItem.amount = cartItem.quantity * cartItem.product?.price!!
                viewHolder.itemView.ryr_amount.text = mConvert.compactAmount(cartItem.amount)
            }
            (it.context as PosActivity).updateTotalAmount()
        }

        //quantity increase
        viewHolder.itemView.ryr_btn_increase.setOnClickListener {
            cartItem.quantity += 1
            viewHolder.itemView.ryr_quantity.setText(cartItem.quantity.toString())

            cartItem.amount = cartItem.quantity * cartItem.product?.price!!
            viewHolder.itemView.ryr_amount.text = mConvert.compactAmount(cartItem.amount)

            (it.context as PosActivity).updateTotalAmount()

        }


        //press container_1 for checkbox
        viewHolder.itemView.ryr_container_1.setOnClickListener {
            if (viewHolder.itemView.ryr_selected.isChecked) {
                viewHolder.itemView.ryr_selected.isChecked = false
                cartItem.selected = false
            } else if (!viewHolder.itemView.ryr_selected.isChecked) {
                viewHolder.itemView.ryr_selected.isChecked = true
                cartItem.selected = true
            }
        }

    }


}

Here is the code that work at activity but not work in fragment

1

1 Answer 1

1

Generally your data classes and therefore your Items shouldn't be mutable, in that case, it'd look like so:

class BindCartItemList(val cartItem: CartList, val actionHandler: ActionHandler) : Item<GroupieViewHolder>() {
    interface ActionHandler {
        fun onCartItemDecreaseClicked(cartItem: CartList)

        fun onCartItemIncreaseClicked(cartItem: CartList)

        fun onCartItemSelectionToggled(cartItem: CartList, shouldBeChecked: Boolean)
    }

    private val mConvert = FormatConversion() // compact conversion

    override fun getLayout() = R.layout.ryr_cartlist_with_select

    override fun bind(viewHolder: GroupieViewHolder, position: Int) {

        viewHolder.itemView.ryr_product_name.text = cartItem.product?.productName
        viewHolder.itemView.ryr_quantity.setText(cartItem.quantity.toString())
        viewHolder.itemView.ryr_product_barcode.text = cartItem.product?.productBarcode

        viewHolder.itemView.ryr_amount.text = mConvert.compactAmount(cartItem.quantity * cartItem.product?.price!!)

        //quantity increase
        viewHolder.itemView.ryr_btn_increase.setOnClickListener {
            actionHandler.onCartItemIncreaseClicked(cartItem)
        }

        //quantity decrease
        viewHolder.itemView.ryr_btn_decrease.setOnClickListener {
            actionHandler.onCartItemDecreaseClicked(cartItem)
        }

        //press container_1 for checkbox
        viewHolder.itemView.ryr_container_1.setOnClickListener {
            val isChecked = viewHolder.itemView.ryr_selected.isChecked
            actionHandler.onCartItemSelectionToggled(cartItem, !isChecked)
        }
    }    
}

All other mutations belong outside of the adapter item, and could potentially be handled in the Fragment.

class YourFragment: Fragment(), BindCartItemList.ActionHandler {
    ....

To update the item, you can either refresh the items in the adapter, or you can use item.notifyChanged().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.