2016-06-30 18 views
0

我目前正在創建一個聊天應用程序。我嘗試了不同的解決方案,但無法讓他們工作。這是我目前的代碼:如何限制ListAdapter中的數據並在滾動時顯示更多內容?

public void callListView() { 
    // GET USER LIST 
    setListAdapter (new SimpleAdapter(
     Messages.this, ListOfMsg, 
     R.layout.messages_list_item, new String[] { 
      "name", 
      "message" 
     }, 
     new int[] { 
      R.id.sname, 
      R.id.message 
     } 
    )); 
} 

我該如何限制數據只能獲得25個項目?當我滾動時,我將如何獲取更多數據?

在此先感謝。

+3

[無盡的滾動列表視圖的可能的複製](http://stackoverflow.com/questions/20324258/endless-scroll-list-view) –

+0

只需先獲取25個項目,然後獲取new ite毫秒,當你達到底部。 – Zoedia

+0

已經在這裏回答http://stackoverflow.com/questions/16398921/dynamic-listview-adding-load-more-items-at-the-end-of-scroll –

回答

0
int totalItems = 100; 
int currentPage = 0; 
int pageSize = 10; 
int numPages = (int) Math.ceil((float) totalItems/pageSize); 

ArrayList<String> items = new ArrayList<String>(totalItems); 

List<String> page = items.subList(currentPage, pageSize); 
+2

添加一個解釋或評論你的代碼 – LaurentY

0

您可以使用下面的查詢訪問數據的有限

SELECT column1, column2, columnN 
FROM table_name 
LIMIT [no of rows] OFFSET [row num] 

然後在

listView.setOnScrollListener 

在滾動顯示方式

public void onScroll(AbsListView view, int firstVisibleItem, 
         int visibleItemCount, int totalItemCount) { 

    if (firstVisibleItem + visibleItemCount == totalItemCount && totalItemCount != 0) { 

//here you can fire query to get next data and attached to listview 
} 
} 

你可以從上面的查詢獲取下一個數據和添加到列表視圖像paginatio ñ。

0

我用RecyclerView OnScrollListener。

LinearLayoutManager llm; 
ArrayList posts; 

然後輸入:

RecyclerView.OnScrollListener scrollListener = new RecyclerView.OnScrollListener() { 
    @Override 
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) { 
     if (!loading && llm.getChildCount() != 0 && 
      llm.findFirstCompletelyVisibleItemPosition() !=0 && //to ensure that the last item and the first item are not both visible 
      llm.findLastVisibleItemPosition() == posts.size() - 1) { 

        //Load Here Your Next Twenty Five Chat 
     } 
     super.onScrollStateChanged(recyclerView, newState); 
    } 

}; 

聽者然後添加到RecyclerView如下

recycler.addOnScrollListener(scrollListener); 

希望工程。(:

相關問題