[英]How to get notify when first item of paged list recycler view changed
I'm using paging 3 to paginate my retrieved data.我正在使用分页 3 对检索到的数据进行分页。 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:现在用户使用调用此 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. MainActivity.mainUtils.newTicket
是一个在所需图标上显示徽章的界面。
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.我的意思是onBindedViewHolder
编写的代码只会在用户滚动页面时被调用。 how can I solve that?我该如何解决?
viewModel.tickets.observe(viewLifecycleOwner, {
//Here you get to know that there's update in your Datalist
adapter.submitData(it)
})
When you insert a new data into the database, observe the data inserted by returning the id of the data.当您向数据库中插入新数据时,通过返回数据的 id 来观察插入的数据。 For example if you are using Room, it can return a long value, which is the new rowId for the inserted item.例如,如果您使用 Room,它可以返回一个 long 值,即插入项的新 rowId。 Compare with existing ones and then you can show a badge if there's new data in the list.与现有数据进行比较,如果列表中有新数据,您可以显示徽章。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.