Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
133 views
in Technique[技术] by (71.8m points)

android - How to clear highlighted items in the recyclerview?

I have implemented the recycler view multiple item selection by changing the background color of item when selected.When i remove those items from the model, items get removed but the highlighting does not go away with the item removed, instead it is applied on the remaining items.

I have tried to remove the items from the model and notify the adapter on the each removal like below

  for (item in selectedItems) {
                        deleteResidentItem(item.key, sharedPreferences.getString("AuthToken", SHARED_PREFERENCE_DOES_NOT_EXIST))
                        removeSelectedItemsFromModelById(item.key)
                        residentRecyclerViewAdapter.notifyItemRemoved(item.value)
                        residentRecyclerViewAdapter.notifyItemRangeChanged(item.value, mutableResidentList.size)
                    }

even after removing the item the highlighted items still appear on the list randomly, like the example below

selected items After removal

i want the highlighted views to be gone after removing from model, how to solve this problem?

Adapter implementation code

class ResidentRecyclerViewAdapter( val residentItems: List<ResidentListModel>):RecyclerView.Adapter<ResidentRecyclerViewAdapter.ResidentViewHolder>() {



    lateinit var residentRecyclerViewListenerInterface:ResidentRecyclerViewListenerInterface

    fun initResidentRecyclerViewListenerInterface(listener:ResidentRecyclerViewListenerInterface) {
        residentRecyclerViewListenerInterface = listener
    }

    override fun onCreateViewHolder(parent: ViewGroup, p1: Int): ResidentViewHolder {
        var layoutInflater = LayoutInflater.from(parent.context)
        var inflatedLayout = layoutInflater.inflate(R.layout.single_resident_item, parent, false)
        return ResidentViewHolder(inflatedLayout, residentRecyclerViewListenerInterface)
    }

    override fun getItemCount() = residentItems.size
    override fun onBindViewHolder(holder: ResidentViewHolder, posistion:Int) {
        //place to bind the values
        val ModelName = residentItems.get(posistion).ModelName
        val VechicleNo = residentItems.get(posistion).VehicleNo
        val SlNo = residentItems.get(posistion).SlNo
        holder.vehicle_number.text = VechicleNo
        holder.model_name.text = ModelName
        holder.sl_no.text = SlNo.toString()

        holder.resident_item_wrapper.setOnLongClickListener {

            when (holder.is_selected.isChecked) {
                // not checked, then check, add to list
                false-> {
                    holder.resident_item_wrapper.setBackgroundColor(Color.parseColor("#1565c0"))
                    holder.resident_vehicle_number.setTextColor(Color.WHITE)
                    holder.resident_model_name.setTextColor(Color.WHITE)
                    holder.is_selected.setBackgroundColor(Color.WHITE)
                    holder.is_selected.visibility = View.VISIBLE
                    holder.is_selected.isChecked = true
                    residentRecyclerViewListenerInterface.onResidentItemLongClickListener(holder.sl_no.text.toString().toInt(), posistion)

                }

                true -> {
                    // checked then unselect and remove from list
                    holder.resident_item_wrapper.setBackgroundColor(Color.WHITE)
                    holder.resident_vehicle_number.setTextColor(Color.BLACK)
                    holder.resident_model_name.setTextColor(Color.BLACK)
                    holder.is_selected.setBackgroundColor(Color.BLACK)
                    holder.is_selected.visibility = View.GONE
                    holder.is_selected.isChecked = false
                    residentRecyclerViewListenerInterface.onResidentItemLongUnselectClickListener(holder.sl_no.text.toString().toInt(),posistion)

                }
            }
            true
        }
    }
    interface ResidentRecyclerViewListenerInterface {
        fun onResidentItemLongClickListener(Id:Int, Position:Int)
        fun onResidentItemClickListener(Id:Int, Position: Int)
        fun onResidentItemLongUnselectClickListener(Id: Int, Position: Int)
    }

    class ResidentViewHolder(val view: View, residentRecyclerViewListenerInterface:ResidentRecyclerViewListenerInterface):RecyclerView.ViewHolder(view) {
        var vehicle_number:TextView
        var model_name:TextView
        var sl_no:TextView
        var resident_item_wrapper:CardView
        var is_selected:CheckBox
        var resident_vehicle_number:TextView
        var resident_model_name:TextView
        lateinit var residentRecyclerViewListenerInterface:ResidentRecyclerViewListenerInterface

        init {
            vehicle_number = view.resident_vehicle_number
            model_name = view.resident_model_name
            sl_no = view.resident_slno
            resident_item_wrapper = view.resident_item_wrapper
            is_selected = view.is_selected
            resident_vehicle_number = view.resident_vehicle_number
            resident_model_name = view.resident_model_name

        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your problem is in when (holder.is_selected.isChecked) {

You should have the information if an item is checked on the ViewModel, not on the View and most definitely not in the ViewHolder.

It should be something like if(residentItems.get(posistion).isSelected){ (Using when is overkill for binary cases)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...