8

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

3 Answers 3

6

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)
}
Sign up to request clarification or add additional context in comments.

Comments

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)

5 Comments

where should I call layoutManager.scrollToPositionWithOffset(0, 0) ?
After your recyclerview's adapter has been updated (i.e. notifyDataSetChanged()), or after you've called dispatchUpdatesTo(...) on your DiffResult
I don't call notifyDataSetChanged() and I have submitList() in adapter and DiffUtil.ItemCallback<>() and my Adapter extends ListAdapter<Asset, SearchAdapter.ViewHolder>(diffCallback)
I'm not familiar with this SearchAdapter, is it part of a library? Try calling scrollTo... after you've called submitList(...)?
SearchAdapter is my adapter
0

You can just wait until a list view updated then scroll:

listAdapter.update(data) // update() method proceed data with DiffUtil
binding.listView.post {
    binding.listView.scrollToPosition(0)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.