android-studio - Recyclerview Item对象在主类中设置函数
问题描述
我正在编写待办事项列表并使用 Groupie 适配器。我创建了一个 ViewHolder 类“TaskItem”。在该 ViewHolder 类中,我正在设置布局,在绑定功能上,我正在设置 Item 应显示的文本。此项目有两个按钮,出于多种原因,我想在将项目添加到适配器的主类中访问它们。我是如何找到解决方案的,但它是一个非常糟糕的解决方案:我使用了函数 adapter.setOnItemClickListener ,我在其中执行了所有其他功能,如其他点击列表器等。一切基本上都是这样工作的,但你必须先点击整个项目,直到项目中的按钮是可点击的。
我需要一个其他函数来解决这个问题,比如 adapter.bindViewHolder 但我不知道要传递哪些参数以及从哪里得到这些参数。
val tasksAdapter = GroupAdapter<ViewHolder>()
val recyclerViewTasks = findViewById<RecyclerView>(R.id.recyclerViewTasks)
recyclerViewTasks.adapter = tasksAdapter
tasksAdapter.setOnItemClickListener { item, view ->
val taskTextView = view.findViewById<TextView>(R.id.taskTextView)
val editTaskButton = view.findViewById<ImageButton>(R.id.buttonEditTask)
val taskCheckBox = view.findViewById<CheckBox>(R.id.taskCheckBox)
editTaskButton.setOnClickListener {
val editLayout = findViewById<View>(R.id.LayoutEdit)
editLayout.visibility = View.VISIBLE
val taskEditText = findViewById<EditText>(R.id.editTextTask)
val exitButton = findViewById<ImageButton>(R.id.buttonExitTask)
val deleteButton = findViewById<ImageButton>(R.id.buttonDeleteTask)
val confirmButton = findViewById<ImageButton>(R.id.buttonConfirmTask)
taskEditText.setText(taskTextView.text)
exitButton.setOnClickListener {
editLayout.visibility = View.GONE
taskEditText.setText(taskTextView.text)
}
deleteButton.setOnClickListener {
deleteTaskFromTasks(taskTextView.text.toString())
loadTasks()
editLayout.visibility = View.GONE
}
confirmButton.setOnClickListener {
deleteTaskFromTasks(taskTextView.text.toString())
addTaskToTasks(taskEditText.text.toString())
loadTasks()
editLayout.visibility = View.GONE
}
}
taskCheckBox.setOnClickListener{
addTaskToFinishedTask(taskTextView.text.toString())
deleteTaskFromTasks(taskTextView.text.toString())
loadTasks()
}
}
class TaskItem(val taskText: String): Item<ViewHolder>(){
override fun bind(viewHolder: ViewHolder, position: Int) {
val taskTextView = viewHolder.itemView.findViewById<TextView>(R.id.taskTextView)
val taskCheckBox = viewHolder.itemView.findViewById<CheckBox>(R.id.taskCheckBox)
taskTextView.text = taskText
taskCheckBox.isChecked = false
}
override fun getLayout(): Int {
return R.layout.task
}
}
解决方案
推荐阅读
- c++ - 使用枚举访问类型的 C++ 宏?
- python - AttributeError:模块 'google.cloud.storage' 没有属性 'open'
- python - 如何将构造函数属性初始化为空列表?
- python - 描述 scikit learn iris 数据库
- python - 如何使用 Python 从 BBC.com 下载视频?
- python - 用 f2py 编译基本的 fortran 模块
- java - 在类方法中删除视图
- python - 如何在 kivy/python 中更好地编写此代码
- json - 在模式推理解析 json 中检测到格式错误的记录
- azure-functions - 使用 Cosmos DB 表 API 查询日期时间范围