I've got a recyclerView with checkBoxes and I want to select all of them at once by pressing a button
I've try this so far...
This is the Adapter -
class AdapterNfs (context: MainActivity, private val listener: OnItemClickListener) : RecyclerView.Adapter<AdapterNfs.NfViewHolder>() {
private val context: Context
private var list = emptyList<ReceiverPayment>()
private var status: Boolean = false
init {
this.context = context
}
fun setAllChecked(isChecked: Boolean){
status = isChecked
Log.d("***log", status.toString())
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NfViewHolder {
val binding = ItemNotasFiscaisBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return NfViewHolder(binding)
}
override fun onBindViewHolder(holder: NfViewHolder, position: Int){
holder.binding.apply {
textNFnumber.text = list[position].id
textNFvalue.text = list[position].totalValue
nfStatus.text = list[position].paid.toString()
}
holder.binding.checkBoxNf.isChecked = status
}
override fun getItemCount() = list.size
inner class NfViewHolder(var binding: ItemNotasFiscaisBinding) : RecyclerView.ViewHolder(binding.root), View.OnClickListener{
init{
binding.checkBoxNf.setOnClickListener(this)
}
override fun onClick(view: View?) {
val checkbox = view as CheckBox
val position : Int = adapterPosition
val data = list
if (position != RecyclerView.NO_POSITION) {
listener.onItemClick(position, checkbox.isChecked, data)
}
}
}
fun setData(newList: List<ReceiverPayment>) {
val nfDiffUtil = DiffUtilGeneric(list, newList)
val nfResult = DiffUtil.calculateDiff(nfDiffUtil)
this.list = newList
nfResult.dispatchUpdatesTo(this)
}
interface OnItemClickListener {
fun onItemClick(
position: Int,
checked: Boolean,
data: List<ReceiverPayment>
)
}
}
and this is the Main -
class MainActivity() : AppCompatActivity(), AdapterNfs.OnItemClickListener {
private val myAdapter = AdapterNfs(this, this)
private lateinit var binding : ActivityMainBinding
private lateinit var mainViewModel: MainViewModel
private lateinit var recyclerView: RecyclerView
private var valueTotal = 00.00
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
mainViewModel = MainViewModel()
setupRecyclerView()
getAllNfs()
setContentView(binding.root)
binding.selectAllCheckBoxes.setOnClickListener {
setAllCheckBoxes(true)
}
}
override fun onItemClick(position: Int, checked: Boolean, data: List<ReceiverPayment>) {
val totalValueSelected = binding.totalValue
val nfSelected = data[position].totalValue.toString().toDouble()
if (checked) {
valueTotal = nfSelected
totalValueSelected.text = valueTotal.toString()
} else {
valueTotal -= nfSelected
totalValueSelected.text = valueTotal.toString()
}
}
private fun setupRecyclerView(){
recyclerView = binding.recyclerViewNfs
binding.recyclerViewNfs.layoutManager = LinearLayoutManager(this)
binding.recyclerViewNfs.itemAnimator = DefaultItemAnimator()
binding.recyclerViewNfs.addItemDecoration(
DividerItemDecoration(
this,
LinearLayoutManager.VERTICAL
)
)
binding.recyclerViewNfs.adapter = myAdapter
}
private fun getAllNfs(){
mainViewModel.allNfs.observe(this@MainActivity){response ->
response.let {
myAdapter.setData(it[0].receiverPayments as List<ReceiverPayment>)
}
}
}
private fun setAllCheckBoxes(isChecked: Boolean){
myAdapter.setAllChecked(isChecked)
}
}
CodePudding user response:
In your adapter , call notifyDataSetChanged() after change the status
fun setAllChecked(isChecked: Boolean){
status = isChecked
Log.d("***log", status.toString())
notifyDataSetChanged()
}