Using Recyclerview with DiffUtil.ItemCallback doesn't scroll to top?

≡放荡痞女 提交于 2020-01-15 09:54:11

问题


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)
    })

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


回答1:


This is expected behaviour for DiffUtil. If you want to scroll to the top of your RecyclerView after the result have been updated, you can call layoutManager.scrollToPositionWithOffset(0, 0), or recyclerView.smoothScrollToPosition(0)




回答2:


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)

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



来源:https://stackoverflow.com/questions/57675926/using-recyclerview-with-diffutil-itemcallback-doesnt-scroll-to-top

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!