recyclerlistview icon indicating copy to clipboard operation
recyclerlistview copied to clipboard

Maintain scroll position when adding rows to the top of the list

Open AbdallaElabd opened this issue 8 years ago • 19 comments

When I call dataProvider.cloneWithRows() and add more rows to the top of the list. The scroll position is lost. I tried investigating by reading the source code. I'd appreciate it if you could help with this!

And thanks for the great library! Its performance is amazing.

AbdallaElabd avatar Sep 19 '17 10:09 AbdallaElabd

Sadly, we do not have anchoring support for items added to top of the list as of now. Can you specify the use case? We are considering adding it soon though. Have you tried FlatList? Does it preserve scroll position? We do perform much faster but never tried scroll anchoring with FlatList.

naqvitalha avatar Sep 19 '17 13:09 naqvitalha

Flatlist's performance is horrible. The reason I switched to RecyclerListView is because of its impressive smooth performance.

And sadly Flatlist doesn't support preserving the scroll positioning when adding items to the top of the list as well. Do you have an idea of a possible work around? I could invest sometime in trying to fix it and make a PR for it

AbdallaElabd avatar Sep 19 '17 16:09 AbdallaElabd

Yeah we'll be more than happy to accept PRs, however, this might not be as simple as a small bugfix. @surya-kanoria from out team is already thinking of solutions around this. Maybe you guys can collaborate.

naqvitalha avatar Sep 20 '17 10:09 naqvitalha

@surya-kanoria I've read most of the source code and I have an idea of how things work. Would love to help implement this. How do you suggest we contact each other?

AbdallaElabd avatar Sep 20 '17 22:09 AbdallaElabd

@AbdallaMohamed Let us connect on hangouts. Can you please share your gmail id and a suitable time to talk?

surya-kanoria avatar Sep 22 '17 07:09 surya-kanoria

@surya-kanoria feel free to mail me at [email protected] anytime

AbdallaElabd avatar Sep 22 '17 12:09 AbdallaElabd

Any updates on this, @surya-kanoria ?

nsantacruz avatar Nov 26 '17 13:11 nsantacruz

Just checking if there's any news updates on this. @naqvitalha

AbdallaElabd avatar Jun 17 '18 19:06 AbdallaElabd

Any updates on this issue?

miladrasooli avatar Oct 28 '18 21:10 miladrasooli

1 year ago...

z98361 avatar Nov 21 '18 10:11 z98361

2 years ago...

mayconmesquita avatar May 25 '19 02:05 mayconmesquita

Has anybody given this a shot yet

pribeh avatar Jul 26 '19 17:07 pribeh

Is it already implemented?

I can give a real use case - a chat stream. A user can search for chat messages, then find something in the middle and jump into it, so the middle of chat stream will be rendered. Then a user can scroll back n forth, so a scroll position preserving must be implemented when insert messages in both directions when scroll

Could someone give an update here?

soulfly avatar Aug 05 '19 20:08 soulfly

dang.. this is still not implemented?

timurridjanovic avatar Jun 06 '20 23:06 timurridjanovic

+1 is there any plan for it?

Jade-Jade-Jade avatar Jul 09 '20 08:07 Jade-Jade-Jade

I was exploring this library as an alternative to FlatList because it has this issue. Is there a way to build on top of the React Native ScrollView and solve this problem of anchoring, or is that something that will need to be solved in ScrollView itself?

jack-guy avatar Jul 25 '20 21:07 jack-guy

I've got this to work with some success:

requestAnimationFrame(() => {
      const listView = listViewRef.current;

      if (listView) {
        listView.scrollToIndex(measuredHeights.length, false);

        // Meh
        setTimeout(() => {
          const renderer = (listView as any).getVirtualRenderer() as VirtualRenderer;

          const heightOffset = measuredHeights.reduce(
            (heightOffset, measuredHeight) =>
              heightOffset + measuredHeight.measuredHeight,
            0
          );

          renderer.updateOffset(0, heightOffset, true, {
            windowShift: 0,
            startCorrection: 0,
            endCorrection: 0
          });
        }, 50);
      }
    });

where measuredHeights is an array of items you're adding to the beginning of the list and measuredHeight.measuredHeight is the measured height of an individual element in your newly added array.

paulxuca avatar Oct 29 '20 00:10 paulxuca

@paulxuca I have a similar issue where the scroll position resets itself when I change the local state and trigger a rerender.. Would this work in my situation you think?

timurridjanovic avatar Oct 29 '20 00:10 timurridjanovic

In case anyone is here in 2025 (like me), Flashlist v2 (currently Alpha) does very well at this with their maintainVisibleContentPosition implementation. https://github.com/Shopify/flash-list/tree/new-rlv-prototype?tab=readme-ov-file#flashlist-v2

chrisvariety avatar May 26 '25 19:05 chrisvariety