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);
}
}});
Related
I want to hide keyboard if user starts scrolling in RecyclerView.
dy is scrolled distance if I understood that correctly, and if it passes certain value, I can hide keyboard.
But I found out if you press and hold the finger on screen and scroll slowly, it will not change dy value (its still 1) that means my method is not working.
Anyone know other alternative how to handle this specific usecase?
Code:
list.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
if (dy.absoluteValue > KEYBOARD_HIDE_DIST) editText.clearFocus()
}
})
Use this function computeVerticalScrollOffset() on recyclerView it will give the offset of scroll position
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(#NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
Log.d("Scrolled","Distance Scrolled : "+ recyclerView.computeVerticalScrollOffset());
}
});
Not started with kotlin yet, Hence I have added java code. Hope you got the thing. Let me know if it works for you
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();
}
}
});
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 :)
I want to add back to top button in my app. I use recyclerview for displaying items. I added button and when I click on it, view is going to top, but my goal is to show this button only if application is scrolled down or up, and my first item is not visible (I hope thats clear explanation). I tried with
LayoutManager.findFirstCompletelyVisibleItemPosition();
and other methods for LayoutManager but without acceptable effects.
My solution is showed below. I add OnScrollListener on my RecyclerView, and when view is scrolled I check if the first item is visible and I set visibility of my button.
MyRecyclerView.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) {
int visibility = (MyLayoutManager.findFirstCompletelyVisibleItemPosition() != 0) ? View.VISIBLE : View.GONE;
btn.setVisibility(visibility);
}
});
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
}
}
});