ListView实现下拉刷新加载更多的实例代码(直接拿来用)
程序员文章站
2024-03-05 08:16:30
listview api bixu 好好看看
mnewsadapter.notifydatasetchanged();//刷新listview
自定义的refreash...
listview api bixu 好好看看
mnewsadapter.notifydatasetchanged();//刷新listview
自定义的refreashlistview
package com.itguang.dell_pc.myapplication.view; import android.content.context; import android.util.attributeset; import android.view.motionevent; import android.view.view; import android.view.animation.animation; import android.view.animation.rotateanimation; import android.widget.abslistview; import android.widget.imageview; import android.widget.listview; import android.widget.progressbar; import android.widget.textview; import com.itguang.dell_pc.myapplication.r; import java.text.simpledateformat; import java.util.date; /** * 作者: 小光哥 on 2016/7/7. * 邮箱: 1445037803@qq.com * 修订历史: * 描述: * ┏┓ ┏┓ * ┏┛┻━━━┛┻┓━━━━┻┓ * ┃ ┃ * ┃ ━ ┃ * ┃ > < ┃ * ┃ ┃ * ┃... ⌒ ... ┃ * ┃ ┃ * ┗━┓ ┏━┛ * ┃ ┃ code is far away from bug with the animal protecting * ┃ 史 ┃ 神兽保佑,代码无bug * ┃ 诗 ┃ * ┃ 之 ┃ * ┃ 宠 ┃ * ┃ ┃ * ┃ ┗━━━┓ * ┃bug天敌 ┣┓┣┓┣┓┣┓┣┓ * ┃ ┏┛ * ┗┓┓┏━┳┓┏┛ * ┃┫┫ ┃┫┫ * ┗┻┛ ┗┻┛ */ public class refreshlistview extends listview implements abslistview.onscrolllistener { private static final int state_pull_refresh = 0;// 下拉刷新 private static final int state_release_refresh = 1;// 松开刷新 private static final int state_refreshing = 2;// 正在刷新 private int mcurrrentstate = state_pull_refresh;// 当前状态 private view mheaderview; private textview tvtitle; private textview tvtime; private progressbar pbprogress; private imageview ivarrow; private rotateanimation animup; private rotateanimation animdown; private int starty = -1;// 滑动起点的y坐标 private int endy; private int measuredheight; private int mfooterviewheight; private view footerview; public refreshlistview(context context) { super(context); initheaderview(); initfooterview(); } public refreshlistview(context context, attributeset attrs) { super(context, attrs); initheaderview(); initfooterview(); } public refreshlistview(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); initheaderview(); initfooterview(); } /** * 初始化头布局 */ private void initheaderview() { mheaderview = view.inflate(getcontext(), r.layout.refeeash_header, null); this.addheaderview(mheaderview); tvtitle = (textview) mheaderview.findviewbyid(r.id.tv_title); tvtime = (textview) mheaderview.findviewbyid(r.id.tv_time); pbprogress = (progressbar) mheaderview.findviewbyid(r.id.pb_progress); ivarrow = (imageview) mheaderview.findviewbyid(r.id.iv_arr); mheaderview.measure(0, 0);//先测量再拿到它的高度 measuredheight = mheaderview.getmeasuredheight(); mheaderview.setpadding(0, -measuredheight, 0, 0); initarrowanim(); tvtime.settext("最后刷新时间:" + getcurrenttime()); } /** * 初始化脚布局 */ public void initfooterview() { footerview = view.inflate(getcontext(), r.layout.refreash_listview_footer, null); this.addfooterview(footerview); footerview.measure(0, 0); mfooterviewheight = footerview.getmeasuredheight(); footerview.setpadding(0, -mfooterviewheight, 0, 0);//默认隐藏脚布局 this.setonscrolllistener(this); } private boolean isloadingmore; @override public void onscrollstatechanged(abslistview view, int scrollstate) { if (scrollstate == scroll_state_fling || scrollstate == scroll_state_idle) { if (getlastvisibleposition() == getcount() - 1 && !isloadingmore) {//滑倒最后 system.out.println("到底了......"); footerview.setpadding(0, 0, 0, 0); setselection(getcount() - 1);//改变listview的显示位置 isloadingmore = true; if (mlistener != null) { mlistener.onloadmore(); } } } } @override public void onscroll(abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount) { } @override public boolean ontouchevent(motionevent ev) { switch (ev.getaction()) { case motionevent.action_down: starty = (int) ev.getrawy(); break; case motionevent.action_move: if (starty == -1) {//有时候不会响应 motionevent.action_down 事件,这是要重新获取starty坐标 starty = (int) ev.getrawy(); } //当正在刷新的时候,跳出循环,不再执行下面逻辑 if (mcurrrentstate == state_release_refresh) { break; } endy = (int) ev.getrawy(); int dy = endy - starty;//计算手指滑动距离 if (dy > 0 && getfirstvisibleposition() == 0) {// 只有下拉并且当前是第一个item,才允许下拉 int padding = dy - measuredheight;//计算padding mheaderview.setpadding(0, padding, 0, 0);//设置当前padding if (padding > 0 && mcurrrentstate != state_release_refresh) { mcurrrentstate = state_release_refresh; refreshstate(); } else if (padding < 0 && mcurrrentstate != state_pull_refresh) {// 改为下拉刷新状态 mcurrrentstate = state_pull_refresh; refreshstate(); } return true; } break; case motionevent.action_up: starty = -1;//手指抬起重置 //当状态是松开刷新时抬起了手指,正在刷新 if (mcurrrentstate == state_release_refresh) { mcurrrentstate = state_refreshing;// 正在刷新 mheaderview.setpadding(0, 0, 0, 0);// 显示 refreshstate(); } else if (mcurrrentstate == state_pull_refresh) { mheaderview.setpadding(0, -measuredheight, 0, 0);// 隐藏 } break; default: break; } return super.ontouchevent(ev); } private string getcurrenttime() { simpledateformat format = new simpledateformat("yyyy-mm-dd hh:mm:ss"); string currenttime = format.format(new date()); return currenttime; } private void initarrowanim() { //初始化箭头动画 animup = new rotateanimation(0, -180, animation.relative_to_self, 0.5f, animation.relative_to_self, 0.5f); animup.setduration(500); animup.setfillafter(true);//保持状态 //箭头向下动画 animdown = new rotateanimation(-180, 0, animation.relative_to_self, 0.5f, animation.relative_to_self, 0.5f); animdown.setduration(500); animdown.setfillafter(true); } /** * 刷新下拉控件的布局 */ private void refreshstate() { switch (mcurrrentstate) { case state_pull_refresh: tvtitle.settext("下拉刷新"); ivarrow.setvisibility(view.visible); pbprogress.setvisibility(view.invisible); ivarrow.startanimation(animdown); break; case state_release_refresh: tvtitle.settext("松开刷新"); ivarrow.setvisibility(view.visible); pbprogress.setvisibility(view.invisible); ivarrow.startanimation(animup); break; case state_refreshing: tvtitle.settext("正在刷新..."); ivarrow.clearanimation();// 必须先清除动画,才能隐藏 ivarrow.setvisibility(view.invisible); pbprogress.setvisibility(view.visible); if (mlistener != null) { mlistener.onrefreash(); } break; default: break; } } onrefreashlistener mlistener; public void setonrefreashlistener(onrefreashlistener listener) { mlistener = listener; } public interface onrefreashlistener { void onrefreash(); void onloadmore(); } /** * 收起下拉刷新的控件 */ public void onrefreashcomplete() { if (isloadingmore) { footerview.setpadding(0, -mfooterviewheight, 0, 0);//隐藏脚布局 isloadingmore = false; } else { mcurrrentstate = state_pull_refresh; tvtitle.settext("下拉刷新"); ivarrow.setvisibility(view.visible); pbprogress.setvisibility(view.invisible); mheaderview.setpadding(0, -measuredheight, 0, 0);//隐藏 } } public void nofooterview() { footerview.setpadding(0, mfooterviewheight, 0, 0);//隐藏脚布局 } }
代码中引用refreashlistview
package com.itguang.dell_pc.myapplication.base; import android.app.activity; import android.support.v4.view.pageradapter; import android.support.v4.view.viewpager; import android.text.textutils; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.imageview; import android.widget.listview; import android.widget.textview; import android.widget.toast; import com.google.gson.gson; import com.itguang.dell_pc.myapplication.r; import com.itguang.dell_pc.myapplication.domain.newsdata; import com.itguang.dell_pc.myapplication.domain.tabdata; import com.itguang.dell_pc.myapplication.globe.globecontents; import com.itguang.dell_pc.myapplication.view.refreshlistview; import com.lidroid.xutils.bitmaputils; import com.lidroid.xutils.httputils; import com.lidroid.xutils.viewutils; import com.lidroid.xutils.exception.httpexception; import com.lidroid.xutils.http.responseinfo; import com.lidroid.xutils.http.callback.requestcallback; import com.lidroid.xutils.http.client.httprequest; import com.lidroid.xutils.view.annotation.viewinject; import com.viewpagerindicator.circlepageindicator; import java.util.arraylist; /** * 作者: 小光哥 on 2016/4/22. * 邮箱: 1445037803@qq.com * 修订历史: * 描述: * ┏┓ ┏┓ * ┏┛┻━━━┛┻┓━━━━┻┓ * ┃ ┃ * ┃ ━ ┃ * ┃ > < ┃ * ┃ ┃ * ┃... ⌒ ... ┃ * ┃ ┃ * ┗━┓ ┏━┛ * ┃ ┃ code is far away from bug with the animal protecting * ┃ 史 ┃ 神兽保佑,代码无bug * ┃ 诗 ┃ * ┃ 之 ┃ * ┃ 宠 ┃ * ┃ ┃ * ┃ ┗━━━┓ * ┃bug天敌 ┣┓┣┓┣┓┣┓┣┓ * ┃ ┏┛ * ┗┓┓┏━┳┓┏┛ * ┃┫┫ ┃┫┫ * ┗┻┛ ┗┻┛ */ /** * 也签详情页 */ public class tabdetailpager extends basemenudetailpager implements viewpager.onpagechangelistener { /** * @param activity * @param newstabdata */ newsdata.newstabdata mtabdata; private string murl; private tabdata mtabdetaildata; @viewinject(r.id.vp_news) private viewpager mviewpager; @viewinject(r.id.lv_newslist) private refreshlistview newslistview;//新闻列表 @viewinject(r.id.tv_title) private textview tvtitle;//新闻标题 @viewinject(r.id.indicator) private circlepageindicator indicator;//头条新闻指示器 private arraylist<tabdata.topnewsdata> mtopnewslist;//新闻数据集合 private int[] images; private arraylist<tabdata.tabnewsdata> mnewslist;//新闻数据集合 private string mmoreurl; private newsadapter mnewsadapter; public tabdetailpager(activity activity, newsdata.newstabdata newstabdata) { super(activity); mtabdata = newstabdata; murl = globecontents.server_url + mtabdata.url; } @override public view initviews() { images = new int[]{r.drawable.topnews1, r.drawable.topnews21, r.drawable.topnews31, r.drawable.topimage}; view view = view.inflate(mactivity, r.layout.tab_detail_pager, null); view headerview = view.inflate(mactivity, r.layout.list_header_topnews, null);//加载头布局 // textview tvtitle = (textview) view.findviewbyid(r.id.tv_title); //事件注入 viewutils.inject(this, view); viewutils.inject(this, headerview); mviewpager.addonpagechangelistener(this); //将肥头条新闻以头布局形式加载到listview中 newslistview.addheaderview(headerview); //设置下拉刷新监听 newslistview.setonrefreashlistener(new refreshlistview.onrefreashlistener() { @override public void onrefreash() { getdatafromserver(); } @override public void onloadmore() { if (mmoreurl != null) { getmoredatafromserver(); } else { toast.maketext(mactivity, "没有更多数据了", toast.length_short).show(); newslistview.onrefreashcomplete(); // newslistview.nofooterview(); } } }); return view; } @override public void initdata() { getdatafromserver();//这是一个异步的, } private void getdatafromserver() { httputils utils = new httputils(); utils.send(httprequest.httpmethod.get, murl, new requestcallback<string>() { @override public void onsuccess(responseinfo<string> responseinfo) { string result = (string) responseinfo.result; // system.out.println("页签详情页返回结果:+++++++++++++++++++" + result); prasedata(result, false); newslistview.onrefreashcomplete(); } @override public void onfailure(httpexception e, string s) { toast.maketext(mactivity, "获取tabdetaildata失败!", toast.length_short).show(); // error.printstacktrace(); } }); } /** * 加载下一页数据 */ private void getmoredatafromserver() { httputils utils = new httputils(); utils.send(httprequest.httpmethod.get, mmoreurl, new requestcallback<string>() { @override public void onsuccess(responseinfo<string> responseinfo) { string result = (string) responseinfo.result; // system.out.println("页签详情页返回结果:+++++++++++++++++++" + result); prasedata(result, true); newslistview.onrefreashcomplete(); } @override public void onfailure(httpexception e, string s) { toast.maketext(mactivity, "获取tabdetaildata失败!", toast.length_short).show(); // error.printstacktrace(); } }); } /** * 把json字符串解析成为json对象 * * @param result 要解析的的json字符串 */ private void prasedata(string result, boolean isloadmore) { gson gson = new gson(); mtabdetaildata = gson.fromjson(result, tabdata.class);//返回tabdata对象 system.out.println("页签详情页:+++++++++++++++++++++" + mtabdata); //处理更多页面的逻辑 final string more = mtabdetaildata.data.more; if (!textutils.isempty(more)) { mmoreurl = globecontents.server_url + more; } else { mmoreurl = null; } if (!isloadmore) { mtopnewslist = mtabdetaildata.data.topnews; mnewslist = mtabdetaildata.data.news; if (mtopnewslist != null) { mviewpager.setadapter(new topnewsadapter()); indicator.setonpagechangelistener(this); indicator.setviewpager(mviewpager);//在设置完适配器后,在设置指示器 indicator.setsnap(true);//设置快照显示 indicator.onpageselected(0);//indicator会自作聪明,离开时会记录当前位置 tvtitle.settext(mtopnewslist.get(0).title); } mnewsadapter = new newsadapter(); //填充新闻列表 if (mnewslist != null) { newslistview.setadapter(mnewsadapter); } } else {//如果是加载下一页,需要将数据追加给原来的集合 arraylist<tabdata.tabnewsdata> news = mtabdetaildata.data.news; mnewslist.addall(news); mnewsadapter.notifydatasetchanged();//刷新listview } } @override public void onpagescrolled(int position, float positionoffset, int positionoffsetpixels) { } @override public void onpageselected(int position) {//更新头条新闻标题 tabdata.topnewsdata topnewsdata = mtopnewslist.get(position); tvtitle.settext(topnewsdata.title); } @override public void onpagescrollstatechanged(int state) { } /** * listview适配器 */ class newsadapter extends baseadapter { private final bitmaputils utils; newsadapter() { utils = new bitmaputils(mactivity); utils.configdefaultloadingimage(r.drawable.pic_item_list_default); } @override public int getcount() { return mnewslist.size(); } @override public object getitem(int position) { return mnewslist.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { viewholder holder; if (convertview == null) { convertview = view.inflate(mactivity, r.layout.list_news_item, null); holder = new viewholder(); holder.ivpic = (imageview) convertview.findviewbyid(r.id.iv_pic); holder.tvtitle = (textview) convertview.findviewbyid(r.id.tv_title); holder.tvdate = (textview) convertview.findviewbyid(r.id.tv_date); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } tabdata.tabnewsdata item = (tabdata.tabnewsdata) getitem(position); holder.tvtitle.settext(item.title); holder.tvdate.settext(item.pubdate); utils.display(holder.ivpic, item.listimage); return convertview; } } static class viewholder { public textview tvtitle; public textview tvdate; public imageview ivpic; } /** * viewpager适配器 */ class topnewsadapter extends pageradapter { private bitmaputils bitmaputils; topnewsadapter() { bitmaputils = new bitmaputils(mactivity); bitmaputils.configdefaultloadingimage(r.drawable.topnews_item_default);//设置默认图片 } @override public int getcount() { return mtabdetaildata.data.topnews.size(); } @override public boolean isviewfromobject(view view, object object) { return view == object; } @override public object instantiateitem(viewgroup container, int position) { imageview imageview = new imageview(mactivity); imageview.setimageresource(images[position]); imageview.setscaletype(imageview.scaletype.fit_xy);//基于控件大小填充图片 // tabdata.topnewsdata topnewsdata = mtopnewslist.get(position); // bitmaputils.display(imageview,mtabdetaildata.data.topnews.get(position).url); container.addview(imageview); return imageview; } @override public void destroyitem(viewgroup container, int position, object object) { container.removeview((view) object); } } }
以上所述是小编给大家介绍的listview实现下拉刷新加载更多的实例代码(直接拿来用)的全部叙述,希望对大家有所帮助