简体   繁体   English

如何在分页列表回收器视图的第一项更改时获得通知

[英]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.

相关问题 从回收商视图中删除物品时,如何通知onBindViewHolder? - How do I notify onBindViewHolder when an item is removed from the recycler view? 查找回收者视图中的第一个可见项目是否为列表中的第一项 - Find if the first visible item in the recycler view is the first item of the list or not 如何使用Espresso获取回收者视图列表项计数 - How to get recycler view list item count using Espresso 如何在 Kotlin 中更改焦点时在回收站视图中获取上次单击项目的位置 - How to get position of last clicked item in recycler view on focus changed in Kotlin 如何将项目添加到分页列表 - how to add item to paged list 如何从回收站视图中获取项目? - How to get item from Recycler View? “回收者”视图中的第一个可见项目将剩余列表 - First visible item in recycler view chagnes rest of the list 当回收器视图中的第一项不可见但在第一项可见时隐藏时,如何在滚动时显示我的 fab? - How do I show my fab on scroll when the first item in the recycler view is not visible but hide when the first item is visible? 如何找出第一次在回收站视图中创建的项目? - How to find out item is created in recycler view for the first time? 如何选择回收商视图中的项目并将项目位置获取到我的活动 - How a item in recycler view is selected and get the item position to my activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM