Skip to content

An improvement recyclerview-expandable integrated with SectionedRecyclerViewAdapter

License

Notifications You must be signed in to change notification settings

Phanapoch/SectionedExpandableRecyclerView

Repository files navigation

Release Download Codacy Badge

SectionedExpandableRecyclerView

An improvement recyclerview-expandable integrated with SectionedRecyclerViewAdapter

Demo: Nexus 5

SectionedExpandableRecyclerView GIF

Download

allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}
  
dependencies {
	implementation 'com.github.Phanapoch:SectionedExpandableRecyclerView:0.1.2'
}

Usage

  1. create SectionedExpandableRecyclerView somewhere in your app
 <com.thevcgroup.phanapoch.sectionedexpandablerecyclerview.SectionedExpandableRecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:paddingBottom="8dp"
        android:paddingTop="8dp" />
  1. define your item_row and section_row
<!-- item_row -->
    <com.thevcgroup.phanapoch.sectionedexpandablerecyclerview.SectionedExpandableItem
        android:id="@+id/item_row"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layoutContent="@layout/view_content"
        app:layoutHeader="@layout/view_header"/>

<!-- section_row -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:text="Title test text"
        android:id="@+id/sectionTitle"/>
  1. custom a section class by extend StatelessSection
class MySection(val section: String, val context: Context) : StatelessSection(SectionParameters.builder()
        .itemResourceId(R.layout.item_row)
        .headerResourceId(R.layout.section_row)
        .build()) {

    private val items = arrayOf(
            Item(R.drawable.ic_test1, "14 Easy Weekend Getaways"),
            Item(R.drawable.ic_test3, "A Paris Farewell")
    )

    override fun getContentItemsTotal(): Int {
        return items.size
    }

    override fun getItemViewHolder(view: View?): ItemViewHolder {
        return ItemViewHolder(view!!)
    }
	// Expandable Item
    override fun onBindItemViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val itemHolder = holder as ItemViewHolder

        val (drawable, title) = items[position]

        itemHolder.imageView.setImageDrawable(context?.let { ContextCompat.getDrawable(it, drawable) })
        itemHolder.textView.text = title
    }
	// Section Header
    override fun getHeaderViewHolder(view: View?): HeaderViewHolder {
        return HeaderViewHolder(view!!)
    }

    override fun onBindHeaderViewHolder(holder: RecyclerView.ViewHolder?) {
        val headerHolder = holder as HeaderViewHolder
        headerHolder.sectionTitle.text = section
    }
}
  1. ViewHolder part
class ItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    val item: SectionedExpandableItem = itemView.findViewById(R.id.item_row) as SectionedExpandableItem
    val imageView: ImageView = item.headerLayout.findViewById(R.id.imageView) as ImageView
    val textView: TextView = item.headerLayout.findViewById(R.id.textView) as TextView
}

class HeaderViewHolder(headerView: View) : RecyclerView.ViewHolder(headerView) {
    val sectionTitle: TextView = headerView.findViewById(R.id.sectionTitle)
}
  1. final setup your recyclerview
val layout = LinearLayoutManager(this)
val adapter = SectionedExpandableRecyclerViewAdapter(layout)

adapter.addSection(MySection("A", this))
adapter.addSection(MySection("B", this))

recyclerView.setLayoutManager(layout)
recyclerView.setAdapter(adapter)

Credits project

License

SectionedExpandableRecyclerView is available under the MIT license