Recalling onBindViewHolder on scrolling up the Recycler view - android

I have list of images in my Recycler View and on scrolling down the recycler view, onBindViewHolder is called.
I want to call onBindViewHolder again when I scroll up the recycler view so that I can get the position of currently viewed image.
How to call onBindViewHolder again(on scrolling up)?

That might not be the best way to figure out the currently viewed image, however you can add a scroll listener to your recycler view and whenever the dy scroll is negative you can call adapter.notifyDataSetChanged, so onBindViewHolder is called again
mFragmentListBinding.movieGrid.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (dy < 0) {
mAdapter.notifyDataSetChanged();
}
}
});

Related

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.

Deny scroll to first position in recyclerview

I have recycler view with adapter. Need deny user to scroll to item with 0 index, when it was scrolled. F.e user scrolls to 5 item and when scrolls back first visible will be item with index 1.
Will be glad any ideas how to implement this feature.
Use smoothScrollToIndex(1) inside addOnScrollListener of the recycler view.
Keep a if check with !recyclerView.canScrollVertically(-1) there if it succeeded then use like :
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if(!recyclerView.canScrollVertically(-1))
{
mRecyclerView.smoothScrollToIndex(1);
}
}});

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 :)

Indication when RecyclerView last item is visible

I have an activity which contain horizontal RecyclerView which display icons of movies retrieved from server
When the user scroll the recycler view to its end - making the last item of the recycler view visible i want to show "More" button to allow the loading of additional icons
How can i get indication when the user scroll to the end of the recycler view?
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (((LinearLayoutManager) recyclerView.getLayoutManager()).findLastVisibleItemPosition() == LAST_POSITION) {
// code here
}
}
});

Scroll multiple recyclerviews on one recyclerview scroll

I have an arraylist of recyclerveiws in another recyclerview and want to know how to scroll all of them when one is scrolled.
This is what I have so far, however it gives me a stack overflow error:
holder.rv.setOnScrollListener(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);
if (dx != -199382734)
for (sched_vh vh : vhs) {
if (vh != holder)
vh.rv.scrollBy(-199382734, dy);
}
}
});
I have worked on this problem with my friend for a few hours..the final answer is really simple.
The main idea is that two recyclerviews should use the same onscrollListener instead of different ones, so that this listener could get which recyclerview is scrolling, and avoid stack over flow. please try following
//rcv and leftrcv are two recyclerviews
RecyclerView.OnScrollListener scrollListener = new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if (recyclerView == leftrcv) {
rcv.removeOnScrollListener(this);
rcv.scrollBy(0, dy);
rcv.addOnScrollListener(this);
} else if (recyclerView == rcv) {
leftrcv.removeOnScrollListener(this);
leftrcv.scrollBy(0, dy);
leftrcv.addOnScrollListener(this);
}
}
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
};
leftrcv.addOnScrollListener(scrollListener);
rcv.addOnScrollListener(scrollListener);
But I don't know whether this will result in performance issue...
Your implementation is probably wrong. You should likely have one RecyclerView that has different view types.
From your comments it seems you want several vertical RecycleViews besides each other and want to horizontally scroll them.
If you want each of the vertical lists fill the screen you'd want to use a ViewPager which allows you to swipe horizontally through a list of views (or fragments).
Otherwise you could have a look at HorzizontalScrollView, though that would only make sense for a small set of vertical lists.

Categories

Resources