如何使用架构组件分页库停止在回收站视图上闪烁 [英] How to stop blinking on recycler view with architecture components paging library

查看:50
本文介绍了如何使用架构组件分页库停止在回收站视图上闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似于聊天的活动,其中我在使用带有PagedListAdaper的RecyclerView来加载一堆消息.我正在使用PositionalDataSource加载数据.它自身的加载工作正常,但是当我发送消息时,我使我的数据源无效,并且列表被重新制作.我的问题是这样做时会闪烁:

I have a chat-like activity where I am using a RecyclerView with a PagedListAdaper to load a bunch of messages. I'm using a PositionalDataSource to load the data. The loading it's self works fine but when I send a message, I invalidate my datasource and the list gets remade. My problem is that it blinks when it does that:

我尝试添加setHasStableIds(true)并覆盖getItemId,这将在一个简单的适配器上运行,但在这里似乎不起作用.我似乎也不能仅将一个项目添加到getCurrentList()中,因为它不受支持.另外,我不使用数据库,只是向服务器发出请求.

I have tried adding setHasStableIds(true) and overriding getItemId which would work on a simple adapter but it doesn't seem to work here. I also cannot seem to be able to just add an item to the getCurrentList() because it's not supported. Also, I'm not using a database, just making requests to a server.

所以我的问题是,除了使数据源无效之外,还有更好的方法吗?有没有一种方法可以阻止列表在发送消息时闪烁?还是这个图书馆不适合我的聊天活动?

So my questions are, is there a better way of doing this besides invalidating the data source? Is there a way to stop the list from blinking when sending a message? Or is this library just not suited for my chat activity?

修改:

我的差异回调

private val DIFF_CALLBACK: DiffCallback<MessageModel> = object : DiffCallback<MessageModel>() {
        override fun areItemsTheSame(@NonNull oldMessage: MessageModel, @NonNull newMessage: MessageModel) =
                oldMessage.id == newMessage.id


        override fun areContentsTheSame(@NonNull oldMessage: MessageModel, @NonNull newMessage: MessageModel) =
                oldMessage.equals(newMessage)
    }

Edit2我已修复:

因此,我设法通过使用PagedListAdapterHelper并在加载项目后设置其列表来对其进行修复:

So I managed to fix it by using PagedListAdapterHelper and setting it's list after the items loaded:

private var mHelper: PagedListAdapterHelper<MessageModel>? = null

init {
    mHelper = PagedListAdapterHelper(this, DIFF_CALLBACK)
    setHasStableIds(true)
}
fun setList(pagedList: PagedList<MessageModel>) {
    pagedList.addWeakCallback(pagedList.snapshot(), object:PagedList.Callback() {
        override fun onChanged(position: Int, count: Int) {

        }

        override fun onInserted(position: Int, count: Int) {
            mHelper?.setList(pagedList)
        }

        override fun onRemoved(position: Int, count: Int) {

        }

    })
}

推荐答案

简短答案:确保调用 PositionalDataSource< T> .loadInitial(@NonNull LoadInitialParams参数,@在 loadInitial 中同步NonNull LoadInitialCallback< T>回调),而无需将其包装在某种异步成功处理程序中.

Short answer: make sure to call the callback of PositionalDataSource<T>.loadInitial(@NonNull LoadInitialParams params, @NonNull LoadInitialCallback<T> callback) synchronously in loadInitial without wrapping it in some kind of asynchronous success handler.

说明:

闪烁可能是由初始负载中的异步负载引起的,即在 PositionalDataSource< T> .loadInitial(@NonNull LoadInitialParams params,@NonNull LoadInitialCallback< T>回调)中引起的.

Blinking can be caused by asynchronous loads in your initial load i.e. in PositionalDataSource<T>.loadInitial(@NonNull LoadInitialParams params, @NonNull LoadInitialCallback<T> callback).

这样做会发生以下情况:

The following will happen when you do so:

您的数据源无效,这将导致创建新的 PagedList (使用 LivePagedListBuilder 创建时).这个新创建的分页列表将传递到您的适配器,但是它仍然为空,因为您没有在初始加载时直接调用回调.只要调用您的回调,这将导致一个空列表.最终会导致闪烁效果.

Your data source gets invalidated which leads to the creation of a new PagedList (when created with LivePagedListBuilder). This newly created paged list will be passed to your adapter but it is still empty because you didn't call the callback directly in your initial load. This will lead to an empty list for as long as it takes for your callback to be called. This ultimately results in a flicker effect.

这篇关于如何使用架构组件分页库停止在回收站视图上闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆