Android listView的下拉刷新

Android ListView的下拉刷新,在实际开发中是很常用的,所以这里总结了,ListView下拉刷新的一个Demo。

该Demo的源码来自于github上的一个开源代码,只不过这里是将所需的library导入到项目中,然后将PulltoRefreshListview提取出来,进行了注释,看起来更简单。。。

该开源代码出来ListView的下拉刷新外,还有很多其他的刷新功能,如果有兴趣,可以把代码下下来研究一下,文章结尾会给出所有的源码。

实现效果图


源代码:


在写该Demo之前,需要:

第一步:导入三个Android library:如上图library、pulltorefreshlistfragment、pulltorefreshviewpager;

第二部:将library分别add到 pulltorefreshlistfragment pulltorefreshviewpager;

第三部:这时候将这三个Android library分别add到本项目Demo中。

然后可以写代码了:

布局文件:

activity_pull_to_refresh_list.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<!--     The PullToRefreshListView replaces a standard ListView widget. -->

    <com.handmark.pulltorefresh.library.PullToRefreshListView
        android:id="@+id/pull_refresh_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:cacheColorHint="#00000000"
        android:divider="#19000000"
        android:dividerHeight="4dp"
        android:fadingEdge="none"
        android:fastScrollEnabled="false"
        android:footerDividersEnabled="false"
        android:headerDividersEnabled="false"
        android:smoothScrollbar="true" />

</LinearLayout>
代码文件:
PullToRefreshListActivity.java:

package com.listviewpulltorefresh;

import java.util.Arrays;
import java.util.LinkedList;

import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

public final class PullToRefreshListActivity extends ListActivity {

	private LinkedList<String> mListItems;
	private PullToRefreshListView mPullRefreshListView;
	private ArrayAdapter<String> mAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pull_to_refresh_list);

		mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
		/**
		 * 当List刷新的时候相应该事件。
		 */
		mPullRefreshListView
				.setOnRefreshListener(new OnRefreshListener<ListView>() {
					@Override
					public void onRefresh(
							PullToRefreshBase<ListView> refreshView) {
						String label = DateUtils.formatDateTime(
								getApplicationContext(),
								System.currentTimeMillis(),
								DateUtils.FORMAT_SHOW_TIME
										| DateUtils.FORMAT_SHOW_DATE
										| DateUtils.FORMAT_ABBREV_ALL);

						/**
						 * 最后一次刷新时间。
						 */
						refreshView.getLoadingLayoutProxy()
								.setLastUpdatedLabel(label);

						/**
						 * 该方法进行刷新的工作,主要使用AsyncTask这个线程池来实现。。
						 */
						new GetDataTask().execute();
					}
				});

		/**
		 * 当最后一个item出现的时候,出现“已滑动到底部...”提示。
		 */
		mPullRefreshListView
				.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {

					@Override
					public void onLastItemVisible() {
						Toast.makeText(PullToRefreshListActivity.this,
								"已滑动到底部...", Toast.LENGTH_SHORT).show();
					}
				});

		ListView actualListView = mPullRefreshListView.getRefreshableView();

		// Need to use the Actual ListView when registering for Context Menu
		registerForContextMenu(actualListView);

		mListItems = new LinkedList<String>();
		mListItems.addAll(Arrays.asList(mStrings));

		mAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, mListItems);
		actualListView.setAdapter(mAdapter);
	}

	private class GetDataTask extends AsyncTask<Void, Void, String[]> {

		@Override
		protected String[] doInBackground(Void... params) {
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
			}
			return mStrings;
		}

		@Override
		protected void onPostExecute(String[] result) {
			mListItems.addFirst("刷新后新增数据...");
			mAdapter.notifyDataSetChanged();
			/**
			 * 刷新完成后调用该方法。
			 */
			mPullRefreshListView.onRefreshComplete();

			super.onPostExecute(result);
		}
	}
	/**
	 * ListView中数据
	 */
	private String[] mStrings = { "Abbaye de Belloc",
			"Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
			"Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu",
			"Airag", "Airedale", "Aisy Cendre", "Allgauer Emmentaler",
			"Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam",
			"Abondance", "Ackawi", "Acorn", "Adelost", "Affidelice au Chablis",
			"Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",
			"Allgauer Emmentaler" };
}

源代码下载:

点击下载源码

github完整项目下载:

点击下载源码

源地址:

点击打开链接


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

红日666

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值