一、前言:
尊重原创,转载请注明:From zsml2016(http://blog.csdn.net/qq_29269233)Power byzsml2016侵权必究!
目前刷新控件很多,前几天做项目时又用到了PullToRefresh,虽然有点旧了,但还好,所以今天我要简单总结一下PullToRefresh这个开源框架了,我觉得还不错的,用起来比较简单方便!
二、PullToRefresh是什么呢?
PullToRefresh是一套实现非常好的下拉刷新框架,它PullToRefresh是一套实现非常好的下拉刷新库,如下图所示它支持:
ListView、ExpandableListView、GridView、WebView、ScrollView、HorizontalScrollView、ViewPager等多种常用的需要刷新的View类型,而且使用起来也十分方便。
三、PullToRefresh库的下载:
PullToRefresh是github上的开源框架,其地址为:https://github.com/chrisbanes/Android-PullToRefresh,大家可以去下载jar包和代码例子;为了方便,我已经把jar包和sample上传到CSDN了,所以也可以到我的博客里下载:http://download.csdn.net/detail/qq_29269233/9703670
四、导入PullToRefresh库:
把库下载下来,解压打开如下图:
library为jar包,sample为案例,当然案例里面也包含了每个view控件刷新的代码实现;在此我建议大家直接导入sample,结合代码案例简单方便易懂!至于eclipse、studio导入库的方法都是家常便饭了,在此就不总结了!直接套用实现了,代码可以参考sample,还可以自己添加一些sample没有的功能实现。
五、用PullToRefreshListView实现ListView下拉刷新:
1、创建布局文件activity_ptr_list.xml
xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:chao="http://schemas.android.com/apk/res-auto" android:orientation="vertical"> layout="@layout/titlebar"/> 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" chao:ptrMode="both" />
2、具体代码如下:
public final class PullToRefreshListActivity extends ListActivity { /** * 装数据 */ private LinkedListmListItems; private PullToRefreshListView mPullRefreshListView; private ArrayAdapter mAdapter; private TextView tv_title; 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"}; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_ptr_list); tv_title = (TextView) findViewById(R.id.tv_title); tv_title.setText("PullToRefreshListView"); //实例化控件 mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list); // Set a listener to be invoked when the list should be refreshed. //设置下拉刷新的监听 // mPullRefreshListView.setOnRefreshListener(new OnRefreshListener () { // @Override // public void onRefresh(PullToRefreshBaserefreshView) { // //得到当前刷新的时间 // String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(), // DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL); // // // Update the LastUpdatedLabel // //设置更新时间 // refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label); // // // Do work to refresh the list here. // new GetDataTask().execute(); // } // }); // Set a listener to be invoked when the list should be refreshed. mPullRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() { /** * 下拉刷新 * @param refreshView */ @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); // Update the LastUpdatedLabel //设置更新时间 refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label); Toast.makeText(PullToRefreshListActivity.this, "下拉刷新", Toast.LENGTH_SHORT).show(); new GetDataTask().execute(); } /** * 上拉刷新 * @param refreshView */ @Override public void onPullUpToRefresh(PullToRefreshBase refreshView) { //得到当前刷新的时间 String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL); // Update the LastUpdatedLabel //设置更新时间 refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label); Toast.makeText(PullToRefreshListActivity.this, "上拉刷新!", Toast.LENGTH_SHORT).show(); new GetDataTask().execute(); } }); // Add an end-of-list listener //设置监听最后一条 mPullRefreshListView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() { @Override public void onLastItemVisible() { Toast.makeText(PullToRefreshListActivity.this, "滑动到最后一条了!", Toast.LENGTH_SHORT).show(); } }); //得到ListView ListView listview = mPullRefreshListView.getRefreshableView(); mListItems = new LinkedList (); mListItems.addAll(Arrays.asList(mStrings)); //创建适配器 mAdapter = new ArrayAdapter (this, android.R.layout.simple_list_item_1, mListItems); /** * Add Sound Event Listener * 添加刷新事件并且发出声音 */ SoundPullEventListener soundListener = new SoundPullEventListener (this); soundListener.addSoundEvent(State.PULL_TO_REFRESH, R.raw.pull_event); soundListener.addSoundEvent(State.RESET, R.raw.reset_sound); soundListener.addSoundEvent(State.REFRESHING, R.raw.refreshing_sound); mPullRefreshListView.setOnPullEventListener(soundListener); // You can also just use setListAdapter(mAdapter) or // mPullRefreshListView.setAdapter(mAdapter) //设置适配器 listview.setAdapter(mAdapter); //设置上拉刷新或者下拉刷新 // mPullRefreshListView.setMode(PullToRefreshBase.Mode.PULL_FROM_END); // mPullRefreshListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START); } private class GetDataTask extends AsyncTask , Void, String[]> { @Override protected String[] doInBackground(Void... params) { // Simulates a background job. try { Thread.sleep(2000); } catch (InterruptedException e) { } return mStrings; } @Override protected void onPostExecute(String[] result) { if(mPullRefreshListView.getMode()== PullToRefreshBase.Mode.PULL_FROM_START){ //下拉刷新 mListItems.addFirst("刷新请求到的新数据..."); }else if(mPullRefreshListView.getMode()==PullToRefreshBase.Mode.PULL_FROM_END){ mListItems.addLast("上拉数据请求到了..."); } mAdapter.notifyDataSetChanged(); // Call onRefreshComplete when the list has been refreshed. mPullRefreshListView.onRefreshComplete(); super.onPostExecute(result); } } }
3、是不是很简单呀,运行效果如下:
六、WebView实现下拉刷新
1、创建布局文件:
xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > layout="@layout/titlebar"/> xmlns:ptr="http://schemas.android.com/apk/res-auto" android:id="@+id/pull_refresh_webview" android:layout_height="fill_parent" android:layout_width="fill_parent" ptr:ptrMode="both" />
2、具体代码实现:
public final class PullToRefreshWebViewActivity extends Activity { PullToRefreshWebView mPullRefreshWebView; WebView mWebView; private TextView tv_title; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_ptr_webview); mPullRefreshWebView = (PullToRefreshWebView) findViewById(R.id.pull_refresh_webview); mWebView = mPullRefreshWebView.getRefreshableView(); tv_title = (TextView) findViewById(R.id.tv_title); tv_title.setText("PullToRefreshWebView"); mWebView.getSettings().setJavaScriptEnabled(true); //设置WebViewClient mWebView.setWebViewClient(new SampleWebViewClient()); mWebView.loadUrl("http://blog.csdn.net/qq_29269233"); } private static class SampleWebViewClient extends WebViewClient { // @Override // public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { // view.loadUrl(request.getUrl().toString()); // return true; // } @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } } }
3、一个简单的浏览器很容易地实现了,而且还支持下拉刷新,上拉加载,我觉得挺方便简洁的,好吧效果如下:
至此,我已经总结了listview和webview的简单快速地集成下拉刷新了,至于其他view控件的刷新也是大同小异了,希望大家能够参照sample的例子,自己尝试去使用,相信你们也可以的,Come,on !
更多精彩内容请关注我的博客:luoweichao.top