How to scroll to a item when use paging library, My recycleView have many paging, i can search the item, and quick scroll to this item? how to do on paging
It depends on the data source you are using.
If you are using the positional data source (what room returns), you can calculate the position w/ a database query and call layoutManager.scrollToPositionWithOffset(position, 0). Make sure you have placeholders enabled.
If you are using keyed data sources, you'll need data to be loaded before you can scroll to there. You can use pagedList.loadAround(position) and when it is loaded, then call scroll to position.
I'm using room which returns PagedList
Kindly advise on how(what database query) I can get the position of a specific item that I want to scroll to.
Try the search row_number() analog sqlite in google. The query can't be provided in general, you should adjust it to your schema. I've managed to do what @yigit mentioned, but 1. performance probably could be not optimal 2. If a new item is far below the current position, the scroll is performed a few items before what I need
@naixx let me try that out now. Thanks
Need use LivePagedListBuilder.setInitialLoadKey load the position when first load scroll to postion,
Have any other function dynamic setInitialLoadKey when the RecyclerView already display?
Need use LivePagedListBuilder.setInitialLoadKey load the position when first load scroll to postion,
Have any other function dynamic setInitialLoadKey when the RecyclerView already display?
Have you got a solution for the above?
If you are using keyed data sources, you'll need data to be loaded before you can scroll to there. You can use
pagedList.loadAround(position)and when it is loaded, then call scroll to position.
@GowthamAshokkumar here
While the above works for Paging2 it's clear we needed to make this easier to implement. In particular, I wanted to add that you don't need to wait for the item to load, you can just scroll to a position if you have placeholders enabled and let paging either catch up or jump to that position depending on the type of DataSource and your config.
In Paging3 we've added .get(index) and .snapshot() methods which are synchronous with what's presented by PagingDataAdapter and specifically do not trigger loads like get / getItem does to make it easier to figure out where to scroll to and fast scrolling deep into placeholders are supported via jumping with PagingSource.getRefreshKey and PagingConfig.jumpThreshold (and this now works with any type of DataSource, not just positional).
Most helpful comment
While the above works for Paging2 it's clear we needed to make this easier to implement. In particular, I wanted to add that you don't need to wait for the item to load, you can just scroll to a position if you have placeholders enabled and let paging either catch up or jump to that position depending on the type of
DataSourceand your config.In Paging3 we've added
.get(index)and.snapshot()methods which are synchronous with what's presented byPagingDataAdapterand specifically do not trigger loads like get / getItem does to make it easier to figure out where to scroll to and fast scrolling deep into placeholders are supported via jumping withPagingSource.getRefreshKeyandPagingConfig.jumpThreshold(and this now works with any type of DataSource, not just positional).