问题:单击一个复选框仅会删除位于回收站视图位置[0]的第一个项目(卡片视图)。如果我单击另一个具有不同位置的复选框,则该项目不会被删除。
我尝试的方法:我创建了一个界面来监听click事件处理程序。我得到了适配器的位置,并将其传递给我的点击监听器。我还尝试使用Lamba,并尝试在适配器构造函数内部传递click监听器。*
我希望发生的事情:单击复选框后,具有该位置的项目(卡片视图)将被删除。
适配器类
class ListAdapter(
private val callback: AdapterCallBack
) :
RecyclerView.Adapter<ListAdapter.MyViewHolder>() {
private var todoList: List<TodoItem> = ArrayList()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val context = parent.context
val view = LayoutInflater.from(context).inflate(
R.layout.item_list, parent, false
)
return MyViewHolder(view)
}
override fun getItemCount(): Int {
return todoList.size
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val currentItem = todoList[position]
holder.itemView.tvTitle.text = currentItem.title
holder.itemView.dueDate.text = currentItem.dueDate
holder.bind(currentItem)
}
fun setList(todo: List<TodoItem>) {
this.todoList = todo
notifyDataSetChanged()
}
inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bind(todoItem: TodoItem) {
val position = adapterPosition
itemView.setOnClickListener() {
callback.onItemClick(todoItem, position)
}
itemView.doneCheckBox.setOnClickListener() {
callback.onCheckBoxClick(todoItem, position)
}
}
}
}
界面
interface AdapterCallBack {
fun onCheckBoxClick(todoItem: TodoItem, position: Int)
fun onItemClick(todoItem: TodoItem, position: Int)
}
MainFragment(实现接口)
class MainFragment : Fragment(), AdapterCallBack {
private lateinit var todoViewModel: TodoViewModel
private lateinit var adapter : ListAdapter
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_main, container, false)
val recyclerView = view.recyclerview
// checkDone = view.findViewById<CheckBox>(R.id.doneCheckBox)
adapter =
ListAdapter(this)
recyclerView.adapter = adapter
recyclerView.layoutManager = LinearLayoutManager(requireContext())
todoViewModel = ViewModelProvider(this).get(TodoViewModel::class.java)
todoViewModel.getAllTodoItems.observe(viewLifecycleOwner, Observer {
adapter.setList(it)
Log.i("list", "$it")
})
view.fabBtn.setOnClickListener {
findNavController().navigate(R.id.action_mainFragment_to_detailFragment)
(activity as AppCompatActivity?)!!.supportActionBar!!.title = "Create"
Log.i("fab_btn", "Fab button is pressed")
}
return view
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.delete_all, menu)
super.onCreateOptionsMenu(menu, inflater)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.delete_all_items -> {
todoViewModel.deleteAllItems()
Toast.makeText(requireContext(), "All items deleted", Toast.LENGTH_SHORT).show()
return true
}
}
return super.onOptionsItemSelected(item)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
}
override fun onCheckBoxClick(todoItem: TodoItem, position: Int) {
if (doneCheckBox.isChecked) {
todoViewModel.deleteItem(todoItem)
Toast.makeText(
requireContext(),
"Item deleted at position $position is click!",
Toast.LENGTH_SHORT
).show()
Log.i("check_click", "$todoItem is deleted at position $position")
}
}
override fun onItemClick(todoItem: TodoItem, position: Int) {
val action = MainFragmentDirections.actionMainFragmentToDetailFragment(todoItem)
findNavController().navigate(action)
Log.i("cardview_click", "Card view item $todoItem is click at position $position")
}
}
ViewModel类
class TodoViewModel(application: Application) : AndroidViewModel(application) {
val getAllTodoItems: LiveData<List<TodoItem>>
private val repository: TodoRepository
private val selectedDate: MutableLiveData<String> = MutableLiveData()
private val selectedTime: MutableLiveData<String> = MutableLiveData()
val date: LiveData<String>
get() = selectedDate
val time: LiveData<String>
get() = selectedTime
init {
val todoDao = TodoDatabase.getDatabase(application).todoDao
repository = TodoRepository(dao = todoDao)
getAllTodoItems = repository.todoItems
}
fun insertItem(todoItem: TodoItem) {
viewModelScope.launch(Dispatchers.IO) {
repository.insert(todoItem)
}
}
fun updateItem(todoItem: TodoItem) {
viewModelScope.launch(Dispatchers.IO) {
repository.update(todoItem)
}
}
fun deleteItem(todoItem: TodoItem) {
viewModelScope.launch(Dispatchers.IO) {
repository.delete(todoItem)
}
}
fun deleteAllItems() {
viewModelScope.launch(Dispatchers.IO) {
repository.deleteAll()
}
}
fun setDateCalenderValue(date: String) {
selectedDate.value = date
}
fun setTimePickerValue(time: String) {
selectedTime.value = time
}
}
答案 0 :(得分:0)
您需要通知适配器哪一行已被删除
将您的onCheckBoxClick
函数实现更改为此
override fun onCheckBoxClick(todoItem: TodoItem, position: Int) {
if (doneCheckBox.isChecked) {
todoViewModel.deleteItem(todoItem)
adapter.notifyItemRemoved(position) // Add this line
Toast.makeText(
requireContext(),
"Item deleted at position $position is click!",
Toast.LENGTH_SHORT
).show()
Log.i("check_click", "$todoItem is deleted at position $position")
}
}