真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

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

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

創(chuàng)新互聯(lián)主營鎮(zhèn)平網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,重慶APP開發(fā)公司,鎮(zhèn)平h5微信小程序開發(fā)搭建,鎮(zhèn)平網(wǎng)站營銷推廣歡迎鎮(zhèn)平等地區(qū)企業(yè)咨詢

今天得需求是做listview+上下拉動在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
 * 動態(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 nearby_data = new ArrayList();
  private int lastItem;
  private HashMap 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");
    //當滾動停止且滾動的總數(shù)等于數(shù)據(jù)的總數(shù),去加載
    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 {
        //加載數(shù)據(jù)
        myHandler.sendEmptyMessage(LOAD);
      }
      return;
    }
    //當往上拉時更新數(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);
    //這里要減二,因為我加了header footer
    lastItem = firstVisibleItem + visibleItemCount - 2;
    firstItem = firstVisibleItem;
  }
  public int getData() {
    try {
      HttpURLConnection conn = DownloadUtil.download(url//自己寫咯);
      ArrayList 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();
    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ā)入門與進階教程》、《Android視圖View技巧總結(jié)》及《Android控件用法總結(jié)》

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


當前題目:Android使用ListView實現(xiàn)下拉刷新及上拉顯示更多的方法
新聞來源:http://weahome.cn/article/iipjch.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部