标签:pulltorefresh pulltorefresh用法 pulltorefresh怎么用 pulltorefresh 上提 上拉 android 下拉刷新
转载请注明:http://blog.csdn.net/duguang77/article/details/40921601
PullToRefresh控件下载地址:https://github.com/chrisbanes/Android-PullToRefresh
作者:https://github.com/chrisbanes
百度网盘下载地址:http://pan.baidu.com/s/1o6umifw
1.下载后将extras,library,sample导入到Eclipse中
2.导入后时可能会报错,请更改依赖包位置
<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" tools:context="com.example.testpulltoreresh.MainActivity" > <com.handmark.pulltorefresh.library.PullToRefreshListView android:id="@+id/pl_refresh" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
mPullList.setOnRefreshListener(new OnRefreshListener2() { @Override public void onPullDownToRefresh(PullToRefreshBase refreshView) { // 下拉刷新触发的事件 //获取格式化的时间 String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL); // 更新LastUpdatedLabel refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label); ///开启线程模拟调接口填充数据 new GetDataTask().execute(); } @Override public void onPullUpToRefresh(PullToRefreshBase refreshView) { // 上提加载触发的事件 ///开启线程模拟调接口填充数据 new GetDataTask().execute(); } });
// 设置PullToRefreshListView的模式 mPullList.setMode(Mode.BOTH); // 设置PullRefreshListView上提加载时的加载提示 mPullList.setMode(Mode.BOTH); mPullList.getLoadingLayoutProxy(false, true).setPullLabel("上拉加载..."); mPullList.getLoadingLayoutProxy(false, true).setRefreshingLabel("正在加载..."); mPullList.getLoadingLayoutProxy(false, true).setReleaseLabel("松开加载更多..."); // 设置PullRefreshListView下拉加载时的加载提示 mPullList.getLoadingLayoutProxy(true, false).setPullLabel("下拉刷新..."); mPullList.getLoadingLayoutProxy(true, false).setRefreshingLabel("正在加载..."); mPullList.getLoadingLayoutProxy(true, false).setReleaseLabel("松开加载更多...");
public class MainActivity extends Activity { private PullToRefreshListView mPullList; private LinkedList<String> mListItems; private ArrayAdapter<String> mAdapter; private String[] mStrings = { "我很善良", "我很温柔", "我是淘女郎", "我是阿里郎", "我是大灰狼","我是羊羊羊" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); setEventListener(); initData(); } /** * 初始化控件 */ private void initView() { mPullList = (PullToRefreshListView) findViewById(R.id.pl_refresh); } /** * 设置监听 */ private void setEventListener() { mPullList.setOnRefreshListener(new OnRefreshListener2() { @Override public void onPullDownToRefresh(PullToRefreshBase refreshView) { // 下拉刷新触发的事件 //获取格式化的时间 String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL); // 更新LastUpdatedLabel refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label); ///开启线程模拟调接口填充数据 new GetDataTask().execute(); } @Override public void onPullUpToRefresh(PullToRefreshBase refreshView) { // 上提加载触发的事件 ///开启线程模拟调接口填充数据 new GetDataTask().execute(); } }); } /** * 初始化数据 */ private void initData() { // 设置PullToRefreshListView的模式 mPullList.setMode(Mode.BOTH); // 设置PullRefreshListView上提加载时的加载提示 mPullList.setMode(Mode.BOTH); mPullList.getLoadingLayoutProxy(false, true).setPullLabel("上拉加载..."); mPullList.getLoadingLayoutProxy(false, true).setRefreshingLabel("正在加载..."); mPullList.getLoadingLayoutProxy(false, true).setReleaseLabel("松开加载更多..."); // 设置PullRefreshListView下拉加载时的加载提示 mPullList.getLoadingLayoutProxy(true, false).setPullLabel("下拉刷新..."); mPullList.getLoadingLayoutProxy(true, false).setRefreshingLabel("正在加载..."); mPullList.getLoadingLayoutProxy(true, false).setReleaseLabel("松开加载更多..."); mListItems = new LinkedList<String>(); mListItems.addAll(Arrays.asList(mStrings)); mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListItems); mPullList.setAdapter(mAdapter); } private class GetDataTask extends AsyncTask<Void, Void, String[]> { @Override protected String[] doInBackground(Void... params) { try { Thread.sleep(2000); } catch (InterruptedException e) { } return null; } @Override protected void onPostExecute(String[] result) { if(mPullList.isHeaderShown()){ mListItems.addFirst("我是新纳入的妾——下拉刷新"); }else if(mPullList.isFooterShown()){ mListItems.addLast("我是让你重回怀抱的妾——上提加载"); } mAdapter.notifyDataSetChanged(); // 调用刷新完成 mPullList.onRefreshComplete(); super.onPostExecute(result); } } }
【PullToRefresh 系列一】 Android上拉加载下拉刷新控件详解
标签:pulltorefresh pulltorefresh用法 pulltorefresh怎么用 pulltorefresh 上提 上拉 android 下拉刷新
原文地址:http://blog.csdn.net/duguang77/article/details/40921601