基于Android官方AsyncListUtil优化经典ListView分页加载机制(二)
程序员文章站
2023-01-29 21:08:13
基于android官方asynclistutil优化经典listview分页加载机制(二)。之前介绍了如何使用android官方的分页加载框架asynclistutil优化改进...
基于android官方asynclistutil优化经典listview分页加载机制(二)。之前介绍了如何使用android官方的分页加载框架asynclistutil优化改进常见的recyclerview分页加载实现。asynclistutil作为一种通用的分页加载框架,不仅可以套用在recyclerview,也可也适用在经典(传统)listview中,下面给出一个简单例子,说明如何通过asynclistutil调整listview的分页加载机制。
一个简单的mainactivity适用asynclistutil和listview,展示分页加载:- packagezhangphil.app;
- importandroid.app.listactivity;
- importandroid.content.context;
- importandroid.graphics.color;
- importandroid.os.bundle;
- importandroid.os.systemclock;
- importandroid.support.annotation.nonnull;
- importandroid.support.annotation.nullable;
- importandroid.support.v7.util.asynclistutil;
- importandroid.util.log;
- importandroid.view.layoutinflater;
- importandroid.view.view;
- importandroid.view.viewgroup;
- importandroid.widget.abslistview;
- importandroid.widget.arrayadapter;
- importandroid.widget.listview;
- importandroid.widget.textview;
- publicclassmainactivityextendslistactivity{
- privatefinalstringtag=“调试”;
- privatefinalintnull=-1;
- privateasynclistutil
masynclistutil; - privatemyadaptermadapter;
- privateintmfirstvisibleitem=null,mvisibleitemcount=null;
- @override
- protectedvoidoncreate(bundlesavedinstancestate){
- super.oncreate(savedinstancestate);
- mydatacallbackmdatacallback=newmydatacallback();
- myviewcallbackmviewcallback=newmyviewcallback();
- masynclistutil=newasynclistutil(dataitem.class,20,mdatacallback,mviewcallback);
- madapter=newmyadapter(this,null);
- setlistadapter(madapter);
- getlistview().setonscrolllistener(newabslistview.onscrolllistener(){
- @override
- publicvoidonscrollstatechanged(abslistviewabslistview,intscrollstate){
- masynclistutil.onrangechanged();
- }
- @override
- publicvoidonscroll(abslistviewabslistview,intfirstvisibleitem,intvisibleitemcount,inttotalitemcount){
- mfirstvisibleitem=firstvisibleitem;
- mvisibleitemcount=visibleitemcount;
- }
- });
- }
- @override
- protectedvoidonlistitemclick(listviewl,viewv,intposition,longid){
- masynclistutil.refresh();
- }
- privateclassmydatacallbackextendsasynclistutil.datacallback
{ - @override
- publicintrefreshdata(){
- //更新数据的个数。
- //假设预先设定更新若干条。
- returninteger.max_value;
- }
- /**
- *在这里完成数据加载的耗时任务。
- *
- *@paramdata
- *@paramstartposition
- *@paramitemcount
- */
- @override
- publicvoidfilldata(dataitem[]data,intstartposition,intitemcount){
- log.d(tag,”filldata:”+startposition+“,”+itemcount);
- for(inti=0;i
- dataitemdataitem=newdataitem();
- dataitem.pos=startposition+i;
- dataitem.content=string.valueof(system.currenttimemillis());
- data[i]=dataitem;
- //模拟耗时任务,故意休眠一定时延。
- systemclock.sleep(100);
- }
- }
- }
- privateclassmyviewcallbackextendsasynclistutil.viewcallback{
- /**
- *@paramoutrange
- */
- @override
- publicvoidgetitemrangeinto(int[]outrange){
- outrange[0]=mfirstvisibleitem;
- outrange[1]=mfirstvisibleitem+mvisibleitemcount;
- /**
- *如果当前listview为空,主动为用户加载数据.
- *假设预先加载若干条数据
- *
- */
- if(outrange[0]==null&&outrange[1]==null){
- log.d(tag,”当前listview为空!”);
- outrange[0]=0;
- outrange[1]=9;
- }
- log.d(tag,”getitemrangeinto,当前可见position:”+outrange[0]+“~”+outrange[1]);
- }
- @override
- publicvoidondatarefresh(){
- madapter.notifydatasetchanged();
- log.d(tag,”ondatarefresh”);
- }
- @override
- publicvoidonitemloaded(intposition){
- madapter.notifydatasetchanged();
- log.d(tag,”onitemloaded:”+position);
- }
- }
- privateclassmyadapterextendsarrayadapter{
- privateviewholderholder;
- publicmyadapter(@nonnullcontextcontext,intresource){
- super(context,resource);
- }
- @nonnull
- @override
- publicviewgetview(intposition,@nullableviewconvertview,@nonnullviewgroupparent){
- if(convertview==null){
- convertview=layoutinflater.from(getapplicationcontext()).inflate(android.r.layout.simple_list_item_2,null);
- holder=newviewholder(convertview);
- convertview.settag(holder);
- }else{
- holder=(viewholder)convertview.gettag();
- }
- holder.setdata(getitem(position));
- returnconvertview;
- }
- @override
- publicintgetcount(){
- returnmasynclistutil.getitemcount();
- }
- @nullable
- @override
- publicdataitemgetitem(intposition){
- returnmasynclistutil.getitem(position);
- }
- }
- privateclassviewholder{
- publictextviewtext1;
- publictextviewtext2;
- publicviewholder(viewview){
- text1=view.findviewbyid(android.r.id.text1);
- text1.settextcolor(color.red);
- text2=view.findviewbyid(android.r.id.text2);
- text2.settextcolor(color.blue);
- }
- publicvoidsetdata(dataitemdataitem){
- if(dataitem==null){
- text1.settext(”pos加载中…”);
- text2.settext(”content加载中…”);
- }else{
- text1.settext(string.valueof(dataitem.pos));
- text2.settext(dataitem.content);
- }
- }
- }
- privateclassdataitem{
- publicintpos;
- publicstringcontent;
- }
- }
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 = "调试"; private final int null = -1; private asynclistutil 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, 20, mdatacallback, mviewcallback); madapter = new myadapter(this, null); setlistadapter(madapter); getlistview().setonscrolllistener(new abslistview.onscrolllistener() { @override public void onscrollstatechanged(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.refresh(); } private class mydatacallback extends asynclistutil.datacallback { @override public int refreshdata() { //更新数据的个数。 //假设预先设定更新若干条。 return integer.max_value; } /** * 在这里完成数据加载的耗时任务。 * * @param data * @param startposition * @param itemcount */ @override public 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; //模拟耗时任务,故意休眠一定时延。 systemclock.sleep(100); } } } private class myviewcallback extends asynclistutil.viewcallback { /** * @param outrange */ @override public void getitemrangeinto(int[] outrange) { outrange[0] = mfirstvisibleitem; outrange[1] = mfirstvisibleitem + mvisibleitemcount; /** * 如果当前listview为空,主动为用户加载数据. * 假设预先加载若干条数据 * */ if (outrange[0] == null && outrange[1] == null) { log.d(tag, "当前listview为空!"); outrange[0] = 0; outrange[1] = 9; } log.d(tag, "getitemrangeinto,当前可见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.getitem(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); } public void setdata(dataitem dataitem) { if (dataitem == null) { text1.settext("pos加载中..."); text2.settext("content加载中..."); } else { text1.settext(string.valueof(dataitem.pos)); text2.settext(dataitem.content); } } } private class dataitem { public int pos; public string content; } }
(一)和recyclerview一样,我在listview中同样适用listview的滚动事件触发asynclistutil的onrangechanged,从而触发分页加载机制开始运作。
(二)作为演示,本例在listview的普通item点击事件触发一次asynclistutil的refresh事件,这种情况模拟当用户长期停留在一个页面需要为用户主动刷新数据的开发场景。
(三)一点儿特别注意:和附录文章1在recyclerview中的asynclistutil.datacallback不同,在recyclerview中的datacallback,加载数据filldata方法的第一个参数data数组,如果作为基本数据类型如string,int等等这类,直接data[i]是没有问题的,data[i]已经被asynclistutil创建和初始化完成,但是如果自定义数据类型,比如本例自定义了dataitem,意图装载一些开发者自定义的复杂类型,此时直接取出的data[i]为null!所以,为了解决这个问题,要从两方面入手:
(a)一方面,如果是自定义的数据结构,在filldata中,每一个data[i]为其重新创建new出来一个对象实例,然后赋值给data[i],本例是data[i]=new dataitem()。
(b)另一方面,在最后一关对view进行赋值时候,判断自定义类型是否为null,不会空指针时候才取出自定义数据结构中的数据元素(本例是dataitem中的成员变量)使用。