Recycler view starts auto scroll when new list is added - android

I have an problem with implementation of recycler view.
I'm using listadapter with recyclerview.
Everythings work perfectyl unitl I'm changing arraylist items order.
When I'm changing the items order and submit it to adapter the recycler view makes scroll.
For examle if i reverse the arraylist , its scrolling to the bottom.
How to disable auto scrol or keep recyclerview scroll position when the sort orders is changed.

There are two possible ways to solve this issue. Please try them :-
You can use notifyItemRangedChanged(fromIndex,toIndex);.
You can use RecyclerView.scrollToPosition(0); when sort orders are changed.
Thank you!

Related

Get RecyclerView to stop scrolling when I add items to the Adapter

How can I stop RecyclerView from scrolling to the bottom of the list whenever I add items?
Even when I insert something at the top of the list and call notifyDataSetChanged or notifyItemInserted the list scrolls to the bottom.
The one feature that works as expected is notifyItemRemoved(index)
I think I was calling notifyRangeInserted() with the entire list and that was scrolling to the end of that range.
If you're having similar issues, just cut the Adapter functionality back to its bare essentials and build up from there.
The problem was I had an item in the list, a ProgressBar to indicate that data was loading, but when the new items loaded I added them to the list, this moved the "focused item," the ProgressBar down and if there were enough items to move it off the screen then the RecyclerView would scroll down to keep it on screen. The problem being 1. I didn't want it to maintain "focus" and 2. The very next thing I did was remove the ProgressBar from the `Adapter.
So the solution is to remove the ProgressBar or other items before adding items earlier positions in the Adapter.

Nested RecyclerView scrolls to the first item on NotifyItemChanged

I have vertical RecyclerView for scrolling group of items and horizontal RecyclerView in each ViewHolder in order to scrol items inside of these groups. They are populated from database. Whenever item content is changed (user tap something or new data come from network) it is written to database and then notifyDataSetChanged() is called for the group cursor. I check if it is the same group in onBingViewHolder() and update items only if it is. But horizontal RecyclerView is scrolled anyway to the first item.
How could I prevent this behavior and why does it happens ?
BTW I'm writing result of this check in 'onBindViewHolder()` to the log and I can see that it is the same item.
Thanks.
RecyclerView creates new ViewHolder in order to perform animation. setItemAnimator(null) solved my problem.

Show the last few items on ListView

How could I keep the just last few items in a ListView shown? Currently, I'm using the following line to achieve this.
lv.smoothScrollToPosition(lv.getCount());
However, smoothScroll() is annoying because it has to scroll through all ListView elements to get to the end.
Use ListView.setSelection() with your preferred position.

Dynamic list view with a dynamic adapter

I want to create a dynamic listview which adds dynamic elements on scrolling to the end of the initial list. New items should be added everytime the scroll position reaches the end of previous list. How can I achieve this? Thank you.
you need to add a scroll listener and override the onscroll()

Android: Add cells to GridView dynamically?

I am want to add cells to GridView dynamically when the user reach the last row of cells its like the show more?
Check out CommonsWare EndlessAdapter. Very straight-forward Adapter implementation.
You could also manually use an OnScrollListener on the GridView. Use the onScrollStateChanged() callback to figure out when the grid is OnScrollListener.SCROLL_STATE_IDLE, then determine if the last grid item is visible. If so, get more items for your Adapter, and call notifyDataSetChanged().

Categories

Resources