Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> Android開發ListView滾動提示框實例

Android開發ListView滾動提示框實例

編輯:Android開發實例

  實例核心代碼:

  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);
          }
      }

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved