Firebase를 이용해서 데이터를 읽어와서 리사이클러뷰에 연동하는것까지는 완성이 되었는데 그 이후 리사이클러뷰 안에서 체크박스 체크시 리사이클러뷰가 재사용되면서 체크가 안되있던 아이템의 체크박스도 체크가 되어있는채로 나오는데 어떻게 해결하면 될까요??
아이템.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="date"
type="com.example.calendar1.Date" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="86dp">
<TextView
android:id="@+id/age_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="276dp"
android:layout_marginBottom="24dp"
android:text="@{String.valueOf(date.calendar_year)}"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="44dp"
android:layout_marginBottom="20dp"
android:fontFamily="@font/nexon_medium"
android:text="@{date.name}"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
데이터클래스
data class Date(
val name: String,
val calendar_year: Int
)
리사이클러뷰 연동 adapter 및 뷰홀더.kt
class list_test : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_list_test)
//임시 Date 배열로 저장
val example = arrayListOf<Date>()
example.clear()
val database : FirebaseDatabase = FirebaseDatabase.getInstance()
for( i in 1..20){
val myRef: DatabaseReference = database.getReference("List/list$i")
myRef.addValueEventListener(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
val value = dataSnapshot?.value
example.add(Date("$value", 2022))
recycler_view.adapter?.notifyDataSetChanged()
recycler_view.adapter = DateAdapter(example.sortedBy { it.name }) { date ->
}
}
override fun onCancelled(p0: DatabaseError) {
println("Failed to read value.")
}
})
}
recycler_view.apply {
layoutManager = LinearLayoutManager(this@list_test)
adapter = DateAdapter(example){ date ->
//확인용 Toast
Toast.makeText(this@list_test, "$date", Toast.LENGTH_SHORT).show()
}
}
}
}
//Adapter 설정
class DateAdapter(val items: List<Date>, private val clickListener: (date: Date) -> Unit) :
RecyclerView.Adapter<DateAdapter.DateViewHolder>(){
//ViewHolder 설정
class DateViewHolder(var binding: ItemDateBinding): RecyclerView.ViewHolder(binding.root)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DateViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.item_date, parent, false)
val viewHolder = DateViewHolder(ItemDateBinding.bind(view))
view.setOnClickListener {
clickListener.invoke(items[viewHolder.adapterPosition])
}
return viewHolder
}
override fun onBindViewHolder(holder: DateViewHolder, position: Int) {
holder.binding.date = items[position]
}
override fun getItemCount(): Int = items.size
}