react-native icon indicating copy to clipboard operation
react-native copied to clipboard

[Android] ScrollView is missing initial scroll position for Android

Open bcalik opened this issue 10 years ago • 82 comments

ScrollView has a contentOffset prop for iOS, which sets the initial scroll offset. But it lacks on Android, so you can not set the initial scroll position.

For anyone who can make this, these are the attributes in Android's native ScrollView.

android:scrollX     The initial horizontal scroll offset, in pixels. 
android:scrollY     The initial vertical scroll offset, in pixels. 

bcalik avatar Apr 06 '16 22:04 bcalik

render() {
  return (
    <ScrollView ref={(view) => this._scrollView = view}>
    </ScrollView>
  );
}

this._scrollView.getScrollResponder().scrollTo({x: 0, y: 0, animated: true})

hufeng avatar Apr 08 '16 12:04 hufeng

@hufeng That will cause flickering after render, first you will see the 0,0 position, then it will scroll.

Also if you are using a ListView, then you will have to render all the data until the scroll position you want, then you can scroll there.. Which will slow your app dramatically. But in iOS, you can just set initialListSize={1} and it can just start rendering where-ever you want by setting contentOffset.

bcalik avatar Apr 08 '16 12:04 bcalik

https://productpains.com/post/react-native/content-offsetinset-for-scrollview

I started a product pains post for this a while back, but it hasn't gained any traction. While @hufeng's approach does the correct thing, it's very janky and even if you set animated: false, it will be noticeable to the end-user that the scrollview is jumping to its initial position. I've wanted to take a crack at a PR for this for a while but haven't had the bandwidth outside of work but for me this would be one of the most welcomed features for Android/iOS parity

mangogogos avatar Apr 12 '16 16:04 mangogogos

When I try to call scrollTo from componentDidMount method it does not scroll. I have to use workaround with setTimeout to make it work:

componentDidMount() {
  setTimeout(() => this.refs._scrollView.scrollTo({ x: 100, y: 0 }) , 0);
}

madox2 avatar Jun 10 '16 08:06 madox2

It'll be awesome if someone could send a pull request for it.

satya164 avatar Jun 10 '16 09:06 satya164

@madox2 Because the scroll position you want to scroll is not rendered yet, you have to wait for all rows to be rendered before you scroll to that position. So setTimeout is not a solution, which may not work.

iOS ListView does't have this issue, because it has a initial scroll position property, so it can start rendering from that position, not from zero.

bcalik avatar Jun 10 '16 09:06 bcalik

Hi there! This issue is being closed because it has been inactive for a while.

But don't worry, it will live on with ProductPains! Check out its new home: https://productpains.com/post/react-native/android-scrollview-is-missing-initial-scroll-position-for-android

Product Pains has been very useful in highlighting the top bugs and feature requests: https://productpains.com/product/react-native?tab=top

Also, if this issue is a bug, please consider sending a pull request with a fix.

mkonicek avatar Oct 27 '16 21:10 mkonicek

Was this fixed or just closed?

The productpain page links back to this issue.

arv avatar Nov 08 '16 19:11 arv

+1

uqix avatar Nov 18 '16 15:11 uqix

+1 , please

cherniv avatar Nov 21 '16 19:11 cherniv

+1

aymericbouzy avatar Nov 25 '16 09:11 aymericbouzy

If someone is willing to send a PR, please do

satya164 avatar Nov 25 '16 10:11 satya164

Insted of initial scroll position for android, you might look at using ViewPagerAndroid instead, and setting the initialPage property

tangkunyin avatar Jan 04 '17 08:01 tangkunyin

@tangkunyin No. What if you need a to use it on an infinite scrolling page?

Think about the instagram app. Go to the explore tab, you will see the grids of posts, then click on an any post, the new page is an infinite scrolling listview, but started from an initial scroll position.

So because of this issue, you can't make an instagram clone for android using React Native.

bcalik avatar Jan 04 '17 14:01 bcalik

Has there been any further discussion on this issue? I would really love to set: pagingEnabled={true} horizontal={true} and have the ScrollView set the x position to the 2nd page initially without the flash of (0, 0). A workaround is fine if there is a solution...

studiobrain avatar Feb 17 '17 18:02 studiobrain

Using scrollTo in componentDidMount is working for us in a couple of apps.

alvaromb avatar Mar 13 '17 12:03 alvaromb

New link for the product pain: https://react-native.canny.io/feature-requests/p/androidscrollview-is-missing-initial-scroll-position-for-android

SudoPlz avatar Mar 14 '17 12:03 SudoPlz

@alvaromb It won't work when you try to scroll to position that is not rendered yet. initial scroll position lets your app to start rendering from your initial scroll position, not from zero. You you won't have to wait for all of the rows to be rendered, which is a massive performance impact.

bcalik avatar Mar 14 '17 12:03 bcalik

@bcalik yeah, I agree, it's not an ideal solution but for some cases it's working for us.

alvaromb avatar Mar 14 '17 12:03 alvaromb

has anybody found a solution for this?

I'm trying to start at the end of a horizontal ScrollView and animate towards the left once the component has rendered (making it obvious that this is a section which is scrollable)

cdimitroulas avatar Mar 20 '17 17:03 cdimitroulas