Andrew G
Andrew G

Reputation: 2614

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)

Upvotes: 3

Views: 2419

Answers (2)

Andrew G
Andrew G

Reputation: 2614

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.

Upvotes: 0

Andrew G
Andrew G

Reputation: 2614

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.

Upvotes: 1

Related Questions