XListView实现网络加载图片和下拉刷新
程序员文章站
2023-12-25 15:55:27
本文实例为大家分享了xlistview实现网络加载图片,和下拉刷新的功能,供大家参考,具体内容如下
mainactivity.java
public...
本文实例为大家分享了xlistview实现网络加载图片,和下拉刷新的功能,供大家参考,具体内容如下
mainactivity.java
public class mainactivity extends appcompatactivity { private xlistview contents; private int page = 0; private mybaseadapter adapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); contents = findviewbyid(r.id.contents); adapter = new mybaseadapter(getlayoutinflater()); contents.setadapter(adapter); //是否开启下拉刷新 上拉加载 //contents.setpullrefreshenable(false); contents.setpullloadenable(true); contents.setxlistviewlistener(new xlistview.ixlistviewlistener() { @override public void onrefresh() { page = 0; loaddata(page); //加载刷新数据 } @override public void onloadmore() { loaddata(page); } }); //一进来就去加载第一页数据 loaddata(page); } private string url = "http://apis.juhe.cn/cook/query?key=3ec004200a6a2f4cf4774e480c006375&menu=%e8%a5%bf%e7%ba%a2%e6%9f%bf&rn=10&pn="; private void loaddata(int page) { string requesturlwithpagenum = url + page; //加载网络数据 new asynctask<string, void, list<dataitem>>() { @override protected list<dataitem> doinbackground(string... strings) { responsebean responsebean = null; try { url url = new url(strings[0]); httpurlconnection urlconnection = (httpurlconnection) url.openconnection(); urlconnection.setrequestmethod("get"); urlconnection.setconnecttimeout(5000); urlconnection.setreadtimeout(5000); int responsecode = urlconnection.getresponsecode(); if (responsecode == 200) { string str = stream2string(urlconnection.getinputstream()); responsebean = new gson().fromjson(str, responsebean.class); } else { // } return responsebean == null ? null : responsebean.getresult().getdata(); } catch (malformedurlexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } return null; } @override protected void onpostexecute(list<dataitem> dataitems) { if (dataitems == null) { toast.maketext(mainactivity.this, "请求数据错误", toast.length_long).show(); return; } //更新数据 updatedata(dataitems); loadcompleted(); } }.execute(url); } private string stream2string(inputstream is) throws ioexception { bufferedreader br = new bufferedreader(new inputstreamreader(is)); stringbuilder sb = new stringbuilder(); for (string tmp = br.readline(); tmp != null; tmp = br.readline()) { sb.append(tmp); } return sb.tostring(); } private void updatedata(list<dataitem> datas) { if (page == 0) { adapter.setdatas(datas); } else { adapter.adddatas(datas); } } //通过 加载 / 刷新 完成 private void loadcompleted() { //通过listview:刷新、加载完成 page++; contents.stoploadmore(); contents.stoprefresh(); } }
mybaseadapter.java
public class mybaseadapter extends baseadapter { private list<dataitem> mdatas; protected layoutinflater minflater; /** * 更新数据 */ public void setdatas(list<dataitem> datas) { mdatas.clear(); if (datas != null) { mdatas.addall(datas); } notifydatasetchanged(); } /** * 追加数据 */ public void adddatas(list<dataitem> datas) { if (datas != null) { mdatas.addall(datas); notifydatasetchanged(); } } public mybaseadapter(layoutinflater minflater) { this.minflater = minflater; mdatas = new arraylist<>(); } @override public int getcount() { return mdatas.size(); } @override public dataitem getitem(int position) { return mdatas.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { baseviewholder viewholder = null; if (convertview == null) { convertview = minflater.inflate(r.layout.item, parent, false); viewholder = new baseviewholder(convertview); } else { viewholder = (baseviewholder) convertview.gettag(); } viewholder.binddata(getitem(position)); return convertview; } public class baseviewholder { private view itemview; private imageview icon; private textview title; private textview date; public baseviewholder(view itemview) { this.itemview = itemview; title = itemview.findviewbyid(r.id.title); date = itemview.findviewbyid(r.id.date); icon = itemview.findviewbyid(r.id.icon); itemview.settag(this); } public void binddata(dataitem dataitem) { title.settext(dataitem.gettitle()); date.settext(dataitem.getid()); imageloader.getinstance().displayimage(dataitem.getfirstimageurl(), icon, imageloaderconfigs.getdefaultdisplayimageoptions(icon.getcontext())); } } } imageloader.java public class imageloaderconfigs { public static imageloaderconfiguration getimageloaderconfiguration(context context) { imageloaderconfiguration configuration = new imageloaderconfiguration.builder(context) //内在缓存额外选项, 最大的宽度,高度 //.memorycacheextraoptions(480, 800) // default = device screen dimensions 内存缓存文件的最大长宽 //.diskcacheextraoptions(480, 800, null) // 本地缓存的详细信息(缓存的最大长宽),最好不要设置这个 //线程池配置 //.taskexecutor() //.taskexecutorforcachedimages() //.threadpoolsize(3) // default 线程池内加载的数量 //.threadpriority(thread.norm_priority - 2) // default 设置当前线程的优先级 //任务处理优先级 fist in fist out //.tasksprocessingorder(queueprocessingtype.fifo) // default //内存中不缓存一张图片的多个尺寸大小 //.denycacheimagemultiplesizesinmemory() //内在缓存策略 //.memorycache(new lrumemorycache(2 * 1024 * 1024)) //可以通过自己的内存缓存实现 //内存缓存大小 //.memorycachesize(2 * 1024 * 1024) // 内存缓存的最大值 //内在缓存大小:占用百分比 .memorycachesizepercentage(13) // default //磁盘缓存策略 //.diskcache(new lrudiskcache()) // default 可以自定义缓存路径 //磁盘缓存大小 .diskcachesize(50 * 1024 * 1024) // 50 mb sd卡(本地)缓存的最大值 //.diskcachefilecount(100) // 可以缓存的文件数量 // default为使用hashcode对uil进行加密命名, 还可以用md5(new md5filenamegenerator())加密 //.diskcachefilenamegenerator(new hashcodefilenamegenerator()) //.imagedownloader(new baseimagedownloader(context)) // default //(new baseimagedecoder(false)) // default //加载具体图片时的一些配置 .defaultdisplayimageoptions(displayimageoptions.createsimple()) // default .writedebuglogs() // 打印debug log .build(); return configuration; } public static displayimageoptions getdefaultdisplayimageoptions(context context) { displayimageoptions displayimageoptions = new displayimageoptions.builder() //是否缓存 .cacheinmemory(true) .cacheondisk(true) //rgb 565 r红色占5 g绿色占6 b蓝色占5 -> 2字节 //alpha //argb 4444 4 4 4 4 -> 2字节 //argb 8888 -> 4字节 //10 * 10 用rgb565 -> 10*10*2 .bitmapconfig(bitmap.config.rgb_565) //加载时、加载错误时展示什么内容 .showimageonloading(r.mipmap.ic_launcher) .showimageonfail(r.mipmap.ic_launcher) // .imagescaletype(imagescaletype.exactly_stretched) //加载效果 //ctrl + p .displayer(new circlebitmapdisplayer()) .build(); //ctrl + h //bitmapdisplayer; return displayimageoptions; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。