Android Jetpack: Paging

many apps work with large sets of data but only need to load and display a small portion at any given time if you're not careful you might request data you don't actually need wasting your users battery and bandwidth if the data you're displaying is constantly updating it can

be difficult to keep your UI in sync and still send only a small amount of information over the network the paging library part of just packed tackles these problems enabling you to load data gradually and gracefully the library supports both large but bounded lists as well as lists

of unbounded sites such as continuously updating feeds it offers integration with recyclerview which is typically used to display large data sets and place nicely with either live data or rx Java for observing new data in your UI the painting library is based on the idea of sending lists

to the UI with a live data of a list that is observed by the recyclerview adapter it then builds up on this idea by adding paging so you can load content gradually let's go over the main components of the library and see how they fit in your apps

architecture the core elements in the paging library are the page list and the data source a page list is a collection that loads data in chunks known as pages asynchronously a data source is the base class for loading snapshots of data into a given page list data sources

can be backed by the network database file orb anywhere you want to retrieve data from you create a data source using a data source Factory object the paging library also provides a page list adapter which helps you present data from page lists in a recycler view the pages

adapter is notified when pages are loaded and it uses the few tail to compute fine-grain updates as new data is received the paging library provides the live page list builder class for getting a live data object of type page list to create a live pages builder pass in

the data source Factory object and a paging configuration if you prefer working with our Java instead of live data then just use the page two this builder it's constructed similarly to life page list builder but instead of a live data object it will return an observable or flowable

depending on what you need let's take a look at some common scenarios uploading data from a database or a retrofit based network source and see how the paging library helps so first case let's say that the database is your data source the room persistence library provides native support

for paging library data sources for a given query room allows you to return any data source factory from the doll and handles the implementation of the data source for you second case let's say that the database is a cache for data loaded from network so here you would

still return a data source factory from the DAO but you would also need to implement another paging component a boundary call back the battery callback loads more data when the user gets near the end of the data that's in the local cache after the data is asserted the

paging library automatically updates the UI but don't forget to associate the boundary call back with a live page this builder you created earlier that way it can be used by the page list third case having only the network as your data source here you will have to create

both your data source and your data source factory but when choosing which data source type to extend consider what your back-end API looks like if you need to request data from your back-end based on a key you will extend from item keyed data source let's take an example

you might need to get the first 100 commits added to a github repository after a certain date then the date will be the key for your data source item kids data source allows you to define how to load the initial page as well as how to load items

both after and before a kid entry if you're back and expose is api's that work with pages then you will extend from page key to data source for example the search repositories github API returns paginated items in the github API request you need to specify the query which

page you want and optionally the number of items per page independent on how you create your network data source you will need to implement a data source factory that knows how to create your data source for full examples of how to implement all of these cases including how

to handle error cases or return mechanisms check out our samples on github okay let's sum it up here's what you'll need to do to integrate paging you'll need to define your data source create a boundary call back if it's needed create the live data of a page this

with the help of a live page list filter update your adapter to be a page this adapter and then finally observe the live data of a page list in your UI and set the page list to the adapter that's it just five steps check out our documentation code

samples and call up and start using the paging library to provide smooth performing lists for your users [Applause]

Hãy bình luận đầu tiên

Để lại một phản hồi

Thư điện tử của bạn sẽ không được hiện thị công khai.


*