一个简单的下拉刷新案例,很实用。
1、首先要在项目中加入library_PullToRefresh的库或者jar包。(文章后面提供资源及demo的链接)
2、在layout中使用PullToRefreshListView控件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/pull_to_refresh_listview"
android:layout_height="fill_parent"
android:layout_width="fill_parent" />
</RelativeLayout>
3、Activity中使用
package com.rainsong.pulltorefreshdemo;
import java.util.Arrays;
import java.util.LinkedList;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
public class MainActivity extends Activity {
private PullToRefreshListView mPullToRefreshListView;
private LinkedList<String> mListItems;
private ArrayAdapter<String> mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 设置一个监听器调用时刷新列表
mPullToRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_to_refresh_listview);
mPullToRefreshListView.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);
// 在此处刷新列表
new GetDataTask().execute();
}
});
ListView actualListView = mPullToRefreshListView.getRefreshableView();
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(4000);
} catch (InterruptedException e) {
}
return mStrings;
}
@Override
protected void onPostExecute(String[] result) {
mListItems.addFirst("新增手机");
mAdapter.notifyDataSetChanged();
// 刷新列表
mPullToRefreshListView.onRefreshComplete();
super.onPostExecute(result);
}
}
private String[] mStrings = { "华为", "小米", "魅族", "锤子", "oppo",
"vivo", "乐视", "三星", "诺基亚", "酷派", "努比亚", "中兴",
"联想", "360", "美图", "一加", "摩托罗拉", "苹果"};
}
4、效果展示
5、资源下载