将Recyclerview与DiffUtil.ItemCallback结合使用不会滚动到顶部吗? [英] Using Recyclerview with DiffUtil.ItemCallback doesn't scroll to top?

查看:788
本文介绍了将Recyclerview与DiffUtil.ItemCallback结合使用不会滚动到顶部吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次从服务器获取内容时,我都会搜索项目 当我搜索"o"时它起作用,然后"ot"它又起作用,由于"o"搜索,我假设有20个项目,如果出现"ot",则有10个项目 这是这20个项目的一部分,我确实在EditText中删除了t,它仅在中间显示了20个项目,其中recyclerview滚动指示器 这是我的diffUtil代码

I do search of items and every time I fetch from server When I search "o" it works and then "ot" it works again and as a result of "o" search I have let's say 20 items and in case "ot" I have 10 items which are part of that 20 items and I do remove t in EditText it shows 20 items only the recyclerview scroll indicator is in the middle Here is my Code for diffUtil

ListAdapter<Asset, SearchAdapter.ViewHolder>(object : DiffUtil.ItemCallback<Asset>() {

    override fun areItemsTheSame(oldItem: Asset, newItem: Asset): Boolean {
        return oldItem.assetId == newItem.assetId && oldItem.originalTitle ==newItem.originalTitle
    }

    override fun areContentsTheSame(oldItem: Asset, newItem: Asset): Boolean {
        return oldItem == newItem
    }

}




  viewModel.searchAssets.observe(viewLifecycleOwner, Observer {
      searchAdapter.submitList(it)
    })

在这里您可以看到我拍摄的视频 https://drive.google.com/drive/folders/1ia5y9TtL0PSAM1M4hVBILWcdSK=BDyKI共享

Here you can see the video I have filmed https://drive.google.com/drive/folders/1ia5y9TtL0PSAM1M4hVBILWcdSK1BDyKI?usp=sharing

推荐答案

我想回答自己的问题,因为我得到了Yiğit的答复.
submit list has another overload which receives a callback when list applied you can use that one and just call rv.scrollToPosition(0)

I want to answer my own question as I got an answer from Yiğit
submit list has another overload which receives a callback when list applied you can use that one and just call rv.scrollToPosition(0)

这是我的代码 searchAdapter.submitList(it) { viewBinding.searchList.scrollToPosition(0) }

here is my code searchAdapter.submitList(it) { viewBinding.searchList.scrollToPosition(0) }

这篇关于将Recyclerview与DiffUtil.ItemCallback结合使用不会滚动到顶部吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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