2016-07-29 79 views
0

我想第一次實現Laravel分頁,我迷失在做什麼。Infinity Scroll Android和Laravel 5.2

我用下面從數據庫

$data = DB::table('news')->orderBy('created_at', 'asc')->paginate(15); 
    return $data; 

提取數據,我得到了以下

{"total":3,"per_page":15,"current_page":1,"last_page":1,"next_page_url":null,"prev_page_url":null,"from":1,"to":3,"data":[{"id":1,"news_id":"3869577","news_title":"First Title","news_content":"First", "created_at":"2016-06-06 09:20:33","updated_at":"2016-06-06 09:20:33"},{"id":2,"news_id":"11081411","news_title":"Second Title","news_content":"Second.","created_at":"2016-06-07 07:20:33","updated_at":"2016-06-07 07:20:33"},{"id":3,"news_id":"14646385","news_title":"Third Title","news_content":"Third","created_at":"2016-06-07 14:58:33","updated_at":"2016-06-07 14:58:33"}]} 

然後,我創建EndlessScrollListener實現AbsListView.OnScrollListener

在我的片段,我那麼有:

listview.setOnScrollListener(new EndlessScrollListener() { 
     @Override 
     public boolean onLoadMore(int page, int totalItemsCount) { 
      // Triggered only when new data needs to be appended to the list 
      // Add whatever code is needed to append new items to your AdapterView 
      customLoadMoreDataFromApi(page); 
      // or customLoadMoreDataFromApi(totalItemsCount); 
      return true; // ONLY if more data is actually being loaded; false otherwise. 
     } 
    }); 

然後

// Append more data into the adapter 
public void customLoadMoreDataFromApi(int offset) { 
    // This method probably sends out a network request and appends new data items to your adapter. 
    // Use the offset value and add it as a parameter to your API request to retrieve paginated data. 
    // Deserialize API response and then construct new objects to append to the adapter 
} 

問題

  1. 如何contruct下一個URL來獲取更多的數據?
  2. 我是否必須在Laravel的路線上捕獲新的網址?

感謝您的幫助

+0

打電話給你的數據讀取內部customLoadMoreDataFromApi() – DKV

+0

,我已經知道,有兩個問題,我問上面1和2 – BlackPearl

+0

任務不需要構造URL - laravle這是否爲你 - 這是下一個頁面url屬性,如果沒有下一頁,則爲空,因爲您總共有3個元素,每頁15個。 2.只需調用這個URL,laravel就可以正確地獲取它。 –

回答

0

我基本上只是要直接回答你的兩個問題

  1. Laravel給你的網址,如果你有超過15個元素。然後,您可以從您剛剛在代碼段中顯示的next_page_url參數中提取網址。它是空的,因爲你有12個元素,並且你爲15個元素設置分頁。

  2. 您不需要爲此創建新路線。該網址將調用您設置分頁的路線。分頁函數將偵聽給定的參數(...?page = 2)並會給你匹配的元素。

+0

謝謝,讓我測試並看看。 – BlackPearl