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

作者:Jacob-wj 时间:2023-01-10 04:29:45 

本文实例讲述了Android使用ListView实现下拉刷新及上拉显示更多的方法。分享给大家供大家参考,具体如下:

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

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


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);
   }
 }
 @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<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;
     }
   }
 };
//创建子线程加载数据,然后更新
 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()方法之前,必须先调用该类的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);
 }
}

希望本文所述对大家Android程序设计有所帮助。

标签:Android,ListView,刷新
0
投稿

猜你喜欢

  • spring使用JavaConfig进行配置的方法

    2023-08-22 20:52:46
  • 如何动态替换Spring容器中的Bean

    2023-05-22 20:18:59
  • Android Handler使用案例详解

    2021-08-17 06:46:32
  • 谈C# using的用法与好处

    2022-02-10 08:20:01
  • MyBatis测试报错:Cannot determine value type from string 'xxx'的解决办法

    2023-03-26 07:19:46
  • 详解C#中的System.Timers.Timer定时器的使用和定时自动清理内存应用

    2022-11-07 14:09:54
  • 盘点几种常见的java排序算法

    2023-09-17 10:13:10
  • 两天没解决的问题chatgpt用了5秒搞定隐藏bug

    2023-11-18 22:54:08
  • C语言转义字符实例详解

    2021-08-23 02:12:20
  • C#软件注册码的实现代码

    2021-10-14 06:14:38
  • Spingboot JPA CriteriaBuilder 如何获取指定字段

    2022-07-20 15:40:22
  • Android模拟器对应的电脑快捷键说明

    2022-11-09 17:44:07
  • SpringBoot Web详解静态资源规则与定制化处理

    2022-01-23 16:19:57
  • SpringBoot如何返回Json数据格式

    2023-01-12 16:28:51
  • Android Xutils3网络请求的封装详解及实例代码

    2021-07-20 20:27:49
  • C#采用OpenXml实现给word文档添加文字

    2022-06-13 09:48:46
  • Android仿知乎悬浮功能按钮FloatingActionButton效果

    2021-08-04 16:33:47
  • Echarts+SpringMvc显示后台实时数据

    2021-06-08 03:38:42
  • JAVA线程池原理实例详解

    2021-10-01 17:59:01
  • Spring Boot和Kotlin的无缝整合与完美交融

    2022-07-08 04:54:09
  • asp之家 软件编程 m.aspxhome.com