Updated 31 December 2016
The idea here is to find the position of the last visible item. If that position is equal to the last item of your dataset, then you should trigger a reload.
There are several methods to accomplish it. I am using addOnScrollListener() with my Recycler View when we scrolling our screen the Listener is fired. then we find the last item of recycler view with the help of our Layout Manager.
1 |
lastCompletelyVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastVisibleItemPosition(); |
Here is the code for Listener-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
myRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); int lastCompletelyVisibleItemPosition = 0; lastCompletelyVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastVisibleItemPosition(); try { if (mToast != null) mToast.setText((lastCompletelyVisibleItemPosition + 1)); else mToast = Toast.makeText(this, lastCompletelyVisibleItemPosition + 1, Toast.LENGTH_LONG); if (dy > 5) { mToast.show(); } } catch (JSONException e) { e.printStackTrace(); trackException(e,this); } try { if (lastCompletelyVisibleItemPosition == totalItems - 1) { if (!loadingWithServer) { loadingWithServer = true; pageNumber++; findViewById(R.id.listVendorRequestBar).setVisibility(View.VISIBLE); loadMoreItem(); } } }catch (JSONException e){ e.printStackTrace(); } } }); |
Note- If you are load data from the server then take care, recycler view work with own Thread and server Thread do own work without affecting each other. so control loading with the following condition –
1 2 3 4 5 6 |
if (!loadingWithServer) { loadingWithServer = true; pageNumber++; findViewById(R.id.listVendorRequestBar).setVisibility(View.VISIBLE); loadMoreItem(); } |
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.