Android--PullToRefreshListView 的簡單使用,androidpulltorefresh

來源:互聯網
上載者:User

Android--PullToRefreshListView 的簡單使用,androidpulltorefresh

  原文:  http://blog.csdn.net/lmj623565791/article/details/38238749;

pull-to-refresh對ListView進行了封裝,叫做:PullToRefreshListView,用法和listview沒什麼區別,下面看demo.

<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" >    <com.handmark.pulltorefresh.library.PullToRefreshListView        xmlns:ptr="http://schemas.android.com/apk/res-auto"        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" >    </com.handmark.pulltorefresh.library.PullToRefreshListView></RelativeLayout>
代碼極其簡單,得到PullToRefreshListView控制項,然後像ListView一樣設定資料集。當然了,我們有下拉重新整理,所以必須設定下拉重新整理的回調:

 

setOnRefreshListener(new OnRefreshListener<ListView>(){}

 // 設定監聽事件          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);                            // 類比載入任務                          new GetDataTask().execute();                      }                  });  

 

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" >    <com.handmark.pulltorefresh.library.PullToRefreshListView        xmlns:ptr="http://schemas.android.com/apk/res-auto"        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"        ptr:ptrMode="both" >    </com.handmark.pulltorefresh.library.PullToRefreshListView></RelativeLayout>

我們添加了一個屬性:ptr:ptrMode="both" ,意思:上拉和下拉都支援。

可選值為:disabled(禁用下拉重新整理),pullFromStart(僅支援下拉重新整理),pullFromEnd(僅支援上拉重新整理),both(二者都支援),manualOnly(只允許手動觸發)

當然了,如果你不喜歡在布局檔案中指定,完全可以使用代碼設定,在onCreate裡面寫:mPullRefreshListView.setMode(Mode.BOTH);//設定你需要的模式

 

   mPullRefreshListView.setOnRefreshListener(new OnRefreshListener2<ListView>(){});注意這裡的介面類型是OnRefreshListener2,多了個2,和上面的不一樣,這個介面包含兩個方法,一個上拉回調,一個下拉回調。好了,這樣我們就成功添加了上拉與下拉,並且分別可以控制其回調代碼。

mPullRefreshListView                  .setOnRefreshListener(new OnRefreshListener2<ListView>()                  {                      @Override                      public void onPullDownToRefresh(                              PullToRefreshBase<ListView> refreshView)                      {                          Log.e("TAG", "onPullDownToRefresh");                          //這裡寫下拉重新整理的任務                          new GetDataTask().execute();                      }                        @Override                      public void onPullUpToRefresh(                              PullToRefreshBase<ListView> refreshView)                      {                          Log.e("TAG", "onPullUpToRefresh");                          //這裡寫上拉載入更多的任務                          new GetDataTask().execute();                      }                  });  

 

3、設定下拉重新整理的表徵圖

 <com.handmark.pulltorefresh.library.PullToRefreshGridView        ptr:ptrDrawable="@drawable/ic_launcher"        ...        />

當然了這是旋轉的效果,一般常用的還有,一個箭頭倒置的效果,其實也很簡單,一個屬性:

ptr:ptrAnimationStyle="flip"

去掉 ptr:ptrDrawable="@drawable/ic_launcher"這個屬性,如果你希望用預設的箭頭,你也可以自訂。

ptr:ptrAnimationStyle的取值:flip(翻轉動畫), rotate(旋轉動畫) 。 

ptr:ptrDrawable則就是設定表徵圖了。

  

4、自訂下拉指標常值內容等效果

可以在初始化完成mPullRefreshListView後,通過mPullRefreshListView.getLoadingLayoutProxy()可以得到一個ILoadingLayout對象,這個對象可以設定各種指標中的樣式、文本等。

ILoadingLayout startLabels = mPullRefreshListView                .getLoadingLayoutProxy();        startLabels.setPullLabel("你可勁拉,拉...");// 剛下拉時,顯示的提示        startLabels.setRefreshingLabel("好嘞,正在重新整理...");// 重新整理時        startLabels.setReleaseLabel("你敢放,我就敢重新整理...");// 下來達到一定距離時,顯示的提示

 設定上次重新整理時間:

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);  
5、常用的一些屬性

當然了,pull-to-refresh在xml中還能定義一些屬性:

ptrMode,ptrDrawable,ptrAnimationStyle這三個上面已經介紹過。

ptrRefreshableViewBackground 設定整個mPullRefreshListView的背景色

ptrHeaderBackground 設定下拉Header或者上拉Footer的背景色

ptrHeaderTextColor 用於設定Header與Footer中文本的顏色

ptrHeaderSubTextColor 用於設定Header與Footer中上次重新整理時間的顏色

ptrShowIndicator如果為true會在mPullRefreshListView中出現icon,右上方和右下角,挺有意思的。

ptrHeaderTextAppearance , ptrSubHeaderTextAppearance分別設定拉Header或者上拉Footer中字型的類型顏色等等。

ptrRotateDrawableWhilePulling當動畫設定為rotate時,下拉是是否旋轉。

ptrScrollingWhileRefreshingEnabled重新整理的時候,是否允許ListView或GridView滾動。覺得為true比較好。

ptrListViewExtrasEnabled 決定了Header,Footer以何種方式加入mPullRefreshListView,true為headView方式加入,就是滾動時重新整理頭部會一起滾動。

最後2個其實對於使用者體驗還是挺重要的,如果設定的時候考慮下~。其他的屬性自己選擇就好。

註:上述屬性很多都可以代碼控制,如果有需要可以直接mPullRefreshListView.set屬性名稱 查看

以上為pull-to-refresh所有支援的屬性~~

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.