android如何使用PullToRefresh框架實現(xiàn)ListView下拉刷新上拉加載更多

這篇文章主要介紹android如何使用PullToRefresh框架實現(xiàn)ListView下拉刷新上拉加載更多,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

創(chuàng)新互聯(lián)為客戶提供專業(yè)的成都網(wǎng)站設(shè)計、成都網(wǎng)站制作、程序、域名、空間一條龍服務(wù),提供基于WEB的系統(tǒng)開發(fā). 服務(wù)項目涵蓋了網(wǎng)頁設(shè)計、網(wǎng)站程序開發(fā)、WEB系統(tǒng)開發(fā)、微信二次開發(fā)、移動網(wǎng)站建設(shè)等網(wǎng)站方面業(yè)務(wù)。

具體內(nèi)容如下

android如何使用PullToRefresh框架實現(xiàn)ListView下拉刷新上拉加載更多

其實谷歌官方目前已經(jīng)推出ListView下拉刷新框架SwipeRefreshLayout,想了解的朋友可以點擊 android使用SwipeRefreshLayout實現(xiàn)ListView下拉刷新上拉加載 了解一下;

大家不難發(fā)現(xiàn)當你使用SwipeRefreshLayout下拉的時候布局文件不會跟著手勢往下滑,而且想要更改這個缺陷好像非常不容易。

雖然SwipeRefreshLayout非常簡單易懂,但是需求需要下拉刷新的時候跟著手勢下滑就不能用SwipeRefreshLayout了;

上面圖片效果使用的是PullToRefresh框架,在我的工程里面沒有導入類庫和jar包,而是把下拉刷新功能直接抽取出來使用;
當下拉的時候回調(diào)監(jiān)聽,在抽取完下拉刷新功能的基礎(chǔ)上實現(xiàn)上拉加載更多功能實現(xiàn)也非常簡單,所以順手寫上了;
我是從github上下載的Android-PullToRefresh-master框架,在library中抽取的;
首先需要復(fù)制的類大概有十個左右:

android如何使用PullToRefresh框架實現(xiàn)ListView下拉刷新上拉加載更多

然后跟進報錯查看需要什么文件就復(fù)制什么文件;把錯誤搞定之后首先來看下布局:

<LinearLayout 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:orientation="vertical"> 
 
 <!-- 我們添加了一個屬性:ptr:ptrMode="both" ,意思:上拉和下拉都支持。 
  可選值為:disabled(禁用下拉刷新),pullFromStart(僅支持下拉刷新), 
  pullFromEnd(僅支持上拉刷新),both(二者都支持),manualOnly(只允許手動觸發(fā)) --> 
 <!-- 
  ptr:ptrAnimationStyle="rotate" 
  FlipLoadingLayout為iOS風格的箭頭顛倒的刷新動畫 
  ptr:ptrAnimationStyle="flip" 
  RotateLoadingLayout為android風格的圖片旋轉(zhuǎn)動畫 --> 
 <com.ptrflv.www.pulltorefreshlistview.PullToRefreshListView 
  xmlns:ptr="http://schemas.android.com/apk/res-auto" 
  android:id="@+id/pull_to_refresh_listview" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  ptr:ptrMode="both" 
  ptr:ptrAnimationStyle="flip" 
   /> 
 
</LinearLayout>

 值得注意的是默認情況下下拉刷新的執(zhí)行動畫中顯示的文本是英文,這里我們需要手動修改pull_refresh_strings.xml中的內(nèi)容:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
 
 <!-- 上拉刷新 --> 
 <!-- …代表三個點 ... --> 
 <string name="pull_to_refresh_pull_label">向下拉刷新…</string> 
 <string name="pull_to_refresh_release_label">松開更新…</string> 
 <string name="pull_to_refresh_refreshing_label">正在加載…</string> 
 
 <!-- 下拉加載更多 --> 
 <string name="pull_to_refresh_from_bottom_pull_label">向下拉加載更多…</string> 
 <string name="pull_to_refresh_from_bottom_release_label">松開加載更多…</string> 
 <string name="pull_to_refresh_from_bottom_refreshing_label">正在加載…</string> 
 
</resources>

下面是調(diào)用下拉刷新和上下加載更多的代碼:

public class MainActivity extends Activity { 
 
 private PullToRefreshListView pullToRefreshListView; 
 //adapter的數(shù)據(jù)源 
 private List<String> numList=new ArrayList<String>(); 
 private ArrayAdapter<String> arrayAdapter; 
  
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.activity_main); 
  pullToRefreshListView=(PullToRefreshListView) findViewById(R.id.pull_to_refresh_listview); 
  //初始化數(shù)據(jù) 
  for(int x=0;x<18;x++){ 
   numList.add(""+x); 
  } 
   
  arrayAdapter = new ArrayAdapter<String>(this, R.layout.item_listview,R.id.textview,numList); 
  pullToRefreshListView.setAdapter(arrayAdapter); 
   
  //設(shè)定刷新監(jiān)聽 
  pullToRefreshListView.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); 
     
     //代表下拉刷新 
     if(refreshView.getHeaderLayout().isShown()){ 
      
      new Thread(){ 
       public void run() { 
        try { 
         sleep(1000); 
         
         handler.sendEmptyMessage(99); 
         
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 
       }; 
      }.start(); 
     } 
     
     //代表下拉刷新 
     if(refreshView.getFooterLayout().isShown()){ 
      new Thread(){ 
       public void run() { 
        try { 
         sleep(1000); 
         
         handler.sendEmptyMessage(98); 
         
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 
       }; 
      }.start(); 
     } 
     
   } 
  }); 
   
 } 
  
 private Handler handler=new Handler(){ 
  public void handleMessage(android.os.Message msg) { 
    
   if(msg.what==99){ 
    numList.add(0, "英雄聯(lián)盟"); 
    arrayAdapter.notifyDataSetChanged(); 
    //關(guān)閉刷新的動畫 
    pullToRefreshListView.onRefreshComplete(); 
   } 
    
   if(msg.what==98){ 
    numList.add(numList.size(), "魔獸世界"); 
    arrayAdapter.notifyDataSetChanged(); 
    //關(guān)閉刷新的動畫 
    pullToRefreshListView.onRefreshComplete(); 
   } 
    
  }; 
 }; 
 
}

在判斷上拉刷新和下拉加載的時候
refreshView.getFooterLayout().isShown()
refreshView.getHeaderLayout().isShown()會報錯,因為PullToRefreshBase這兩個方法默認不是共有方法,我們需要手動該更為public

以上是“android如何使用PullToRefresh框架實現(xiàn)ListView下拉刷新上拉加載更多”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

當前標題:android如何使用PullToRefresh框架實現(xiàn)ListView下拉刷新上拉加載更多
分享路徑:http://bm7419.com/article16/jddpdg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、用戶體驗、搜索引擎優(yōu)化網(wǎng)站營銷、小程序開發(fā)網(wǎng)站內(nèi)鏈

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)