本文实例讲述了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程序设计有所帮助。
总结以上是内存溢出为你收集整理的Android使用ListView实现下拉刷新及上拉显示更多的方法全部内容,希望文章能够帮你解决Android使用ListView实现下拉刷新及上拉显示更多的方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)