在我的Android应用程序中,我有2个片段(MainFragment和FavoriteFragment) 在FavoriteFragment中,当我单击按钮(从“收藏夹列表”中删除)notifyItemRemoved方法对我不起作用
ItemAdapter
holder.btn_maqal.setOnClickListener {
if (!db!!.searchInFavoriteTable(item)) {
holder.btn_maqal.setImageResource(R.drawable.ic_bookmark_black_full)
db!!.saveFavorite(item)
Snackbar.make(holder.itemView, "Added to favorite list", Toast.LENGTH_SHORT).show()
notifyDataSetChanged()
} else {
holder.btn_maqal.setImageResource(R.drawable.ic_bookmark_black)
db!!.deleteFromFavorite(item)
Snackbar.make(holder.itemView, "Deleted from favorite list", Toast.LENGTH_SHORT).show()
update(position)
}
}
更新方法
fun update(position: Int) {
notifyItemRemoved(position)
notifyDataSetChanged()
}