0

Requirement is like how can I track the Parent Item is selected, on which the child item can be selected accordingly. But in my case without selecting the parent Item child can be selected which I want to restrict. I am using Nested to show the parent and child in Recyclerview like Hotel is the Parent Item and according to the Hotel, Rooms are being Listed in child Recyclerview. Go through the attachment for better visibility Structure 1 Structure 2

Parent RecyclerView

class HotelAdapter(private val context: Context, private var selectedHotelID: Int, private val interaction: Interaction) :
    RecyclerView.Adapter<HotelAdapter.NavigationOptionViewHolder>(),HotelRoomAdapter.Interaction {

    var selectedItemPos = -1
    var lastItemSelectedPos = -1

    private val viewPool = RecyclerView.RecycledViewPool()

    private val DIFF_CALLBACK = object : DiffUtil.ItemCallback<Hotel>() {

        override fun areItemsTheSame(
            oldItem: Hotel,
            newItem: Hotel
        ): Boolean {
            return oldItem.hotelName == newItem.hotelName
        }

        override fun areContentsTheSame(
            oldItem: Hotel,
            newItem: Hotel
        ): Boolean {
            return oldItem.equals(newItem)
        }
    }

    private val differ = AsyncListDiffer(this, DIFF_CALLBACK)

    /**
     * Interface for any kind of listener event in recyclerView
     * */
    interface Interaction {
        fun onItemHotelSelected(position: Int, item: Hotel)
    }

    class NavigationOptionViewHolder(
        val itemDataBindingUtil: RowHotelSelectionBinding,
        val interaction: Interaction
    ) :
        RecyclerView.ViewHolder(itemDataBindingUtil.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NavigationOptionViewHolder {
        val itemDatabinding = DataBindingUtil.inflate<RowHotelSelectionBinding>(LayoutInflater.from(parent.context),
                R.layout.row_hotel_selection, parent, false)
        return NavigationOptionViewHolder(
            itemDatabinding,
            interaction
        )
    }

    override fun getItemCount(): Int {
        return differ.currentList.size
    }

    fun submitList(list: List<Hotel>) {
        differ.submitList(list)
    }

    override fun onBindViewHolder(holder: NavigationOptionViewHolder, position: Int) {
        var item = differ.currentList[position]
        holder.itemDataBindingUtil.navigationItem = item
        holder.itemDataBindingUtil.clickEvent = interaction
        holder.itemDataBindingUtil.position = position
        holder.itemDataBindingUtil.mRadioButton.setOnCheckedChangeListener(null)

        if(selectedHotelID == item.hotelId) {
            selectedItemPos = position
            lastItemSelectedPos = position
        }

        if(position == selectedItemPos) {
            holder.itemDataBindingUtil.mRadioButton.isChecked = true
            holder.itemDataBindingUtil.container.setBackgroundResource(R.color.colorAccent)
        } else {
            holder.itemDataBindingUtil.mRadioButton.isChecked = false
            holder.itemDataBindingUtil.container.setBackgroundResource(R.color.white)
        }

        holder.itemDataBindingUtil.mRadioButton.setOnCheckedChangeListener { _, _ ->
            selectedItemPos = position
            selectedHotelID = item.hotelId
            lastItemSelectedPos = if(lastItemSelectedPos == -1)
                selectedItemPos
            else {
                notifyItemChanged(lastItemSelectedPos)
                selectedItemPos
            }
            notifyItemChanged(selectedItemPos)
        }

        holder.itemDataBindingUtil.hotelBanner.load(item?.hotelImages?.get(0)) {
            scale(Scale.FILL)
        }

        holder.itemDataBindingUtil.roomListView.apply {
            Toast.makeText(context,selectedItemPos.toString(),Toast.LENGTH_SHORT).show()
            layoutManager = LinearLayoutManager(holder.itemDataBindingUtil.roomListView.context, LinearLayoutManager.VERTICAL, false)
            adapter = HotelRoomAdapter(context,item.rooms, this@HotelAdapter, selectedItemPos)
            holder.itemDataBindingUtil.roomListView.setRecycledViewPool(viewPool)
        }
    }

    class ViewHolder(
        val itemDataBindingUtil: RowHotelSelectionBinding,
        val interaction: Interaction
    ) :
        RecyclerView.ViewHolder(itemDataBindingUtil.root)

    override fun onRoomSelected(position: Int, item: Room) {
        Toast.makeText(context,item.roomName,Toast.LENGTH_SHORT).show()
    }
}

Room Adapter

class HotelRoomAdapter(private val context: Context, private val children: List<Room>,
                       private val interaction: Interaction,private val parentSelectedItemPos: Int) :
    RecyclerView.Adapter<HotelRoomAdapter.NavigationOptionViewHolder>() {

    var selectedItemPos = -1
    var currentItemSelected: Int = 0
    var lastItemSelectedPos = -1

    private val viewPool = RecyclerView.RecycledViewPool()

    private val DIFF_CALLBACK = object : DiffUtil.ItemCallback<Room>() {

        override fun areItemsTheSame(
            oldItem: Room,
            newItem: Room
        ): Boolean {
            return oldItem.roomName == newItem.roomName
        }

        override fun areContentsTheSame(
            oldItem: Room,
            newItem: Room
        ): Boolean {
            return oldItem == newItem
        }
    }

    private val differ = AsyncListDiffer(this, DIFF_CALLBACK)

    /**
     * Interface for any kind of listener event in recyclerView
     * */
    interface Interaction {
        fun onRoomSelected(position: Int, item: Room)
    }

    class NavigationOptionViewHolder(
        val itemDataBindingUtil: RowRoomSelectionBinding,
        val interaction: Interaction
    ) :
        RecyclerView.ViewHolder(itemDataBindingUtil.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NavigationOptionViewHolder {
        val itemDatabinding = DataBindingUtil.inflate<RowRoomSelectionBinding>(LayoutInflater.from(parent.context),
            R.layout.row_room_selection, parent, false)
        return NavigationOptionViewHolder(
            itemDatabinding,
            interaction
        )
    }

    override fun getItemCount(): Int {
        return children.size
    }

    fun submitList(list: List<Room>) {
        differ.submitList(list)
    }

    override fun onBindViewHolder(holder: NavigationOptionViewHolder, position: Int) {
        val item = children[position]
        holder.itemDataBindingUtil.navigationItem = item
        holder.itemDataBindingUtil.clickEvent = interaction
        holder.itemDataBindingUtil.position = position

        val list: List<String> = listOf(*item.roomFacility.split(",").toTypedArray())
        if(list.isNotEmpty()){
            val buttonLayoutParams = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            )
            holder.itemDataBindingUtil.vContainer.removeAllViews()
            buttonLayoutParams.setMargins(5, 5, 5, 5)
            for (i in list.indices) {
                val tv = TextView(context)
                tv.text = list[i]
                tv.height = 80
                tv.textSize = 8.0f
                tv.gravity = Gravity.CENTER
                tv.setTextColor(Color.parseColor("#000000"))
                tv.background = context.resources.getDrawable(R.drawable.rounded_corner_yellow)
                tv.id = i + 1
                tv.layoutParams = buttonLayoutParams
                tv.tag = i
                tv.setPadding(10, 10, 10, 10)
                holder.itemDataBindingUtil.vContainer.addView(tv)
            }
        }

        holder.itemDataBindingUtil.mSelectRoom.setOnCheckedChangeListener(null)

        if(position == selectedItemPos) {
            holder.itemDataBindingUtil.mSelectRoom.isChecked = true
            holder.itemDataBindingUtil.container.setBackgroundResource(R.color.colorAccent)
        }
        else {
            holder.itemDataBindingUtil.mSelectRoom.isChecked = false
            holder.itemDataBindingUtil.container.setBackgroundResource(R.color.gray)
        }

        holder.itemDataBindingUtil.mSelectRoom.setOnCheckedChangeListener { _, _ ->
            selectedItemPos = position
            lastItemSelectedPos = if(lastItemSelectedPos == -1)
                selectedItemPos
            else {
                notifyItemChanged(lastItemSelectedPos)
                selectedItemPos
            }
            notifyItemChanged(selectedItemPos)
        }

    }
}
androholic
  • 686
  • 6
  • 23

0 Answers0