header

mail@pastecode.io avatar
unknown
kotlin
a year ago
2.5 kB
2
Indexable
Never
class CatalogItem(val catalog: SurveyCatalog) :
    AbstractExpandableHeaderItem<CatalogItem.CatalogViewHolder, CatalogSubItem>() {
    init {
        val subItems = mutableListOf<CatalogSubItem>()
        catalog.client?.forEach {
            val subItem = CatalogSubItem(it, this)
            Log.d("sado", ": ${it == null}" )
            subItems.add(subItem)
        }
        setSubItems(subItems)
    }

    class CatalogViewHolder(
        view: View?,
        adaper: FlexibleAdapter<IFlexible<RecyclerView.ViewHolder>>?
    ) : ExpandableViewHolder(view, adaper) {
        var name: TextView?
        var description: TextView?
        var expandButton: ImageView?

        init {
            name = view?.findViewById(R.id.survey_name)
            description = view?.findViewById(R.id.survey_desc)
            expandButton = view?.findViewById(R.id.expand_btn)
        }
    }

    override fun equals(other: Any?): Boolean {
        other?.let {
            return this == (it as CatalogItem)
        }
        return false
    }

    override fun hashCode(): Int {
        return Objects.hash(catalog.id)
    }

    override fun getLayoutRes(): Int {
        return R.layout.listitem_dynamic_checklist_catalog
    }

    override fun createViewHolder(
        view: View?,
        adapter: FlexibleAdapter<IFlexible<RecyclerView.ViewHolder>>?
    ): CatalogViewHolder = CatalogViewHolder(view, adapter)

    override fun bindViewHolder(
        adapter: FlexibleAdapter<IFlexible<RecyclerView.ViewHolder>>?,
        holder: CatalogViewHolder?,
        position: Int,
        payloads: MutableList<Any>?
    ) {
        holder?.apply {
            name?.text = catalog.surveyName
            description?.text = catalog.surveyDescription
            if (adapter?.isExpanded(position) == true) {
                expandButton?.setImageDrawable(
                    ContextCompat.getDrawable(
                        itemView.context,
                        R.drawable.ic_up_arrow
                    )
                )
            } else {
                expandButton?.setImageDrawable(
                    ContextCompat.getDrawable(
                        itemView.context,
                        R.drawable.ic_down_arrow
                    )
                )
            }

            itemView.setOnClickListener {
                Log.d("sado", "bindViewHolder: " + subItems.size)
            }
        }
    }
}