When using SwitchCompat in RecyclerView , if you add 3 entries to the list and change the state of SwitchCompat and add more entries to make scrolling then some SwitchCompat go to the initial state.
What to do with it I do not know, still new to Android.

Github

Adapter:

 class AlarmListAdapter(list: MutableList<AlarmItemListData>): RecyclerView.Adapter<AlarmListAdapter.AlarmViewHolder>(){ val list:MutableList<AlarmItemListData> = list val checkedL = AlarmCheckedListener(list) class AlarmViewHolder(val view: View?) : RecyclerView.ViewHolder(view) { val root = view?.rootView } override fun onBindViewHolder(holder: AlarmViewHolder?, position: Int) { var data = list[position] var days = data.days if(holder!=null) { whenAlarmActive(days, holder) holder.root?.time_start?.text = SimpleDateFormat("HH:mm").format(data.time) holder.root?.alarm_switch?.isChecked = data.isEnable holder.root?.id = data.id holder.root?.alarm_switch?.setOnCheckedChangeListener(checkedL) } } override fun getItemCount(): Int = list.size override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): AlarmViewHolder? { var view = LayoutInflater.from(parent?.context).inflate(R.layout.fragment_item_alarm,parent,false) var holder = AlarmViewHolder(view) return holder } private fun whenAlarmActive(days: BooleanArray, holder: AlarmViewHolder) { if(days.size !=7) return if(days[0] && days[1] && days[2] && days[3] && days[4] && !days[5] && !days[6]){ holder.root?.weekdays?.visibility = View.VISIBLE holder.root?.weekend?.visibility = View.GONE setDaysVisibility(holder,View.GONE) return } if(!days[0] && !days[1] && !days[2] && !days[3] && !days[4] && days[5] && days[6]){ holder.root?.weekdays?.visibility = View.GONE holder.root?.weekend?.visibility = View.VISIBLE setDaysVisibility(holder,View.GONE) return } days.forEachIndexed { i, b -> when(i){ 0 -> holder.root?.day1?.visibility = boolToIntVisibiblity(b) 1 -> holder.root?.day2?.visibility = boolToIntVisibiblity(b) 2 -> holder.root?.day3?.visibility = boolToIntVisibiblity(b) 3 -> holder.root?.day4?.visibility = boolToIntVisibiblity(b) 4 -> holder.root?.day5?.visibility = boolToIntVisibiblity(b) 5 -> holder.root?.day6?.visibility = boolToIntVisibiblity(b) 6 -> holder.root?.day7?.visibility = boolToIntVisibiblity(b) } } } private fun setDaysVisibility(holder: AlarmViewHolder, visibility:Int) { holder.root?.day1?.visibility = visibility holder.root?.day2?.visibility = visibility holder.root?.day3?.visibility = visibility holder.root?.day4?.visibility = visibility holder.root?.day5?.visibility = visibility holder.root?.day6?.visibility = visibility holder.root?.day7?.visibility = visibility } fun boolToIntVisibiblity(b:Boolean): Int = if(b) View.VISIBLE else View.GONE fun addAlarm(alarm:AlarmItemListData){ list.add(alarm) // sortAlarmList(list) notifyDataSetChanged() } private fun sortAlarmList(list:MutableList<AlarmItemListData>){ list.sortBy { it.time?.time } notifyDataSetChanged() } override fun onViewRecycled(holder: AlarmViewHolder?) { super.onViewRecycled(holder) } } 
  • Put the problem code here - Vladyslav Matviienko
  • At the end of the message there is a link to github - github.com/Bleser92/HeadsetAlarm - Bleser
  • one
    Very cool, but no one would rummage through the whole project for 10 lines of problem code. So please do it by the rules. - Vladyslav Matviienko
  • The solution to your problem is similar to this answer most likely - pavlofff
  • There is a suspicion that the problem is in val root = view?.rootView . rootView - it looks like it will be the recyclerView itself, not the item. try writing val root=view . And why are you starting to get acquainted with android with kotlin? There are substantially more responders and information in java than in kotlin. - Yura Ivanov

1 answer 1

I found it because it didn’t work, it was just magic)) When 3 elements were added and one of them had a state changed in the Switch (clicked on it) and the screen was completely blocked, the remaining switches either took the same state as the first switch [1] or or he became like the others [2]. Having spent a lot of time, I realized that in the model the isEnable variable changes somehow in it is set to true [2] or false [1] depending on the events, although this variable was private and the setter had a non-standard name. Replacing boolean with int and then converting back to boolean everything worked (he also has a crutch =)))