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.
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) } }
val root = view?.rootView. rootView - it looks like it will be the recyclerView itself, not the item. try writingval 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