Super simple and easy to use using Paging3 packaging RecyclerView Adapter

Advantages of this library:

  • The use of Paging3 encapsulation, with paging3 full advantage, quickly achieve list paging, seamless loading;
  • Entries can be added, deleted or modified to remedy the problem that paging cannot modify data;
  • There is no need to implement database cache of Paging, and network request can be made directly.
  • Multi-type entries are decoupled. New entries do not need to modify the original code at all, but directly create a new holder.
  • You no longer need to implement adapter yourself;
  • All user interface operations are performed by helper chain operations, no longer binding viewId;

Use steps:

Mode of dependence:

  1. Add the JitPack repository to the project root build.gradle
allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
	}
Copy the code
  1. Dependencies: added in the build.gradle module
Dependencies {implementation 'com. Making. Jarryleo: PagingAdapter: 1.0.2'}Copy the code

Code use:

1. The data model bean class implements the interface DifferData

Interface DifferData {fun areItemsTheSame(d: DifferData); // For data comparison, use the RecyclerView item animation instead of implementing interface DifferData {fun areItemsTheSame(d: DifferData): Boolean fun areContentsTheSame(d: DifferData): Boolean fun getChangePayload(d: DifferData): Any? }Copy the code

2. Implement the entry Holder: inherits SimpleHolder

class NewsHolder : SimpleHolder<NewsBean.StoriesBean>() { override fun getLayoutRes(): Int { return R.layout.item_news } override fun bindData( helper: PagingDataAdapterKtx.ItemHelper, data: NewsBean.StoriesBean? , payloads: MutableList<Any>? ) { if (data == null) return helper.setText(R.id.tv_title, data.title) .findViewById<ImageView>(R.id.iv_cover) .loadImage(data.images? .get(0) ? : "", corners = 6.dp) } }Copy the code

3. Set the Adapter

 recyclerView.adapter = SimplePagingAdapter(NewsHolder())
 
Copy the code

Multiple entry types implement multiple holders, passed directly to the SimplePagingAdapter construct, which supports variable parameters

4. Set data for the Adapter

Val pager = SimplePager<Long, DifferData>(viewModelScope) {val date = it. Key? : InitialKey try {/ / get the data from the network val data = API. The getNews (date). Await () / / return data PagingSource. LoadResult. Page (data. Stories, null, data.date? .toLongOrNull()) } catch (e: Exception) {/ / request failed PagingSource. LoadResult. The Error (e)}} / / the bound data source adapter. SetPager (model. The pager)Copy the code

Use is so convenient, some of the definitions need to have a certain understanding of Paging3, see the example for more functions; All you want, welcome to issue, if you think it is ok, can you give a star?

The GitHub address is here