我正在尝试使用齐射要求从RecyclerView中删除一个项目。该请求工作正常,但问题是当我也尝试将其从列表中删除时。我阅读了同一主题的不同文章,但没有找到解决问题的解决方案。我认为是有关使用notifyItemRemoved的,但我不知道具体如何。任何建议,将不胜感激!
这是我的适配器类:
class BooksAdapter(var deleteClickListener: OnDeleteBtnClicked) : RecyclerView.Adapter<BooksAdapter.BooksViewHolder>() {
private var booksList: List<BookItem> = ArrayList()
class BooksViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val textviewBookTitle: TextView = itemView.textView_BookTitle
val textviewBookAuthor: TextView = itemView.textView_BookAuthor
val textviewBookPublisher: TextView = itemView.textView_BookPublisher
private val deleteBtn: Button = itemView.deleteBook_btn
fun delete(bookItem: BookItem, action: OnDeleteBtnClicked) {
deleteBtn.setOnClickListener {
action.onDeleteClick(bookItem, adapterPosition)
}
}
}
fun setData(list: List<BookItem>) {
this.booksList = list
notifyDataSetChanged()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BooksViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.book_item, parent, false)
return BooksViewHolder(itemView)
}
override fun onBindViewHolder(holder: BooksViewHolder, position: Int) {
val currentBook = booksList[position]
holder.textviewBookTitle.text = "Title: " + currentBook.bookTitle
holder.textviewBookAuthor.text = "Author: " + currentBook.bookAuthor
holder.textviewBookPublisher.text = "Publisher: " + currentBook.bookPublisher
holder.delete(booksList[position], deleteClickListener)
}
override fun getItemCount() = booksList.size
interface OnDeleteBtnClicked {
fun onDeleteClick(bookItem: BookItem, position: Int)
}
}
这是我的片段
class BooksFragment : Fragment(), BooksAdapter.OnDeleteBtnClicked {
lateinit var booksModel: BooksFragmentViewModel
var adapter: BooksAdapter = BooksAdapter(this)
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_books, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
booksModel = ViewModelProvider(requireActivity()).get(BooksFragmentViewModel::class.java)
booksLoading_progressBar.visibility = View.VISIBLE
var c: Context? = context
if (c != null) {
booksModel.setContext(c)
}
recyclerView_books.layoutManager = LinearLayoutManager(context)
booksModel.init()
booksModel.books.observe(viewLifecycleOwner, Observer {
if (it != null) {
adapter.setData(it)
recyclerView_books.adapter = adapter
hideProgressBar()
} else {
showErrorMessage()
hideProgressBar()
}
})
booksModel.deletedBook.observe(viewLifecycleOwner, Observer {
if (booksModel.triggerDeleteBook.value!!) {
if (it == null) {
booksModel.init()
booksModel.triggerDeleteBook.value = false
booksModel.bookItem = null
}
}
})
recyclerView_books.setHasFixedSize(true)
addBook_button.setOnClickListener {
var addBookFragment = AddBookFragment()
openAddNewBookFragment(addBookFragment)
}
super.onViewCreated(view, savedInstanceState)
}
private fun hideProgressBar() {
booksLoading_progressBar.visibility = View.INVISIBLE
}
private fun showErrorMessage() {
Toast.makeText(context, getString(R.string.getBooksErrorMessage), Toast.LENGTH_SHORT).show()
}
private fun openAddNewBookFragment(fragment: Fragment) {
parentFragmentManager.beginTransaction().apply {
replace(R.id.main_fragment, fragment).addToBackStack("addBookFrag")
commit()
}
}
override fun onDeleteClick(bookItem: BookItem, position: Int) {
booksModel.bookItem = bookItem
Log.i("MYTAGG", booksModel.bookItem.toString())
booksModel.triggerDeleteBook.value = true
}
}