mardi 20 octobre 2020

Android Spinner With Checkbox

I am trying to implement a custom spinner with 5 items with checkboxes, basically, the custom layout for the dropdown item has a checkbox and a textView for the title. I noticed that the onItemSelected Listener stops firing and I cannot select any individual item, it does not register that I have selected a particular item even if I click the checkbox. Is there a workaround for this so that when I check any checkbox it registers that the item has been selected

class CustomAdapter(
    context: Context,
    resource: Int,
    private val colorsList: List<ColorStateVO>
) :
    ArrayAdapter<ColorStateVO?>(context, resource, daysList) {
    private var getPosition: Int = 0
    override fun getDropDownView(
        position: Int, convertView: View?,
        parent: ViewGroup?
    ): View? {
        return getCustomView(position.plus(1), convertView, parent)
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        val view = createViewFromResource(convertView, parent, layout.spinner_item)
        val textView = view.findViewById<TextView>(id.spinnerText)
        textView.text = coloursList[position].title
        return view
    }

    private fun createViewFromResource(
        convertView: View?,
        parent: ViewGroup,
        layoutResource: Int
    ): View {
        val context = parent.context
        return convertView ?: LayoutInflater.from(context).inflate(layoutResource, parent, false)
    }

    override fun getCount(): Int = daysList.size - 1
    private fun getCustomView(
        position: Int, convertView: View?,
        parent: ViewGroup?
    ): View {
        var holder = ViewHolder()
        val view: View
        if (convertView == null) {
            view =
                LayoutInflater.from(context).inflate(layout.spinner_checkbox_item, parent, false)

            holder.colorTitleTextView = view.findViewById(id.checkbox_text)
            holder.colorCheckBox = view.findViewById(id.spinnerCheckbox)
            view.tag = holder
        } else {
            holder = convertView.tag as ViewHolder
            view = convertView
        }
        holder.colorTitleTextView?.text = colorsList[position].title

        holder.colorCheckBox?.isChecked = colorsList[position].selected

        holder.colorCheckBox?.tag = position
        holder.colorCheckBox?.setOnCheckedChangeListener { buttonView, isChecked ->
   
            colorsList[position].selected = isChecked
            if(isChecked) {
               //Do something
            }
        }
        holder.colorTitleTextView?.setOnClickListener {
            holder.colorCheckBox?.toggle()
        }
        return view
    }

    override fun getPosition(item: ColorStateVO?): Int {
        return super.getPosition(item)
    }

    inner class ViewHolder {
        var colorTitleTextView: TextView? = null
        var colorCheckBox: CheckBox? = null
    }
}

data class ColorStateVO(val title: String, var selected: Boolean)```



Aucun commentaire:

Enregistrer un commentaire