luizgrp / SectionedRecyclerViewAdapter

An Adapter that allows a RecyclerView to be split into Sections with headers and/or footers. Each Section can have its state controlled individually.
MIT License
1.68k stars 372 forks source link

How to close a section in Expandable Section? #132

Closed douglasalipio closed 5 years ago

douglasalipio commented 6 years ago

Hi guys,

I'm trying to use the expandable section in my project but I have an issue to close a section. When I click on an arrow nothing happens. So, I think I'm doing the exact behaviour as the sample do. It's my code:

The section


class LessonMenuSection(private val titleSection: String,
                        private val sectionAdapter: SectionedRecyclerViewAdapter)
    : StatelessSection(SectionParameters.builder()
        .itemResourceId(R.layout.lesson_menu_item)
        .headerResourceId(R.layout.lesson_menu_head)
        .build()) {

    var expanded = true

    override fun getContentItemsTotal() = if (expanded) 1 else 0

    override fun getItemViewHolder(view: View) = LessonMenuHolder(view)

    override fun getHeaderViewHolder(view: View) = HeadHolder(view)

    override fun onBindItemViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val lessonHolder = holder as LessonMenuHolder
        lessonHolder.bind(titleSection)
    }

    override fun onBindHeaderViewHolder(holder: RecyclerView.ViewHolder) {
        val subHeaderHolder = holder as HeadHolder
        subHeaderHolder.bind(titleSection, sectionAdapter)
    }

    class HeadHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private var expanded = true

        fun bind(titleSection: String, sectionAdapter: SectionedRecyclerViewAdapter) {
            itemView.subheaderText.text = titleSection
            itemView.rootView.setOnClickListener {
                expanded = !expanded
                val imgID: Int = if (expanded) R.drawable.ic_keyboard_arrow_down_black
                else R.drawable.ic_keyboard_arrow_up_black
                itemView.arrow.setImageResource(imgID)
                sectionAdapter.notifyDataSetChanged()
            }
        }
    }

    class LessonMenuHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        fun bind(titleSection: String, clickListener: (LessonMenuHolder) -> Unit) {
            itemView.categoryName.text = "vai logo"
            itemView.setOnClickListener { clickListener(this) }
        }
    }
}

The way that I'm instantiating it.

override fun showAllLesson(categories: List<Category>) {
        categories.forEach { category ->
            sectionAdapter.addSection(LessonMenuSection(category.title, lessonClick, headerClick, sectionAdapter))
        }
        lessonMenu?.adapter = sectionAdapter
}

Just one more information. I'm using Conductor instead of Fragment.

yccheok commented 6 years ago

I'm not familiar with Kotlin. But, I notice you have 2 different variable scopes. Both of them named expanded. I guess you don't want to deal with the expanded within HeadHolder scope.