How to avoid recyclerview shaking while scrolling? - android

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?

can you try disabling item animator
mRecyclerViewHome.setItemAnimator(null);

Related

How to infinite scroll last fragment from multiple fragment?

I have multiple fragment in one activity and want last fragment to infinite scroll, but addOnScrollListener not working when scrolling.
I have success to implement infinite scroll in activity and in a fragment.
In this case I want to implement infinite scroll in activity with multiple child fragment and the only one can infinite scroll is last fragment. When i use addOnScrollListener on last fragment my code not working, I realize the one scrolling is my activity not my fragment that why addOnScrollListener i my fragment not work.
Im using this code for activity and fragment :
public void onScrolled(#NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
Toast.makeText(getContext(), "yeah", Toast.LENGTH_SHORT).show();
visibleItemCount = layoutManager.getChildCount();
totalItemCount = layoutManager.getItemCount();
pastVisibleItemCount = layoutManager.findLastVisibleItemPosition();
if (dy > 0) {
if (isLoading) {
if (totalItemCount > previousTotal) {
isLoading = false;
previousTotal = totalItemCount;
}
}
if (!isLoading && (totalItemCount - visibleItemCount) <= (pastVisibleItemCount - itemTreshold)) {
page++;
loadMore();
isLoading = true;
}
}
}
How can I archive only the last fragment can infinite scroll from that case?

How to detect recyclerview’s scrolling outside it bounds?

Is there a way to detect that user is trying to scroll recyclerview and actual scrolling is not happened? For example, the vertical recyclerview is at its top position, and user tries to scroll it up.
Yeah , you can detect the scrolling behaving using onScroll function of recycler view .
recyclerView
.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView,
int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
lastVisibleItem = linearLayoutManager
.findLastVisibleItemPosition();
if (!loading
&& totalItemCount <= (lastVisibleItem + visibleThreshold)) {
// Do something
}
}
});
}
As you can see above , (linearLayoutManager.findLastVisibleItemPosition();) gives you the last visible position , once the user try to scroll the recycler view , it tends to change the last visible position .
Even dx gives you the int: The amount of horizontal scroll and dy (int) gives you the amount of vertical scroll .
This callback will also be called if visible item range changes after a layout calculation. In that case, dx and dy will be 0.

RecyclerView ScrollY always 0

I have a RecyclerView inside a fragment. I need to be notified of scroll changes, so I added a callback to the ScrollChange event (which corresponds to the SetOnScrollChangeListener in classical Android API):
private void RecyclerViewScrollChange(object sender, View.ScrollChangeEventArgs e)
{
int deltaY = e.ScrollY - e.OldScrollY;
}
The problem is that deltaY is always zero, because both ScrollY and OldScrollY are always zero. I am using vertical linear layout in the recycler view's layout manager, so the vertical dimension ought to be updated. What can cause this problem?
View.SetOnScrollChangeListener() was added in API level 23 which you can see here in the docs: View.SetOnScrollChangeListener()
If your app should support versions before API level 23, a workaround is by adding a ScrollChangeListener to the RecyclerView's `ViewTreeObserver' using AddOnScrollChangedListener() as followed:
_recyclerView.ViewTreeObserver.AddOnScrollChangedListener(param);
Or adding a callback to the ViewTreeObserver's ScrollChanged event
scrolling to Y position is getting using
recyclerView.addOnScrollListener(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);
Log.v("onScrolled", "dx:" + dx + " dy:" + dy);
}
});

Hide floating action button after recyclerview is scrolled in Viewpager fragment

I have a viewpager in the containerActivity hosting two fragments, both having recyclerview, The floating action button is in the container activity, I want to hide fab on recyclerview scrolling.
This StackOverFlow post has the solution
But the problem is how to notify the fab in container activity that the recyclerview in the fragment is scrolled.
I am new to android, So any help would be appreciated.
Thanks!
try this method.
it worked for me
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (dy > 0 && mFloatingActionButton.getVisibility() == View.VISIBLE) {
mFloatingActionButton.hide();
} else if (dy < 0 && mFloatingActionButton.getVisibility() != View.VISIBLE) {
mFloatingActionButton.show();
}
}});

Swipe Back Activity Android

I am using swipe back activity in one of my project activity.
Demo Project Which I have implemented
My activity layout contains recyclerview and other components as well. So the problem is when I scroll up my recyclerview items then activity get finished.
However it is working perfectly in Listview.
So is there any way to prevent activity to finish while recyclerview is scrolling ?
I solved by adding a scroll listener.
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int firstVisibleItem = layoutManager.findFirstCompletelyVisibleItemPosition();
if (firstVisibleItem == 0) {
setEnableSwipe(true);
} else {
setEnableSwipe(false);
}
}
});
I don't know if this is the best solution. But it works :)

Categories

Resources