Created
February 28, 2019 11:47
-
-
Save Eng-MFQ/9b659f6d918fcfbccc0a4e268119b25e to your computer and use it in GitHub Desktop.
listener on RecyclerView that detect all load more trigger from all sides TOP, BOTTOM, RIGHT, LEFT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void loadMore() { | |
int LAUNCH_LOAD_MORE_AT_POSITION_FROM_TOP =5; | |
mRecycler.addOnScrollListener(new RecyclerView.OnScrollListener() { | |
@Override | |
public void onScrolled(RecyclerView recyclerView, int dx, int dy) { | |
super.onScrolled(recyclerView, dx, dy); | |
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager(); | |
int firstCompleteVisibleItemPosition = -1; | |
int lastCompleteVisibleItemPosition = -1; | |
int visibleItemCount = layoutManager.getChildCount(); | |
int totalItemCount = layoutManager.getItemCount(); | |
if(layoutManager instanceof GridLayoutManager) | |
{ | |
GridLayoutManager gridLayoutManager = (GridLayoutManager)layoutManager; | |
firstCompleteVisibleItemPosition = gridLayoutManager.findFirstCompletelyVisibleItemPosition(); | |
lastCompleteVisibleItemPosition = gridLayoutManager.findLastCompletelyVisibleItemPosition(); | |
}else if(layoutManager instanceof LinearLayoutManager) | |
{ | |
LinearLayoutManager linearLayoutManager = (LinearLayoutManager)layoutManager; | |
firstCompleteVisibleItemPosition = linearLayoutManager.findFirstCompletelyVisibleItemPosition(); | |
lastCompleteVisibleItemPosition = linearLayoutManager.findLastCompletelyVisibleItemPosition(); | |
} | |
String message = ""; | |
// Means scroll at beginning ( top to bottom or left to right). | |
if(firstCompleteVisibleItemPosition == LAUNCH_LOAD_MORE_AT_POSITION_FROM_TOP) | |
{ | |
// dy < 0 means scroll to bottom, dx < 0 means scroll to right at beginning. | |
if(dy < 0 || dx < 0) | |
{ | |
// Means scroll to Top. | |
if(dy < 0) | |
{ | |
if (BuildConfig.DEBUG) | |
Log.d(TAG, "onScrolled: Top"); | |
loadData(true); | |
} | |
// Means scroll to right. | |
if(dx < 0 ) | |
{ | |
if (BuildConfig.DEBUG) | |
Log.d(TAG, "onScrolled: Right"); | |
loadData(true); | |
} | |
} | |
} | |
// Means scroll at ending ( bottom to top or right to left ) | |
else if(lastCompleteVisibleItemPosition == (totalItemCount - 1)) | |
{ | |
// dy > 0 means scroll to up, dx > 0 means scroll to left at ending. | |
if(dy > 0 || dx > 0) | |
{ | |
// Scroll to Bottom | |
if(dy > 0) | |
{ | |
if (BuildConfig.DEBUG) | |
Log.d(TAG, "onScrolled: Bottom"); | |
loadData(false); | |
} | |
// Scroll to left | |
if(dx > 0 ) | |
{ | |
loadData(false); | |
if (BuildConfig.DEBUG) | |
Log.d(TAG, "onScrolled: Left"); | |
} | |
} | |
} | |
if(message.length() > 0) { | |
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show(); | |
} | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment