Android ListView 下拉刷新 上拉更多

AndroidListView下拉刷新上拉更多[实例]

今天得需求是做listview+上下拉动在header和footer显示progressdialog,但不影响用户操作

直接上代码,我已经加上注释了,自己看。

packagecom.stay.main;

importjava.net.HttpURLConnection;

importjava.util.ArrayList;

importjava.util.HashMap;

importorg.json.JSONObject;

importandroid.app.Activity;

importandroid.app.ProgressDialog;

importandroid.graphics.drawable.Drawable;

importandroid.os.Bundle;

importandroid.os.Handler;

importandroid.os.HandlerThread;

importandroid.os.Looper;

importandroid.os.Message;

importandroid.view.LayoutInflater;

importandroid.view.View;

importandroid.widget.AbsListView;

importandroid.widget.AbsListView.OnScrollListener;

importandroid.widget.Button;

importandroid.widget.LinearLayout;

importandroid.widget.ListView;

importandroid.widget.RelativeLayout;

importandroid.widget.TextView;

importcom.stay.wifi.R;

/**

*@authorStay

*动态加载listview数据,上拉刷新,下拉更多

*/

publicclassListViewActivityextendsActivityimplementsOnScrollListener{

privatestaticfinalintLOAD=0;

privatestaticfinalintERROR=0;

privatestaticfinalintMEMBER=1;

privatestaticfinalintLOADED=2;

privatestaticfinalintDIALOG=3;

privatestaticfinalintFULL=4;

privateNearbyAdapteradapter;

privateListViewnearby_lv;

privateRelativeLayoutnearby_lv_header;

privateButtonlist_bottom_btn;

privateLinearLayoutlist_bottom_linear;

privateTextViewbottom_progress_text;

privateRelativeLayoutnearby_lv_footer;

privateButtonlist_header_btn;

privateLinearLayoutlist_header_linear;

privateTextViewheard_progress_text;

privateArrayList<JSONObject>nearby_data=newArrayList<JSONObject>();

privateintlastItem;

privateHashMap<String,Drawable>imageCache;

privatecom.stay.main.ListViewActivity.MyHandlermyHandler;

privateProgressDialogdialog;

privateintcurPage=1;

privatebooleanisMember=false;

privateintfirstItem;

publicintcount;

/**Calledwhentheactivityisfirstcreated.*/

@Override

publicvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

initView();

myHandler.sendEmptyMessage(LOAD);

}

@Override

publicvoidonScrollStateChanged(AbsListViewview,intscrollState){

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

publicvoidonScroll(AbsListViewview,intfirstVisibleItem,intvisibleItemCount,inttotalItemCount){

DebugUtil.debug("firstVisibleItem="+firstVisibleItem);

DebugUtil.debug("visibleItemCount="+visibleItemCount);

DebugUtil.debug("totalItemCount="+totalItemCount);

//这里要减二,因为我加了headerfooter

lastItem=firstVisibleItem+visibleItemCount-2;

firstItem=firstVisibleItem;

}

publicintgetData(){

try{

HttpURLConnectionconn=DownloadUtil.download("http://zdevl.mapi.jjdd.com/nearby/lbs?page="+curPage);

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{

returnFULL;

}

returnLOADED;

}catch(Exceptione){

returnERROR;

}

}

privateHandlerhandler=newHandler(){

@Override

publicvoidhandleMessage(Messagemsg){

super.handleMessage(msg);

switch(msg.what){

caseDIALOG:

list_bottom_linear.setVisibility(View.VISIBLE);

list_header_linear.setVisibility(View.VISIBLE);

break;

caseLOADED:

list_bottom_linear.setVisibility(View.GONE);

list_header_linear.setVisibility(View.GONE);

curPage++;

adapter.notifyDataSetChanged();

break;

caseERROR:

DebugUtil.debug("error,missingdata");

break;

caseMEMBER:

DebugUtil.debug("youmustregistformalmember");

break;

default:

break;

}

}

};

//创建子线程加载数据,然后更新

privateclassMyHandlerextendsHandler{

privateintstatus;

publicMyHandler(Looperlooper){

super(looper);

}

@Override

publicvoidhandleMessage(Messagemsg){

synchronized(this){

switch(msg.what){

caseLOAD://getdatafromserver

handler.sendEmptyMessage(DIALOG);//显示等待框

status=getData();

handler.sendEmptyMessageDelayed(status,1000);

break;

default:

break;

}

}

}

}

publicvoidinitView(){

imageCache=newHashMap<String,Drawable>();

HandlerThreadhandlerThread=newHandlerThread("nearby");

//在使用HandlerThread的getLooper()方法之前,必须先调用该类的start();

handlerThread.start();

myHandler=newMyHandler(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=newNearbyAdapter(ListViewActivity.this,nearby_data);

nearby_lv.setAdapter(adapter);

nearby_lv.setOnScrollListener(ListViewActivity.this);

}

}

核心部分就是这了,其他adapter,downloadUtil,debugUtil都在以前的帖子里,就不发了。

有问题的可以直接留言,或者加qq或加群:19370204

OwnBlog:http://www.stayalways.com/

QQ:104816053

AndroidQQGroup:19370204

转载请注明出处http://stay.cnblogs.com请尊重个人劳动成果。

相关推荐