Advertisement
Guest User

adapter

a guest
Jul 9th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.83 KB | None | 0 0
  1. package com.jg.internetradio.ui.fragment.categorylist.recyclerview
  2.  
  3. import android.support.v7.widget.RecyclerView
  4. import android.view.ViewGroup
  5. import android.view.LayoutInflater
  6. import android.databinding.DataBindingUtil
  7. import android.view.View
  8.  
  9. import com.jg.internetradio.databinding.CategoryListItemBinding
  10. import com.jg.internetradio.entity.Category
  11. import com.jg.internetradio.R
  12. import com.jg.internetradio.ui.fragment.categorylist.OnCategoryClick
  13.  
  14. class CategoryListAdapter(var categories: List<Category> = emptyList(), val categoryClickListener: OnCategoryClick) : RecyclerView.Adapter<CategoryListAdapter.ViewHolder>() {
  15.     interface AdapterNotifier {
  16.         fun onChanged(position: Int)
  17.     }
  18.  
  19.     class ViewHolder(val binding: CategoryListItemBinding) : RecyclerView.ViewHolder(binding.root) {
  20.  
  21.         fun bind(position: Int, expandablePosition: MutableSet<Int>, adapterNotifier: AdapterNotifier) {
  22.             if (binding.category?.description?.isEmpty() == true) {
  23.                 binding.categoryDescription.visibility = View.GONE
  24.                 binding.arrowImage.visibility = View.GONE
  25.             } else {
  26.  
  27.                 binding.arrowImage.visibility = View.VISIBLE
  28.  
  29.                 val isExpanded = expandablePosition.contains(position)
  30.                 binding.arrowImage.setImageResource(if (isExpanded) R.drawable.ic_up_arrow else R.drawable.ic_down_arrow)
  31.                 binding.categoryDescription.visibility = if (isExpanded) View.VISIBLE else View.GONE
  32.                 binding.cardView.isActivated = isExpanded
  33.                 binding.cardView.setOnLongClickListener {
  34.                     if (isExpanded)
  35.                         expandablePosition.remove(position)
  36.                     else
  37.                         expandablePosition.add(position)
  38.                     adapterNotifier.onChanged(position)
  39.                     isExpanded
  40.  
  41.                 }
  42.             }
  43.         }
  44.     }
  45.  
  46.     init {
  47.         setHasStableIds(true)
  48.     }
  49.  
  50.     private var expandablePosition = mutableSetOf<Int>()
  51.  
  52.     override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
  53.         val binding: CategoryListItemBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.context),
  54.                 R.layout.category_list_item, parent, false)
  55.         binding.callback = categoryClickListener
  56.         return ViewHolder(binding)
  57.     }
  58.  
  59.     override fun onBindViewHolder(holder: ViewHolder, position: Int) {
  60.         holder.binding.category = categories[position]
  61.  
  62.         holder.bind(position, expandablePosition, object : AdapterNotifier {
  63.             override fun onChanged(position: Int) {
  64.                 notifyItemChanged(position)
  65.             }
  66.         })
  67.     }
  68.  
  69.     override fun getItemId(position: Int) = categories[position].id.toLong()
  70.  
  71.     override fun getItemCount() = categories.size
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement