2017-04-10 59 views

回答

1

只需在您的recyclerview上實現addOnScrollListener()。然後在滾動監​​聽器裏面實現下面的代碼。

RecyclerView.OnScrollListener mScrollListener = new RecyclerView.OnScrollListener() { 
    @Override 
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) { 
     if (mIsLoading) 
      return; 
     int visibleItemCount = mLayoutManager.getChildCount(); 
     int totalItemCount = mLayoutManager.getItemCount(); 
     int pastVisibleItems = mLayoutManager.findFirstVisibleItemPosition(); 
     if (pastVisibleItems + visibleItemCount >= totalItemCount) { 
      //End of list 
     } 
    } 
}; 
0
private RecyclerView.OnScrollListener nRecyclerViewOnScrollListener = new RecyclerView.OnScrollListener() { 
    @Override 
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) { 
     super.onScrollStateChanged(recyclerView, newState); 
    } 

    @Override 
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) { 
     super.onScrolled(recyclerView, dx, dy); 
     int visibleItemCount = notificationLayoutManager.getChildCount(); 
     int totalItemCount = notificationLayoutManager.getItemCount(); 
     int firstVisibleItemPosition = notificationLayoutManager.findFirstVisibleItemPosition(); 
     if ((visibleItemCount + firstVisibleItemPosition) >= totalItemCount) { 
      nLoadMoreItems(); 

     } 
    } 
}; 

private void nLoadMoreItems() { 
    if (defaultCurrentPage < defaultpageCount) 
     layout_load_more_loadingNotification.setVisibility(View.VISIBLE); 
    int pageCount = (defaultCurrentPage + 1); 
    //fetch second page data 
     } 

嘗試

+0

謝謝, 你可以在下面查看我更新的答案@Safal –

0

看到所有的答案後,我解決我的問題,通過這樣做,

private RecyclerView.OnScrollListener nRecyclerViewOnScrollListener = new RecyclerView.OnScrollListener() { 
    @Override 
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) { 
     super.onScrollStateChanged(recyclerView, newState); 
     if (newState == 2) { 
      try { 
       if (lastVisibleItemPosition + 1 == totalItemCount) { 
        //Write you code here 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
       Log.i(TAG, e.getMessage()); 
      } 
     } 
    } 

    @Override 
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) { 
     super.onScrolled(recyclerView, dx, dy); 
     totalItemCount = mLayoutManager.getItemCount(); 
     lastVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastCompletelyVisibleItemPosition(); 

    } 
}; 

添加監聽器來回收視圖

recyclerView.addOnScrollListener(nRecyclerViewOnScrollListener); 
相關問題