4

Scenario:

  • my adapter supports two different view types: A & B
  • the adapter is notified about 100 items of type A - everything is rendered correctly
  • the adapter is notified about one item of type B insertion using notifyItemRangeInserted at position 0 - this item is "invisible" by default and in order to see it I have to manually scroll up.

How can I get this first item of type B "automatically" visible?

Mariusz
  • 1,825
  • 5
  • 22
  • 36
  • I think there is a bug there related to position 0, but cant remember correctly, I would call dataChanged event instead and see if this helps – Sander Rito Feb 27 '19 at 07:00
  • The problem with your proposal is that I am using DiffUtil for notifications and there is so much built already on top of it. In order to call dataChanged I would make some really bad hacks which I'd like to avoid – Mariusz Feb 27 '19 at 07:03
  • Then call smoothScrollToPosition(0) after notify item inserted? – Sander Rito Feb 27 '19 at 07:04
  • That will definitely do the trick but my assumption is that for insertion at the beginning when the list is scrolled to the top I don't have to do any "manual" scrolling – Mariusz Feb 27 '19 at 07:14
  • I think you can verify if the first element is visible and if it is then execute the scroll. Cant remember the method exactly but is something like firstFullyVisibleItem or something like that. Maybe this would work! – Sander Rito Feb 27 '19 at 07:17

2 Answers2

4

You can use this line of code after notify item of type B:

yourRecyclerView.smoothScrollToPosition(0);
Masoud Mokhtari
  • 2,390
  • 1
  • 17
  • 45
1

using https://stackoverflow.com/a/54899984/8144663 only wont resolve your issue.

You will need to call smoothScrollToPosition() in the next frame like,

recyclerview.post(new Runnable() {
            @Override
            public void run() {
                 recycleview.smoothScrollToPosition(n);
            }
        });
Sandip Savaliya
  • 784
  • 6
  • 19