I'm using paging 3 to paginate my retrieved data. I want to show a badge on the bottom navigation bar when a new item has been added to the recycler view. imagine the user is watching recycler view in the situation below:
item A
item B
item C
now the user refresh the page using a swipe refreshing button which calls this function:
binding.refresh.setOnRefreshListener {
adapter.refresh()
}
and a new item is added, so the situation would be like this:
item D
item A
item B
item C
the new data would be propagated using this line of code:
viewModel.tickets.observe(viewLifecycleOwner, {
viewLifecycleOwner.lifecycleScope.launch {
adapter.submitData(it)
}
})
I want to show a badge at this moment. so I added an interface into my recycler paged list adapter like this:
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
ticketUtils.hasNewItem(getItem(0)?.ticket_id)
holder.bind(getItem(position))
}
which checks what is the first item of the recycler view. so I have implemented the body of this interface inside of my fragment like this:
override fun hasNewItem(itemID: String?) {
if (itemID.isNullOrEmpty())
return
if (itemID != firstTicketID)
MainActivity.mainUtils.newTicket(showBadge = true)
}
and MainActivity.mainUtils.newTicket
is an interface that shows a badge on the desired icon.
but I have faced to the problem that it seems the recycler view won't be notified for new data till the user start scrolling. I mean the code written in onBindedViewHolder
would be called just when the user scrolls the page.
how can I solve that?