Item Touch Helper 的空列表不包含索引 0 处的元素

Empty list doesn't contain element at index 0 for Item Touch Helper

我在使用 ItemTouchHelper 和我的 recyclerview 时遇到了两个问题。我在 Main Activity.

中的不同片段中有两个回收器视图
  1. 我的第一个问题是 Item touch helper 不会滑动或无法用于我的第二个回收器视图,即使它们几乎相同。

  2. 每当我滑动删除时,我都会得到 Empty list doesn't contain element at index 0。我有点理解为什么,但我看到其他人使用相同的方法,所以我不明白如何解决。

片段1(可滑动):

    package com.example.bubblereminder

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.bubblereminder.room.RemindersListAdapter
import com.example.bubblereminder.room.RemindersViewModel
import kotlinx.android.synthetic.main.fragment_scheduled.*

class ScheduledFragment : Fragment() {
    private lateinit var reminderViewModel: RemindersViewModel

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        val v = inflater.inflate(R.layout.fragment_scheduled, container, false)

        return v
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        val recyclerView = view?.findViewById<RecyclerView>(R.id.recycler_scheduled)
        val adapter = context?.let { RemindersListAdapter(it) }
        if (recyclerView != null) {
            recyclerView.adapter = adapter
        }
        if (recyclerView != null) {
            recyclerView.layoutManager = LinearLayoutManager(context)
        }

        this.reminderViewModel = ViewModelProvider(this).get(RemindersViewModel::class.java)
        reminderViewModel.allRemindersDue.observe(viewLifecycleOwner, Observer { reminders ->
            // Update the cached copy of the words in the adapter.
            reminders?.let {
                if (adapter != null) {
                    adapter.setReminders(it)
                }
            }
        })

        ItemTouchHelper(itemTouchHelperCallback).attachToRecyclerView(recycler_scheduled)
    }

    private val itemTouchHelperCallback = object : ItemTouchHelper.SimpleCallback(0,
        ItemTouchHelper.LEFT)
    {
        override fun onMove(
            recyclerView: RecyclerView,
            viewHolder: RecyclerView.ViewHolder,
            target: RecyclerView.ViewHolder
        ): Boolean {
            TODO("Not yet implemented")
        }

        override fun onSwiped(viewHolder: RecyclerView.ViewHolder, position: Int) {
            reminderViewModel.delete(RemindersListAdapter(activity as MainActivity).getReminderAt(viewHolder.adapterPosition))
        }

    }
}

片段2(不能刷):

    package com.example.bubblereminder

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.bubblereminder.room.RemindersListAdapter
import com.example.bubblereminder.room.RemindersViewModel
import kotlinx.android.synthetic.main.fragment_done.*

class DoneFragment : Fragment() {
    private lateinit var reminderViewModel: RemindersViewModel

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        val v = inflater.inflate(R.layout.fragment_scheduled, container, false)

        return v
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        val recyclerView = view?.findViewById<RecyclerView>(R.id.recycler_scheduled)
        val adapter = context?.let { RemindersListAdapter(it) }
        if (recyclerView != null) {
            recyclerView.adapter = adapter
        }
        if (recyclerView != null) {
            recyclerView.layoutManager = LinearLayoutManager(context)
        }

        this.reminderViewModel = ViewModelProvider(this).get(RemindersViewModel::class.java)
        reminderViewModel.allRemindersDone.observe(viewLifecycleOwner, Observer { reminders ->
            // Update the cached copy of the words in the adapter.
            reminders?.let {
                if (adapter != null) {
                    adapter.setReminders(it)
                }
            }
        })

        ItemTouchHelper(itemTouchHelperCallback).attachToRecyclerView(recycler_done)
    }

    private val itemTouchHelperCallback = object : ItemTouchHelper.SimpleCallback(0,
        ItemTouchHelper.LEFT)
    {
        override fun onMove(
            recyclerView: RecyclerView,
            viewHolder: RecyclerView.ViewHolder,
            target: RecyclerView.ViewHolder
        ): Boolean {
            TODO("Not yet implemented")
        }

        override fun onSwiped(viewHolder: RecyclerView.ViewHolder, position: Int) {
            reminderViewModel.delete(RemindersListAdapter(activity as MainActivity).getReminderAt(viewHolder.adapterPosition))
        }

    }
}

我的适配器:

    package com.example.bubblereminder.room

import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.lifecycle.LiveData
import androidx.recyclerview.widget.RecyclerView
import com.example.bubblereminder.R
import java.util.*

class RemindersListAdapter internal constructor(
    context: Context
) : RecyclerView.Adapter<RemindersListAdapter.RemindersViewHolder>() {

    private val inflater: LayoutInflater = LayoutInflater.from(context)
    private var reminders = emptyList<Reminders>() // Cached copy of words


        inner class RemindersViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val remindersItemView: TextView = itemView.findViewById(R.id.recycler_item_text)
        val remindersImageView: ImageView = itemView.findViewById(R.id.recycler_item_image)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RemindersViewHolder {

        val itemView = inflater.inflate(R.layout.layout_recycler_item, parent, false)
        return RemindersViewHolder(itemView)
    }

    override fun onBindViewHolder(holder: RemindersViewHolder, position: Int) {
        val current = reminders[position]
        holder.remindersItemView.text = current.reminderText
        if (holder.remindersItemView.getText().toString().contains(taskOne)) {
            holder.remindersItemView.setTextColor(Color.parseColor("#FF00B59C"))
            holder.remindersImageView.setImageResource(R.drawable.ic_washing_machine)
        }
        if (holder.remindersItemView.getText().toString().contains(taskTwo)) {
            holder.remindersItemView.setTextColor(Color.parseColor("#EA5455"))
            holder.remindersImageView.setImageResource(R.drawable.ic_iron)
        }
        if (holder.remindersItemView.getText().toString().contains(taskThree)) {
            holder.remindersImageView.setImageResource(R.drawable.ic_shirt)
            holder.remindersItemView.setTextColor(Color.parseColor("#FFFD5900"))
        }
        if (holder.remindersItemView.getText().toString().contains(taskFour)) {
            holder.remindersImageView.setImageResource(R.drawable.ic_dumbbell)
            holder.remindersItemView.setTextColor(Color.parseColor("#8C1BAB"))
        }
        if (holder.remindersItemView.getText().toString().contains(taskFive)) {
            holder.remindersImageView.setImageResource(R.drawable.ic_bathtub)
            holder.remindersItemView.setTextColor(Color.parseColor("#0396FF"))
        }
    }

    override fun getItemCount() = reminders.size

    internal fun setReminders(reminders: List<Reminders>) {
        this.reminders = reminders
        notifyDataSetChanged()
    }

    private val taskOne = "Camasirlar Yikandi"
    private val taskTwo = "Kiyafetler Utulendi"
    private val taskThree = "Kiyafetler Duruldu"
    private val taskFour = "Egzersiz Yapildi"
    private val taskFive = "Banyo Yapildi"

    fun getReminderAt(position: Int) : Reminders {
       return reminders[position]
    }


}

谢谢你的帮助。

我为第二个片段夸大了错误的视图,这就是为什么它似乎不起作用的原因。

关于尺码问题;由于某种原因更换适配器:

return this.reminders = reminders

return reminders = reminders

并在片段中进行更改:

reminderViewModel.delete(RemindersListAdapter(activity as MainActivity).getReminderAt(viewHolder.adapterPosition))

val position = viewHolder.adapterPosition val myReminder: Reminders = adapter!!.getReminderAt(position) reminderViewModel.delete(myReminder)

现在工作得很好。