Paging An introduction to the Paging library

Paging page page is a new architecture library launched by Google combined with RecyclerView for Paging loading data. It is mainly to solve the problem of resource waste caused by loading a large amount of data at one time. By paging, one page of data is loaded at a time, which not only speeds up the rendering of the interface, but also reduces the consumption of creating resources such as objects. Specific can see the official website

The paging library consists of three main parts

  • DataSource: Defines a method for obtaining data. There are three methods

    1. PageKeyedDataSource 2. ItemKeyedDataSource 3. PositionalDataSource. Load data based on location information, in conjunction with Room database or local data source.Copy the code
  • PagedListAdapter: a page library adapter that inherits from RecyclerView and implements a DiffUtil.ItemCallback differentiator to analyze whether data has changed.

  • PagedList: Defines the configuration of the pagination library, which has the default load data size, pagination data size, etc. And through the PagedListAdapter data changes are updated.

The Room database is used for paging loading

(1) The creation of a DataSource

Room is Google’s official database framework: Room, which is an ORM framework. Details can be found here. Android Paging library for local DataSource (1) — using Room to generate a DataSource.Factory for local DataSource (2) — using Room to generate a DataSource

  1. “SELECT COUNT(*) FROM ( ” + mSourceQuery.getSql() + ” )”; Getting the size of the data source, which makes sense, is getting the total amount of data to load.
  2. “SELECT * FROM ( ” + mSourceQuery.getSql() + ” ) LIMIT ? OFFSET ?” ; This is how you query the data table. Limit is the number of queries per page, which corresponds to the size of the pages, and Offset is the Offset of the query, which corresponds to the start of each page. Since we are using Room to generate a PositionalDataSource, we will leave the PositionalDataSource to the local data load. Details about the stamp

(2) The implementation of PagedListAdapter

Because PagedListAdapter inherited from RecyclerView adapter, so it is not difficult to achieve, just need to provide a difference implementation used for data analysis, the code is as follows:

class ArticlePageAdapter : PagedListAdapter<ArticleEntity, ArticleViewHolder>(diffCallback) {
    override fun onBindViewHolder(holder: ArticleViewHolder, position: Int) {
        holder.bindTo(getItem(position))
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ArticleViewHolder =
            ArticleViewHolder(parent)

    companion object {
        private val diffCallback = object : DiffUtil.ItemCallback<ArticleEntity>() {
            override fun areItemsTheSame(oldItem: ArticleEntity, newItem: ArticleEntity): Boolean =
                    oldItem.id == newItem.id

            override fun areContentsTheSame(oldItem: ArticleEntity, newItem: ArticleEntity): Boolean =
                    oldItem == newItem
        }
    }
}
Copy the code

(3) Configure PagedList

PagedList sets the size of pages, initializes the size of data to be loaded, and so on.

 val pagedListConfig =PagedList.Config.Builder().setEnablePlaceholders(true).setPageSize(10).setInitialLoadSizeHint(20).build()
 val pagedList = LivePagedListBuilder(articleDao.getAllByDataSource(), pagedListConfig).build()
Copy the code

Generated from the above code is a PagedList with LiveData

(4) Summary

Configure the Room database, generate the DataSource responsible for the DataSource, then implement the PagedListAdapter responsible for UI rendering, and finally perform some PagedList configuration. Generate a PagedList with LiveData, and notify the pageAdapter to call submitList to update the UI once the data changes

recycle_article.layoutManager = LinearLayoutManager(this) val pageAdapter = ArticlePageAdapter() recycle_article.adapter  = pageAdapter val articleDao = dataBase.articleDao() val pagedListConfig = PagedList.Config.Builder().setEnablePlaceholders(true).setPageSize(10).setInitialLoadSizeHint(20).build()
   val postList = LivePagedListBuilder(articleDao.getAllByDataSource(), pagedListConfig).build()
   postList.observe(this, Observer { pageAdapter.submitList(it)})

Copy the code

The demo has been uploaded, click on the portal, if there is any doubt or error, welcome to point out.