androidkotlinandroid-recyclerviewsectionedrecyclerviewadapter

sectioned recycler view adapter, hide empty sections


my issue is very similar to this: https://github.com/luizgrp/SectionedRecyclerViewAdapter/issues/130

what i want to do is hide empty sections, i've tried various ways to do that, but i'm failing it constantly. I believe someone could instantly point out what i must do in order to hide a section that's empty.

    val headerHolder = holder as HeaderViewHolder
    if (sectionItemsTotal<=1){
        headerHolder.itemView.cl_lesson_header.visibility = View.GONE
        holder.itemView.visibility = View.GONE
    }

this works, but shows empty views instead. tried sectionAdapter!!.notifyDataSetChanged() but it doesn't really help the case.


Solution

  • It is usually better to remove an item from aRecyclerView, so you won't have to worry about hiding it. If you don't remove the item from the RecyclerView then, to hide it, you would have to return some zero-height view from onCreateViewHolder(). (zero-width if horizontal)

    You can search for "hiding items RecyclerView" to get some hits on this topic. Also see this as an example.