• PagedListAdapter • ViewModel has a LiveData<PagedList> • Highly flexible • Automatic with Room database • Custom DataSource: Network, Files, or any other source • Also works with RxJava • Uses Observable<PagedList> • Uses Flowable<PagedList> • Skips LiveData
sources of data • Save / Transform / Combine data Sample projects: https://codelabs.developers.google.com/codelabs/android-paging/index.html https://github.com/googlesamples/android-architecture-components/tree/master/PagingWithNetworkSample
ViewHolder • Constructor needs data entity and DiffUtil comparator class ReposAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() … class ReposAdapter : PagedListAdapter<RepoEntity, RecyclerView.ViewHolder>(REPO_COMPARATOR) • PagedList • Extends AbstractList<E> • Basically, ArrayList with extra features
Custom DataSources possible. Three different types: • PageKeyedDataSource • Backend sends “Next” and “Previous” links • Or, count items, and send “offset” with next request • ItemKeyedDataSource • Send “After” or “Before” to backend, with Item ID • PositionalDataSource • Entire size of dataset is known • Enables “Placeholders”, and rapid scrolling to arbitrary positions in the dataset • (Room database uses this under the hood) • TouchTunes tried Room • Not enough time to implement Room for full data model • Went with PageKeyedDataSource
= OnConflictStrategy.REPLACE) fun insert(posts: List<RepoEntity>) @Query("SELECT * FROM repos WHERE (name LIKE :queryString) // fun reposByName(queryString: String): LiveData<List<RepoEntity>> fun reposByName(queryString: String): DataSource.Factory<Int, RepoEntity> }
implement create method: public abstract static class Factory<Key, Value> { public abstract DataSource<Key, Value> create(); • BoundaryCallback • Only for Room DB, must implement 3 methods: public abstract static class BoundaryCallback<T> { public void onZeroItemsLoaded() {} public void onItemAtFrontLoaded(@NonNull T itemAtFront) {} public void onItemAtEndLoaded(@NonNull T itemAtEnd) {}