Recyclerview page loading framework based on Android's official paging library
I have previously written an article on Recyclerview page loading mechanism, which is based on the official Android Asynclistutil, see Appendix 1 for details. Now we introduce a Recyclerview page loading framework: Android Paging Library.
Android Paging Library is a page frame specifically made in Android's official SUPPORT-V7 support package, detailed documentation is available in Appendix 2 of Google's official documentation. When I wrote this article, the paging library version was 1.0.0-alpha3.
Using the Android Paging library you first need to add a reference to the Gradle:
Implementation ' ANDROID.ARCH.PAGING:RUNTIME:1.0.0-ALPHA3 '
Here I write the sample code to do a simple demo:
Package Zhangphil.demo;import Android.arch.paging.pagedlist;import Android.arch.paging.pagedlistadapter;import Android.arch.paging.tileddatasource;import Android.graphics.color;import Android.support.annotation.NonNull; Import Android.support.v7.app.appcompatactivity;import Android.os.bundle;import Android.support.v7.recyclerview.extensions.diffcallback;import Android.support.v7.widget.LinearLayoutManager; Import Android.support.v7.widget.recyclerview;import Android.text.textutils;import Android.util.log;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.linearlayout;import Android.widget.textview;import Java.util.arraylist;import java.util.List; Import Java.util.concurrent.executor;public class Mainactivity extends Appcompatactivity {private Pagedlist<databe An> mpagedlist; Private myDataSource Mdatasource; Private Recyclerview Mrecyclerview; Private Pagedlistadapter Madapter; Private Linearlayoutmanager MLAyoutmanager; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Mdatasource = new myDataSource (); Makepagelist (); Mrecyclerview = Findviewbyid (R.id.recycler_view); Mlayoutmanager = new Linearlayoutmanager (this); Mlayoutmanager.setorientation (linearlayout.vertical); Mrecyclerview.setlayoutmanager (Mlayoutmanager); Madapter = new Myadapter (); Mrecyclerview.setadapter (Madapter); Madapter.setlist (mpagedlist); Mrecyclerview.addonscrolllistener (New Recyclerview.onscrolllistener () {private int lastpos; @Override public void onscrollstatechanged (Recyclerview recyclerview, int newstate) {SUPER.ONSC Rollstatechanged (Recyclerview, newstate); Lastpos = Mlayoutmanager.findlastvisibleitemposition (); Mpagedlist.loadaround (Lastpos);//Trigger Android PaginThe load transaction logic for G. } }); } private void Makepagelist () {pagedlist.config mpagedlistconfig = new PagedList.Config.Builder () . SetPageSize (3)//number of paging data. In the back of the DataSource Loadrange, Count is the set value for each load: setprefetchdistance (5)//initialization, prefetch data quantity: Setena Bleplaceholders (False). build (); Mpagedlist = new Pagedlist.builder (). Setconfig (Mpagedlistconfig). Setdatasource (Mdatasource ). Setmainthreadexecutor (New Backgroundthreadtask ())//initialization phase enabled. Setbackgroundthreadexecutor ( New Mainthreadtask ())//initialization phase start. Build (); } Private class Backgroundthreadtask implements Executor {public backgroundthreadtask () {THIS.EXECU Te (new Runnable () {@Override public void run () {LOG.D ("Backgroundthread Task "," Run "); } }); } @Override Public VOID Execute (@NonNull Runnable Runnable) {runnable.run (); }} Private class Mainthreadtask implements Executor {public mainthreadtask () {This.execute (new Runnable () {@Override public void run () {LOG.D ("Mainthreadtask", "Run") ; } }); } @Override public void execute (@NonNull Runnable Runnable) {runnable.run (); }} Private class myDataSource extends tileddatasource<databean> {@Override public int countite MS () {return tileddatasource.count_undefined; /** * Note that background threading is required here. * * @param startposition * @param count * @return */@Override public LIST&L t;databean> loadrange (int startposition, int count) {log.d ("myDataSource", "Loadrange:" + startposition + " , "+ count"); list<databean> list = LOaddata (StartPosition, Count); return list; }}/** * Suppose you need to do some data loading tasks for background threads. * * @param startposition * @param count * @return */private list<databean> loaddata (int startpos ition, int count) {list<databean> List = new ArrayList (); for (int i = 0; i < count; i++) {Databean data = new Databean (); Data.id = StartPosition + i; Data.content = "[email protected]" + data.id; List.add (data); } return list; } private class Myviewholder extends Recyclerview.viewholder {public TextView Text1; Public TextView Text2; Public Myviewholder (View Itemview) {super (Itemview); Text1 = Itemview.findviewbyid (Android. R.ID.TEXT1); Text1.settextcolor (color.red); Text2 = Itemview.findviewbyid (Android. R.ID.TEXT2); Text2.settextcolor (Color.Blue); }} private Class Myadapter exTends Pagedlistadapter<databean, myviewholder> {public myadapter () {super (mdiffcallback); } @Override Public Myviewholder oncreateviewholder (viewgroup parent, int viewtype) {View view = Layoutinflater.from (Getapplicationcontext ()). Inflate (Android. R.layout.simple_list_item_2, NULL); Myviewholder holder = new Myviewholder (view); return holder; } @Override public void Onbindviewholder (myviewholder holder, int position) {Databean data = MPa Gedlist.get (position); Holder.text1.setText (string.valueof (position)); Holder.text2.setText (string.valueof (data.content)); }} private Diffcallback<databean> mdiffcallback = new diffcallback<databean> () {@Override public boolean areitemsthesame (@NonNull databean olditem, @NonNull Databean newitem) {log.d ("Diffcallback", "Areitemsthesame"); return olditem.id == Newitem.id; } @Override public boolean arecontentsthesame (@NonNull databean olditem, @NonNull Databean newitem) { LOG.D ("Diffcallback", "arecontentsthesame"); Return Textutils.equals (Olditem.content, newitem.content); } }; Private class Databean {public int id; public String content; }}
The function of code implementation is very simple, when the Recyclerview is falling, it triggers page loading, and displays the page load of the data Recyclerview subsequent use. For the time being, I will record the current experience of Android Paging Library technology as a stage memo to my study of Android Paging Library technology. This part of the code needs to be further refined, the Android Paging Library technical details Follow-up will continue to follow-up research.
Appendix:
1, "Android-based official Asynclistutil optimization improved Recyclerview page loading mechanism (i)" Link: http://blog.csdn.net/zhangphil/article/details/78603499
2, Google Android official Android Paging Library Technical documents home: Https://developer.android.google.cn/topic/libraries/architecture/ Paging.html#classes
Recyclerview page loading framework based on Android's official paging library