溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Android使用ListView實(shí)現(xiàn)下拉刷新及上拉顯示更多的方法

發(fā)布時(shí)間:2020-09-06 04:03:16 來源:腳本之家 閱讀:154 作者:Jacob-wj 欄目:移動(dòng)開發(fā)

本文實(shí)例講述了Android使用ListView實(shí)現(xiàn)下拉刷新及上拉顯示更多的方法。分享給大家供大家參考,具體如下:

今天得需求是做listview+上下拉動(dòng)在header和footer顯示progressdialog,但不影響用戶操作

直接上代碼,我已經(jīng)加上注釋了,自己看。

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
 * 動(dòng)態(tài)加載listview數(shù)據(jù),上拉 刷新,下拉 更多
 */
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");
    //當(dāng)滾動(dòng)停止且滾動(dòng)的總數(shù)等于數(shù)據(jù)的總數(shù),去加載
    if (lastItem == count && scrollState == SCROLL_STATE_IDLE) {
      DebugUtil.debug("onScrollStateChanged--------next");
      if (curPage == 4 && !isMember) {
        DebugUtil.show(this, "您不是正式會(huì)員,請(qǐng)申請(qǐng)正式會(huì)員,");
        list_bottom_linear.setVisibility(View.GONE);
      } else {
        //加載數(shù)據(jù)
        myHandler.sendEmptyMessage(LOAD);
      }
      return;
    }
    //當(dāng)往上拉時(shí)更新數(shù)據(jù),將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);
    //這里要減二,因?yàn)槲壹恿薶eader 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;
      }
    }
  };
//創(chuàng)建子線程加載數(shù)據(jù),然后更新
  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()方法之前,必須先調(diào)用該類的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);
  }
}

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android Service組件使用技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android資源操作技巧匯總》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android視圖View技巧總結(jié)》及《Android控件用法總結(jié)》

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI