DevStore首页 > DEV资源 > 源码下载 > 源码详情

listview的上拉下拉刷新(带刷新时间)

 
Dancing | 2015-06-25 18:47    浏览量(3865)    评论(2)    收藏(20)
功能分类:特效 支持平台:Android 运行环境:Eclipse
开发语言:Java 开发工具:Eclipse 源码大小:1.29MB
下载源码 800 人下载

源码简介

listview的上拉下拉刷新(带刷新时间)

源码截图

  • 运行截图
  • 运行截图
  • 运行截图
DevStore所有源码来自用户上传分享,版权问题及牵扯到商业纠纷均与DevStore无关。

源码片段

package com.example.listviewrefresh;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

import com.example.listviewrefresh.XListView.IXListViewListener;

public class MainActivity extends Activity implements IXListViewListener {

	private XListView listView;
	private StoreAdapter listadapter;
	FreshTimeDBUtil dbUtil = null; // 刷新时间数据库
	private List list = new ArrayList();
	public static MainActivity mainActivity; // 单例对象

	private String activityname = "MainActivity";
	private int page = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		dbUtil = new FreshTimeDBUtil(this);
		intiView();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		setFirstData();
	}

	private void intiView() {
		// TODO Auto-generated method stub
		listadapter = new StoreAdapter(this, list);
		listView = (XListView) findViewById(R.id.NiceListview);
		listView.setAdapter(listadapter);
		listView.setXListViewListener(this);
		listView.setPullLoadEnable(false);
		listView.setFooterDividersEnabled(false);
		listView.setHeaderDividersEnabled(false);
	}

	// 上拉加载更多
	public void getRecordUpPullForeRefresh(final String url) {
		// 网络判断
		new Thread() {
			public void run() {
				if (page == 2) {
					for (int i = 10; i < 20; i++) {
						String iString = i + "";
						list.add(iString);
					}
				}
				Message msg = Message.obtain();
				msg.what = 1;
				msg.obj = list;
				handler.sendMessage(msg);

			};
		}.start();
	}

	// 下拉刷新最新数据
	public void getRecordDownPullForeRefresh(final String url) {
		// 网络判断
		new Thread() {
			public void run() {
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if (page == 1) {
					list.clear();
					for (int i = 0; i < 10; i++) {
						String iString = i + "";
						list.add(iString);
					}
				}
				Message msg = Message.obtain();
				msg.what = 2;
				msg.obj = list;
				handler.sendMessage(msg);

			};
		}.start();

	}

	@SuppressLint("HandlerLeak")
	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			onLoad();
			switch (msg.what) {
			case 1:
				listadapter.notifyDataSetChanged();
				if (list.size() != 10) {
					listView.setPullLoadEnable(false);
				}
				break;
			case 2:
				if (list.size() >= 10) {
					listView.setPullLoadEnable(true);
				}
				listadapter.notifyDataSetChanged();
				break;
			case -1:
				page--;
				Toast.makeText(MainActivity.this, "数据加载失败!", Toast.LENGTH_SHORT)
						.show();
				break;
			default:
				break;
			}
			listView.stopRefresh();
			listView.stopLoadMore();
		};
	};

	// 下拉刷新数据
	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		handler.postDelayed(new Runnable() {

			@Override
			public void run() {
				page = 1;
				getRecordDownPullForeRefresh("url");
			}
		}, 1200);
	}

	// 上拉加载更多
	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		handler.postDelayed(new Runnable() {

			@Override
			public void run() {
				page++;
				getRecordUpPullForeRefresh("url");
			}
		}, 1000);
	}

	/**
	 * 每次刷新结束调用此方法结束操作 保存时间
	 */
	private void onLoad() {
		listView.stopRefresh();
		listView.stopLoadMore();
		listView.setRefreshTime2(Tools.friendly_time(dbUtil.findLastTime( // 设置新的刷新时间
				activityname, activityname)));
		dbUtil.addRecord(activityname, activityname,
				String.valueOf(System.currentTimeMillis()));
	}

	// 第一次加载数据
	public void setFirstData() {
		onLoad();
		if (list.size() > 0)
			return;
		if (list.size() <= 0) { // 未传入数据,自行进行刷新
			if (!listView.mPullRefreshing)
				listView.startHeaderRefresh();
			return;
		}
	}

}
  •   赞(2) 赞 +1 赞(2) 已赞
  •   收藏(20) 收藏 +1 已收藏 取消
  •   下载(800)
评论(2)
jiachaotc

jiachaotc 2016-04-06 11:25:36

看上去不错,学习一下
回复(0) 赞(0) 赞(1)
a1232626

a1232626 2015-07-09 00:40:20

过来学习学习
回复(0) 赞(0) 赞(1)
  • 推荐
  • 审核
  • 收录
  • 获取活力值

源码上传作者

  • 16 能力值
  • 8 源码
  • 0 评测
  • 0 资料

热门源码