Reputation: 1547
When user scroll the recyclerview, I want a back_to_top button to display when scrolling up, and a load_more button to disappear when scrolling up and display when reaching the end of the list. The OnScrollListener is as below:
mRecyclerViewHome.addOnScrollListener(new OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int visibleItemCount = layoutManager.getChildCount();
int totalItemCount = layoutManager.getItemCount();
int pastVisibleItems = layoutManager.findFirstVisibleItemPosition();
if (pastVisibleItems + visibleItemCount >= totalItemCount) {
mButtonLoadMore.setVisibility(View.VISIBLE);
Log.i(TAG, "reach bottom detected");
}
// TODO: 7/20/2017 remove shaking
if (dy < 0) { // scrolling up
mButtonLoadMore.setVisibility(GONE);
mButtonToTop.setVisibility(VISIBLE);
}
if (dy > 0) {
mButtonToTop.setVisibility(GONE);
}
}
});
This code works. But the problem is that, if while the recyclerview is scrolling and not stop yet, I interrupt and touch the screen and do another scroll gesture, chance is that the view of the recyclerview is shaking while moving. I guess it's becuase I'm using dy parameter in the code, so that it keeps tracking the value of dy and causes view shaking? Is there anyway to avoid this shaking while detect if the user is scrolling up or down?
Upvotes: 4
Views: 1181
Reputation: 187
can you try disabling item animator
mRecyclerViewHome.setItemAnimator(null);
Upvotes: 1