package com.casic.endoscope.adapter import android.annotation.SuppressLint import android.content.Context import android.graphics.Color import android.view.LayoutInflater import android.view.ViewGroup import android.widget.CheckBox import android.widget.LinearLayout import androidx.recyclerview.widget.RecyclerView import com.casic.endoscope.R import com.casic.endoscope.bean.CameraPointBean import com.pengxh.kt.lite.adapter.ViewHolder class CameraPointAdapter( context: Context, private val dataRows: MutableList<CameraPointBean> ) : RecyclerView.Adapter<ViewHolder>() { private var inflater: LayoutInflater = LayoutInflater.from(context) private var multipleSelected = mutableSetOf<Int>() private var selectedItems = ArrayList<CameraPointBean>() override fun getItemCount(): Int = dataRows.size override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { return ViewHolder(inflater.inflate(R.layout.item_point_list_rv, parent, false)) } @SuppressLint("NotifyDataSetChanged") fun setRefreshData(dataRows: MutableList<CameraPointBean>) { this.dataRows.clear() this.dataRows.addAll(dataRows) notifyDataSetChanged() } override fun onBindViewHolder(holder: ViewHolder, position: Int) { val item = dataRows[position] holder.setText(R.id.stepNameView, "步骤 ${item.id}") .setText(R.id.hAngleView, "水平角度 ${item.hAngle}") .setText(R.id.vAngleView, "垂直角度 ${item.vAngle}") val linearLayout = holder.itemView.findViewById<LinearLayout>(R.id.rootView) //item背景色 if (position % 2 == 0) { linearLayout.setBackgroundColor(Color.parseColor("#EEF1F6")) } else { linearLayout.setBackgroundColor(Color.parseColor("#FFFFFF")) } //多选 val multiCheckBox: CheckBox = holder.itemView.findViewById(R.id.multiCheckBox) multiCheckBox.isSelected = multipleSelected.contains(position) multiCheckBox.setOnClickListener { if (multipleSelected.contains(position)) { multipleSelected.remove(position) selectedItems.remove(dataRows[position]) holder.itemView.isSelected = false } else { multipleSelected.add(position) selectedItems.add(dataRows[position]) holder.itemView.isSelected = true } itemCheckedListener?.onItemChecked(selectedItems) } } private var itemCheckedListener: OnItemCheckedListener? = null interface OnItemCheckedListener { fun onItemChecked(items: ArrayList<CameraPointBean>) } fun setOnItemCheckedListener(listener: OnItemCheckedListener) { itemCheckedListener = listener } }