Home > Net >  Automatically hide items in recyclerView
Automatically hide items in recyclerView

Time:10-04

I'm trying to figure out how to get access to the views in my recyclerView. I want to be able to expand items in the recyclerView. When one expands, all the others should collapse.

class CustomAdapter(private val mList: List<Card>) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {

class ViewHolder(ItemView: View) : RecyclerView.ViewHolder(ItemView) {
    val textView: TextView = itemView.findViewById(R.id.questionView)
    val categoryView: TextView = itemView.findViewById(R.id.category_view)
    val solutionView: TextView = itemView.findViewById(R.id.solutionView)
    val editButton: FloatingActionButton = itemView.findViewById(R.id.floatingActionButton)
}


// create new views
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    val view = LayoutInflater.from(parent.context)
        .inflate(R.layout.items_view_design, parent, false)
    return ViewHolder(view)
}

// binds the list items to a view
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    val itemsViewModel = mList[position]


    holder.textView.text = itemsViewModel.question
    holder.categoryView.text = itemsViewModel.subjectCategory
    holder.solutionView.text = itemsViewModel.solution


    fun showHide(view:View) {
        view.visibility = if (view.visibility == View.VISIBLE){
            View.GONE
        } else{
            View.VISIBLE
        }
    }

    fun show(view:View){
        view.visibility =View.VISIBLE
    }

    fun hide(view:View){
        view.visibility = View.GONE
    }


    
    holder.textView.setOnClickListener{
        showHide(holder.solutionView)
        showHide(holder.editButton)
        }

        //This is where I need help.

}'''

In pseudocode, what I want to say is:

  • for loop (items in mList)
  • items.solution = holder.solutionView
  • hide(solutionView)

But I can't figure out how to get access to holder.categoryView for an item that isn't the current item being bound.

CodePudding user response:

As I understand you need a property to indicate that an item was chosen and use it to check whether to hide or show some elements of your views:

class CustomAdapter(private val mList: List<Card>) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {
    private var selectedCard: Card? = null

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val itemsViewModel = mList[position]
        // Your code
        if (selectedCard != null && selectedCard != itemsViewModel) {
            showHide(holder.solutionView)
            showHide(holder.editButton)
        }
        holder.textView.setOnClickListener{
           if (itemsViewModel == selectedCard) {
              selectedCard = null
           } else {
              selectedCard = itemsViewModel
           }
           notifyDataSetChanged() // It's not optimal, but the easiest way
        }
    }
}

CodePudding user response:

Thanks for your help. I followed the link posted by Umesh RecyclerView expand/collapse items

Worked for me! This is a Kotlin translation of the answer he linked to:

    global variables:
    // private var previousExpandedPosition = -1
   // private var mExpandedPosition = -1

  var isExpanded: Boolean = (position == mExpandedPosition)
    holder.details.setVisibility(if (isExpanded) View.VISIBLE else View.GONE)
    holder.itemView.isActivated = isExpanded

    if (isExpanded) previousExpandedPosition = position

    holder.itemView.setOnClickListener {
        mExpandedPosition = if (isExpanded) -1 else position
        notifyItemChanged(previousExpandedPosition)
        notifyItemChanged(position)
    }
  • Related