based on Android official Asynclistutil optimization Classic ListView Paging loading mechanism (ii)
I wrote an addendum to article 1, which describes how to use the official Android paging Framework Asynclistutil optimization to improve common Recyclerview page load implementations. Asynclistutil as a general-purpose page loading framework that can be applied not only in Recyclerview but also in classic (traditional) ListView, here is a simple example Explains how to adjust the paging mechanism of a ListView through Asynclistutil.
A simple mainactivity for Asynclistutil and ListView, showing page loading:
Package Zhangphil.app;import Android.app.listactivity;import Android.content.context;import Android.graphics.Color ; Import Android.os.bundle;import Android.os.systemclock;import Android.support.annotation.nonnull;import Android.support.annotation.nullable;import Android.support.v7.util.asynclistutil;import Android.util.Log;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.abslistview;import Android.widget.arrayadapter;import Android.widget.listview;import Android.widget.textview;public class Mainactivity extends Listactivity {private final String TAG = "Debug"; Private final int NULL =-1; Private asynclistutil<dataitem> Masynclistutil; Private Myadapter Madapter; private int mfirstvisibleitem = NULL, Mvisibleitemcount = NULL; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Mydatacallback mdatacallback = new Mydatacallback (); MyViEwcallback mviewcallback = new Myviewcallback (); Masynclistutil = new Asynclistutil (Dataitem.class, Mdatacallback, Mviewcallback); Madapter = new Myadapter (this, NULL); Setlistadapter (Madapter); Getlistview (). Setonscrolllistener (New Abslistview.onscrolllistener () {@Override public void Onscrol Lstatechanged (Abslistview abslistview, int scrollstate) {masynclistutil.onrangechanged (); } @Override public void Onscroll (Abslistview abslistview, int firstvisibleitem, int visibleitemcount , int totalitemcount) {mfirstvisibleitem = Firstvisibleitem; Mvisibleitemcount = VisibleItemCount; } }); } @Override protected void Onlistitemclick (ListView l, View v, int position, long id) {Masynclistutil.refres H (); } private class Mydatacallback extends asynclistutil.datacallback<dataitem> {@Override public int RefReshdata () {//update the number of data. Suppose a preset updates several bars. return integer.max_value; /** * The time-consuming task of loading data here. * * @param data * @param startposition * @param itemCount */@Override Publi c void Filldata (dataitem[] data, int startposition, int itemCount) {log.d (TAG, "filldata:" + startposition + " , "+ ItemCount); for (int i = 0; i < ItemCount; i++) {DataItem DataItem = new DataItem (); Dataitem.pos = StartPosition + i; Dataitem.content = string.valueof (System.currenttimemillis ()); Data[i] = DataItem; Simulate time-consuming tasks and deliberately hibernate for a certain delay. Systemclock.sleep (100); }}} Private class Myviewcallback extends Asynclistutil.viewcallback {/** * @param outrange */@Override public void Getitemrangeinto (int[] outrange) {outrange[0] = MfirstvisIbleitem; OUTRANGE[1] = Mfirstvisibleitem + mvisibleitemcount; /** * If the current ListView is empty, actively load the data for the user. * Assuming that several data are preloaded * */if (outrange[0] = = NULL && outrange[1] = = null) { LOG.D (TAG, "Current ListView is empty!") "); Outrange[0] = 0; OUTRANGE[1] = 9; } log.d (TAG, "getitemrangeinto, currently visible position:" + outrange[0] + "~" + outrange[1]); } @Override public void Ondatarefresh () {madapter.notifydatasetchanged (); LOG.D (TAG, "Ondatarefresh"); } @Override public void onitemloaded (int position) {madapter.notifydatasetchanged (); LOG.D (TAG, "onitemloaded:" + position); }} Private class Myadapter extends Arrayadapter {private Viewholder holder; Public Myadapter (@NonNull context context, int resource) {Super (context, Resource); } @NonNull @Override public view GetView (int position, @Nullable View Convertview, @NonNull viewgroup parent) {if (Convertview = = null) {Convertview = Layoutinflater.from (Getapplicationcontext ()). Inflate (Android. R.layout.simple_list_item_2, NULL); Holder = new Viewholder (Convertview); Convertview.settag (holder); } else {holder = (Viewholder) convertview.gettag (); } holder.setdata (GetItem (position)); return convertview; } @Override public int getcount () {return masynclistutil.getitemcount (); } @Nullable @Override public DataItem getItem (int position) {return masynclistutil.getite m (position); }} private class Viewholder {public TextView Text1; Public TextView Text2; Public Viewholder (view view) {Text1 = View.findviewbyid (Android. R.ID.TEXT1); Text1.settextcolor (color.red); Text2 = View.findviewbyid (Android. R.ID.TEXT2); Text2.settextcolor (Color.Blue); The public void SetData (DataItem DataItem) {if (DataItem = = null) {text1.settext ("pos load. .."); Text2.settext ("Content loading ..."); } else {Text1.settext (string.valueof (Dataitem.pos)); Text2.settext (dataitem.content); }}} private class DataItem {public int pos; public String content; }}
(a) Like Recyclerview, I also apply the ListView scrolling event in the ListView to trigger the Asynclistutil onrangechanged, triggering the paging mechanism to start working.
(ii) As a demonstration, this example triggers a asynclistutil refresh event at the normal item click event in the ListView, which simulates a development scenario where the user is permanently stuck on a page that needs to actively refresh the data for the user.
(c) A little special attention: and Appendix Article 1 in Recyclerview in the Asynclistutil.datacallback different, in Recyclerview in the Datacallback, load the data filldata method of the first parameter of the database data array, if as a base This data type, such as String,int and so on, the direct data[i] is not a problem, Data[i] has been created and initialized by Asynclistutil, but if the custom data type, such as this example, the custom DataItem, Intent to load some developer-defined complex type, at which point the data[i "directly removed" is null! Therefore, in order to solve this problem, we should start from two aspects:
(a) on the one hand, if it is a custom data structure, in Filldata, each data[i] re-creates a new object instance for it and assigns it to data[i], in this case Data[i]=new DataItem ().
(b) On the other hand, when assigning a value to a view at the last level, it is determined whether the custom type is null, and the data element in the custom data structure (in this case, the member variable in DataItem) is not removed when the pointer is empty.
Appendix:
1, "Android-based official Asynclistutil optimization improved Recyclerview page loading mechanism (i)" Link: http://blog.csdn.net/zhangphil/article/details/78603499
2, "Recyclerview page loading framework based on the official Android paging Library" link: http://blog.csdn.net/zhangphil/article/details/78627332
Based on Android official Asynclistutil optimization Classic ListView Paging loading mechanism (ii)