android / architecture-components-samples

Samples for Android Architecture Components.
https://d.android.com/arch
Apache License 2.0
23.4k stars 8.29k forks source link

Paging Library scroll to some item #239

Closed crossle closed 4 years ago

crossle commented 6 years ago

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

yigit commented 6 years ago

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.

denno640 commented 6 years ago

I'm using room which returns PagedList livedata when I use DataSource. Factory<Integer,Item> Kindly advise on how(what database query) I can get the position of a specific item that I want to scroll to.

naixx commented 6 years ago

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

denno640 commented 6 years ago

@naixx let me try that out now. Thanks

crossle commented 6 years ago

Need use LivePagedListBuilder.setInitialLoadKey load the position when first load scroll to postion, Have any other function dynamic setInitialLoadKey when the RecyclerView already display?

GowthamAshokkumar commented 4 years ago

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?

crossle commented 4 years ago

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

dlam commented 4 years ago

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

davidmak2709 commented 1 year ago

@dlam is there any short example provided?

Let me explain. The PagingSource is a database (using Room) table, now on start the RV should scroll to the N-th element in the given list. Is it possible?