Android项目:使用pulltorefresh开源项目扩展为下拉刷新上拉加载更多的处理方法,监听listview滚动方向...
很多android應用的下拉刷新都是使用的pulltorefresh這個開源項目,但是它的擴展性在下拉刷新同時又上拉加載更多時會有一定的局限性。查了很多地方,發現這個開源項目并不能很好的同時支持下拉刷新和上拉加載更多。這個組件有個mode的屬性,可以設置為both,即上下同時都可拉動。但是只設置這個屬性的話,上拉與下拉產生的效果是完全一致的。所以要使用這個開源項目做到下拉刷新并同時可上拉加載更多,就需要在代碼中進行一些處理。
==========================pulltorefresh屬性相關=====================================
開源項目:Android-PullToRefresh
項目地址:https://github.com/chrisbanes/Android-PullToRefresh/wiki/Quick-Start-Guide
1.屬性:https://github.com/chrisbanes/Android-PullToRefresh/blob/master/library/res/values/attrs.xml
命名空間: xmlns:ptr="http://schemas.android.com/apk/res-auto"
| ptr:ptrAnimationStyle | 動畫效果 提供了兩個值 flip和rotate 默認為rotate |
| ptr:ptrRefreshableViewBackground | 設置刷新View的背景顏色 |
| ptr:ptrHeaderBackground | 設置頭部View的背景顏色 |
| ptr:ptrHeaderTextColor | 設置頭部View文字的顏色 |
| ptr:ptrHeaderSubTextColor | 設置頭部view副標題文字的顏色 |
| ptr:ptrMode | pullFromStart: pullFromEnd: both; |
2.常用方法
setOnRefreshListener(OnRefreshListener listener):設置刷新監聽器;
setOnLastItemVisibleListener(OnLastItemVisibleListener listener):設置是否到底部監聽器;
setOnPullEventListener(OnPullEventListener listener);設置事件監聽器;
onRefreshComplete():設置刷新完成
==========================監聽listview滾動方向=====================================
修改為上拉加載更多的關鍵在于onrefresh方法執行之前判斷出listview的滾動方向。以下方法是所嘗試的方法中效果最好的一種,并不能說完美解決,但應該是效果最接近的一種了。(當首屏數據行數未充滿屏幕,或者滾動時第一行的滾動距離小于行高,可能還是會存在一點誤差。不過大部分應用的列表每頁數據一般都能充滿屏幕,也可在此基礎結合其他手勢判斷對此方法進行改善)。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | int?mLastFirstVisibleItem?=?0; boolean?mIsScrollingUp; @Override public?void?onScrollStateChanged(AbsListView?view,?int?scrollState) { ?????????????????????????????????????????????????????????????????????????????????????????????????????? ????if?(view.getId()?==?mListView.getId()) ????{ ????????final?int?currentFirstVisibleItem?=?mListView.getFirstVisiblePosition(); ?????????????????????????????????????????????????????????????????????????????????????????????????????????? ????????if?(currentFirstVisibleItem?>?mLastFirstVisibleItem) ????????{ ????????????mIsUp?=?true; ????????} ????????else?if?(currentFirstVisibleItem?<?mLastFirstVisibleItem) ????????{ ????????????mIsUp?=?false; ????????} ????????mLastFirstVisibleItem?=?currentFirstVisibleItem; ????} ?????????????????????????????????????????????????????????????????????????????????????????????????????? } |
參考資料:http://stackoverflow.com/questions/12114963/detecting-the-scrolling-direction-in-the-adapter-up-down/12115157#12115157
==========================實現下拉刷新和上拉加載更多====================================
解析json完畢后,判斷是上拉操作還是下拉刷新操作:
| 1 2 3 4 5 6 7 8 9 10 11 | //?解析json private?void?parseJson(String?result) { ????List<ListJson>?localList?=?parseJsonArray(Utils.parseListJson(result,?"key")); ?????????????????????????????????????????????????????????? ????if(!mIsUp) ????{ ????????mDataList.clear(); ????} ????mDataList.addAll(localList); } |
數據加載完畢后,notifyDataSetChanged和通知PullRefreshListView,同時頁碼加1:
| 1 2 3 4 5 6 7 | //?加載完畢處理 private?void?loadComplete() { ????mPullRefreshListView.onRefreshComplete(); ????mAdapter.notifyDataSetChanged(); ????mPage?+=?1; } |
判斷上拉和下拉方向,監聽刷新listview,修改頭部和底部view的文字說明:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | /** ?*?*******************下拉刷新與上拉加載的監聽處理************************ ?*/ //?刷新listview監聽 @Override public?void?onRefresh(PullToRefreshBase<ListView>?refreshView) { ????//?獲取刷新時間,設置刷新時間格式 ????String?str?=?DateUtils.formatDateTime(getActivity(),?System.currentTimeMillis(),?DateUtils.FORMAT_NUMERIC_DATE?|?DateUtils.FORMAT_NO_NOON); ?????????????????? ????//?判斷下拉還是上拉 ????if?(!mIsUp) ????????mPage?=?0; ?????????????????? ????//?設置刷新文本說明(刷新過程中) ????if?(mIsUp) ????{ ????????mPullRefreshListView.getLoadingLayoutProxy().setRefreshingLabel("正在加載"); ????????mPullRefreshListView.getLoadingLayoutProxy().setPullLabel("上拉加載更多"); ????????mPullRefreshListView.getLoadingLayoutProxy().setReleaseLabel("釋放開始加載"); ????????refreshView.getLoadingLayoutProxy().setLastUpdatedLabel("最后加載時間:"?+?str); ????} ????else ????{ ????????mPullRefreshListView.getLoadingLayoutProxy().setRefreshingLabel("正在刷新"); ????????mPullRefreshListView.getLoadingLayoutProxy().setPullLabel("下拉刷新"); ????????mPullRefreshListView.getLoadingLayoutProxy().setReleaseLabel("釋放開始刷新"); ????????refreshView.getLoadingLayoutProxy().setLastUpdatedLabel("最后更新時間:"?+?str); ????} ?????????????????? ????//?啟動下載任務,加載數據 ????loadTask(); } int?mLastFirstVisibleItem?=?0; boolean?mIsScrollingUp; @Override public?void?onScrollStateChanged(AbsListView?view,?int?scrollState) { ?????????????????? ????if?(view.getId()?==?mListView.getId()) ????{ ????????final?int?currentFirstVisibleItem?=?mListView.getFirstVisiblePosition(); ?????????????????????? ????????if?(currentFirstVisibleItem?>?mLastFirstVisibleItem) ????????{ ????????????mIsUp?=?true; ????????} ????????else?if?(currentFirstVisibleItem?<?mLastFirstVisibleItem) ????????{ ????????????mIsUp?=?false; ????????} ????????mLastFirstVisibleItem?=?currentFirstVisibleItem; ????} ?????????????????? } @Override public?void?onScroll(AbsListView?view,?int?firstVisibleItem,?int?visibleItemCount,?int?totalItemCount) { ????//?設置刷新文本說明(展開刷新欄前) ????if?(mIsUp) ????{ ????????mPullRefreshListView.getLoadingLayoutProxy().setRefreshingLabel("正在加載"); ????????mPullRefreshListView.getLoadingLayoutProxy().setPullLabel("上拉加載更多"); ????????mPullRefreshListView.getLoadingLayoutProxy().setReleaseLabel("釋放開始加載"); ????} ????else ????{ ????????mPullRefreshListView.getLoadingLayoutProxy().setRefreshingLabel("正在刷新"); ????????mPullRefreshListView.getLoadingLayoutProxy().setPullLabel("下拉刷新"); ????????mPullRefreshListView.getLoadingLayoutProxy().setReleaseLabel("釋放開始刷新"); ????} ?????????????????? } @Override public?void?onLastItemVisible() { ????mIsUp?=?true; } |
==========================其他使用筆記====================================
1.PullRefreshListView.setRefreshing()方法無法執行更新
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | @Override ????protected?void?onRefreshing(final?boolean?doScroll)?{ ????????/** ?????????*?If?we're?not?showing?the?Refreshing?view,?or?the?list?is?empty,?the ?????????*?the?header/footer?views?won't?show?so?we?use?the?normal?method. ?????????*/ ????????/*ListAdapter?adapter?=?mRefreshableView.getAdapter(); ????????if?(!mListViewExtrasEnabled?||?!getShowViewWhileRefreshing()?||?null?==?adapter?||?adapter.isEmpty())?{ ????????????super.onRefreshing(doScroll); ????????????return; ????????}*/ ????????super.onRefreshing(false); ????????} ????} |
mPullRefreshListView.setRefreshing();如果adapter是里面的size是null 或者大小是0
這個時候 setRefreshing是不會有效果的.需要將PullToRefreshListView上面代碼注釋掉
2.通過繼承OnRefreshListener2<ListView>直接實現上拉與下拉監聽,但是下拉窗口的文字無法及時更新,即方法只在下拉操作后才執行。
| 1 2 3 4 5 6 7 8 9 | ????????mPullRefreshListView?=?(PullToRefreshListView)?findViewById(R.id.refresh_listview); ????????mListView?=?((ListView)?this.mPullRefreshListView.getRefreshableView()); ????????? ????????//?監聽listview ????????mPullRefreshListView.setOnRefreshListener(this); ????????//?mPullRefreshListView.setOnRefreshListener(this); ????????//?mPullRefreshListView.setOnLastItemVisibleListener(this); ????????//?mPullRefreshListView.setOnScrollListener(this); ?????????mPullRefreshListView.setOnItemClickListener(this); |
相應接口方法:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | ????@Override ????public?void?onPullDownToRefresh(PullToRefreshBase<ListView>?refreshView) ????{ ????????//?獲取刷新時間 ????????String?str?=?DateUtils.formatDateTime(this,?System.currentTimeMillis(),?DateUtils.FORMAT_NUMERIC_DATE?|?DateUtils.FORMAT_NO_NOON); ????????? ????????//?設置刷新文本說明(刷新過程中) ????????mPullRefreshListView.getLoadingLayoutProxy().setRefreshingLabel("正在刷新"); ????????mPullRefreshListView.getLoadingLayoutProxy().setPullLabel("下拉刷新"); ????????mPullRefreshListView.getLoadingLayoutProxy().setReleaseLabel("釋放開始刷新"); ????????refreshView.getLoadingLayoutProxy().setLastUpdatedLabel("最后更新時間:"?+?str); ????????? ????????onPullDownListView(); ????} ????? ????public?void?onPullDownListView() ????{ ????????//?TODO?Auto-generated?method?stub ????????? ????} ????@Override ????public?void?onPullUpToRefresh(PullToRefreshBase<ListView>?refreshView) ????{ ????????//?獲取刷新時間 ????????String?str?=?DateUtils.formatDateTime(this,?System.currentTimeMillis(),?DateUtils.FORMAT_NUMERIC_DATE?|?DateUtils.FORMAT_NO_NOON); ????????? ????????//?設置刷新文本說明(刷新過程中) ????????mPullRefreshListView.getLoadingLayoutProxy().setRefreshingLabel("正在加載"); ????????mPullRefreshListView.getLoadingLayoutProxy().setPullLabel("上拉加載更多"); ????????mPullRefreshListView.getLoadingLayoutProxy().setReleaseLabel("釋放開始加載"); ????????refreshView.getLoadingLayoutProxy().setLastUpdatedLabel("最后加載時間:"?+?str); ????????? ????????onPullUpListView(); ????} ????public?void?onPullUpListView() ????{ ????????//?TODO?Auto-generated?method?stub ????????? ????} |
本文轉自 glblong 51CTO博客,原文鏈接:http://blog.51cto.com/glblong/1312349,如需轉載請自行聯系原作者
總結
以上是生活随笔為你收集整理的Android项目:使用pulltorefresh开源项目扩展为下拉刷新上拉加载更多的处理方法,监听listview滚动方向...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 11.18-11.19总结(transf
- 下一篇: 入职五年回顾(十六) 2013年11月