Not the most elegant solution, but it is working. For example it is much better to use this approach when using Firestore pagination when deleting item instead of re fetching whole list....
Also it can be optimized, because this is just a draft. Main Idea is to use dummy ViewHolder with blank view.
class PostAdapter(
options: FirestorePagingOptions<Post>,
private val feedViewModel: FeedViewModel
) : FirestorePagingAdapter<Post, RecyclerView.ViewHolder>(options) {
companion object {
private const val DELETED_VIEW_TYPE = 1
private const val NORMAL_VIEW_TYPE = 2
}
private val deletedItems = arrayListOf<String>()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val inflater = parent.getLayoutInflater()
return when (viewType) {
NORMAL_VIEW_TYPE -> {
PostViewHolder(ItemPostBinding.inflate(inflater, parent, false))
}
else -> BindingViewHolder<ItemDeletedPostBinding>(
ItemDeletedPostBinding.inflate(inflater, parent, false)
)
}
}
override fun getItemViewType(position: Int): Int {
val item = getItem(position)?.toObject(Post::class.java)
if (deletedItems.firstOrNull { it == item?.postId }.isNotNull()) {
return DELETED_VIEW_TYPE
} else {
return NORMAL_VIEW_TYPE
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int, model: Post) {
if (holder is PostViewHolder) {
holder.bind(model, feedViewModel)
}
}
fun deleteItem(post: Post) {
val postId = post.postId
if (postId != null) {
val element = currentList?.find { it.toObject(Post::class.java)?.postId == postId }
val index = currentList?.indexOf(element)
index?.let {
deletedItems.add(postId)
notifyItemChanged(it)
}
}
}
}