一聚教程网:一个值得你收藏的教程网站

热门教程

Android使用ListView实现下拉刷新及上拉显示更多的方法

时间:2022-06-25 23:29:14 编辑:袖梨 来源:一聚教程网

今天得需求是做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;

/**

 * @author Stay

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

 */

publicclassListViewActivityextendsActivityimplementsOnScrollListener {

  privatestaticfinalintLOAD =0;

  privatestaticfinalintERROR =0;

  privatestaticfinalintMEMBER =1;

  privatestaticfinalintLOADED =2;

  privatestaticfinalintDIALOG =3;

  privatestaticfinalintFULL =4;

  privateNearbyAdapter adapter;

  privateListView nearby_lv;

  privateRelativeLayout nearby_lv_header;

  privateButton list_bottom_btn;

  privateLinearLayout list_bottom_linear;

  privateTextView bottom_progress_text;

  privateRelativeLayout nearby_lv_footer;

  privateButton list_header_btn;

  privateLinearLayout list_header_linear;

  privateTextView heard_progress_text;

  privateArrayListnearby_data =newArrayList();

  privateintlastItem;

  privateHashMapimageCache;

  privatecom.stay.main.ListViewActivity.MyHandler myHandler;

  privateProgressDialog dialog;

  privateintcurPage =1;

  privatebooleanisMember =false;

  privateintfirstItem;

  publicintcount;

  /** Called when the activity is first created. */

  @Override

  publicvoidonCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    initView();

    myHandler.sendEmptyMessage(LOAD);

  }

  @Override

  publicvoidonScrollStateChanged(AbsListView view,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(AbsListView view,intfirstVisibleItem,intvisibleItemCount,inttotalItemCount) {

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

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

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

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

    lastItem = firstVisibleItem + visibleItemCount -2;

    firstItem = firstVisibleItem;

  }

  publicintgetData() {

    try{

      HttpURLConnection conn = DownloadUtil.download(url//自己写咯);

      ArrayListtemp = 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(Exception e) {

      returnERROR;

    }

  }

  privateHandler handler =newHandler() {

    @Override

    publicvoidhandleMessage(Message msg) {

      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,missing data");

        break;

      caseMEMBER:

        DebugUtil.debug("you must regist formal member");

        break;

      default:

        break;

      }

    }

  };

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

  privateclassMyHandlerextendsHandler {

    privateintstatus;

    publicMyHandler(Looper looper) {

      super(looper);

    }

    @Override

    publicvoidhandleMessage(Message msg) {

      synchronized(this) {

        switch(msg.what) {

        caseLOAD:// get data from server

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

          status = getData();

          handler.sendEmptyMessageDelayed(status,1000);

          break;

        default:

          break;

        }

      }

    }

  }

  publicvoidinitView() {

    imageCache =newHashMap();

    HandlerThread handlerThread =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);

  }

}

 

热门栏目