Android使用ListView实现下拉刷新及上拉显示更多的方法
程序员文章站
2023-12-13 11:11:46
本文实例讲述了android使用listview实现下拉刷新及上拉显示更多的方法。分享给大家供大家参考,具体如下:
今天得需求是做listview+上下拉动在header...
本文实例讲述了android使用listview实现下拉刷新及上拉显示更多的方法。分享给大家供大家参考,具体如下:
今天得需求是做listview+上下拉动在header和footer显示progressdialog,但不影响用户操作
直接上代码,我已经加上注释了,自己看。
package com.stay.main; import java.net.httpurlconnection; import java.util.arraylist; import java.util.hashmap; import org.json.jsonobject; import android.app.activity; import android.app.progressdialog; import android.graphics.drawable.drawable; import android.os.bundle; import android.os.handler; import android.os.handlerthread; import android.os.looper; import android.os.message; import android.view.layoutinflater; import android.view.view; import android.widget.abslistview; import android.widget.abslistview.onscrolllistener; import android.widget.button; import android.widget.linearlayout; import android.widget.listview; import android.widget.relativelayout; import android.widget.textview; import com.stay.wifi.r; /** * @author stay * 动态加载listview数据,上拉 刷新,下拉 更多 */ public class listviewactivity extends activity implements onscrolllistener { private static final int load = 0; private static final int error = 0; private static final int member = 1; private static final int loaded = 2; private static final int dialog = 3; private static final int full = 4; private nearbyadapter adapter; private listview nearby_lv; private relativelayout nearby_lv_header; private button list_bottom_btn; private linearlayout list_bottom_linear; private textview bottom_progress_text; private relativelayout nearby_lv_footer; private button list_header_btn; private linearlayout list_header_linear; private textview heard_progress_text; private arraylist<jsonobject> nearby_data = new arraylist<jsonobject>(); private int lastitem; private hashmap<string, drawable> imagecache; private com.stay.main.listviewactivity.myhandler myhandler; private progressdialog dialog; private int curpage = 1; private boolean ismember = false; private int firstitem; public int count; /** called when the activity is first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); initview(); myhandler.sendemptymessage(load); } @override public void onscrollstatechanged(abslistview view, int scrollstate) { debugutil.debug("onscrollstatechanged"); //当滚动停止且滚动的总数等于数据的总数,去加载 if (lastitem == count && scrollstate == scroll_state_idle) { debugutil.debug("onscrollstatechanged--------next"); if (curpage == 4 && !ismember) { debugutil.show(this, "您不是正式会员,请申请正式会员,"); list_bottom_linear.setvisibility(view.gone); } else { //加载数据 myhandler.sendemptymessage(load); } return; } //当往上拉时更新数据,将data清空然后去重新加载 if (firstitem == 0 && scrollstate == scroll_state_idle) { debugutil.debug("onscrollstatechanged--------refresh"); curpage = 0; myhandler.sendemptymessage(load); } } @override public void onscroll(abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount) { debugutil.debug("firstvisibleitem=" + firstvisibleitem); debugutil.debug("visibleitemcount=" + visibleitemcount); debugutil.debug("totalitemcount=" + totalitemcount); //这里要减二,因为我加了header footer lastitem = firstvisibleitem + visibleitemcount - 2; firstitem = firstvisibleitem; } public int getdata() { try { httpurlconnection conn = downloadutil.download(url//自己写咯); arraylist<jsonobject> temp = jsonutil.streamtojsonlist(conn.getinputstream()); if (curpage == 0 && nearby_data.size() > 0) { nearby_data.clear(); count = 0; } if (temp != null && temp.size() > 0) { count += temp.size(); nearby_data.addall(temp); debugutil.debug("nearby_data.size()="+nearby_data.size()); } else { return full; } return loaded; } catch (exception e) { return error; } } private handler handler = new handler() { @override public void handlemessage(message msg) { super.handlemessage(msg); switch (msg.what) { case dialog: list_bottom_linear.setvisibility(view.visible); list_header_linear.setvisibility(view.visible); break; case loaded: list_bottom_linear.setvisibility(view.gone); list_header_linear.setvisibility(view.gone); curpage++; adapter.notifydatasetchanged(); break; case error: debugutil.debug("error,missing data"); break; case member: debugutil.debug("you must regist formal member"); break; default: break; } } }; //创建子线程加载数据,然后更新 private class myhandler extends handler { private int status; public myhandler(looper looper) { super(looper); } @override public void handlemessage(message msg) { synchronized (this) { switch (msg.what) { case load:// get data from server handler.sendemptymessage(dialog);//显示等待框 status = getdata(); handler.sendemptymessagedelayed(status, 1000); break; default: break; } } } } public void initview() { imagecache = new hashmap<string, drawable>(); handlerthread handlerthread = new handlerthread("nearby"); // 在使用handlerthread的getlooper()方法之前,必须先调用该类的start(); handlerthread.start(); myhandler = new myhandler(handlerthread.getlooper()); nearby_lv = (listview) findviewbyid(r.id.nearby_lv); nearby_lv_footer = (relativelayout) layoutinflater.from(listviewactivity.this).inflate(r.layout.nearby_lv_header, null); list_bottom_btn = (button) nearby_lv_footer.findviewbyid(r.id.list_bottom_btn); list_bottom_linear = (linearlayout) nearby_lv_footer.findviewbyid(r.id.list_bottom_linear); bottom_progress_text = (textview) nearby_lv_footer.findviewbyid(r.id.progress_text); nearby_lv_header = (relativelayout) layoutinflater.from(listviewactivity.this).inflate(r.layout.nearby_lv_header, null); list_header_btn = (button) nearby_lv_header.findviewbyid(r.id.list_bottom_btn); list_header_linear = (linearlayout) nearby_lv_header.findviewbyid(r.id.list_bottom_linear); heard_progress_text = (textview) nearby_lv_header.findviewbyid(r.id.progress_text); list_header_btn.settext("刷新"); list_bottom_btn.settext("更多"); list_header_linear.setvisibility(view.gone); nearby_lv.addheaderview(nearby_lv_header); nearby_lv.addfooterview(nearby_lv_footer); // list_header_btn.setonclicklistener(header_click); adapter = new nearbyadapter(listviewactivity.this, nearby_data); nearby_lv.setadapter(adapter); nearby_lv.setonscrolllistener(listviewactivity.this); } }
更多关于android相关内容感兴趣的读者可查看本站专题:《android service组件使用技巧总结》、《android编程之activity操作技巧总结》、《android资源操作技巧汇总》、《android操作json格式数据技巧总结》、《android开发入门与进阶教程》、《android视图view技巧总结》及《android控件用法总结》
希望本文所述对大家android程序设计有所帮助。