[Android] ScrollView is missing initial scroll position for Android
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.
render() {
return (
<ScrollView ref={(view) => this._scrollView = view}>
</ScrollView>
);
}
this._scrollView.getScrollResponder().scrollTo({x: 0, y: 0, animated: true})
@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.
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
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);
}
It'll be awesome if someone could send a pull request for it.
@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.
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.
Was this fixed or just closed?
The productpain page links back to this issue.
+1
+1 , please
+1
If someone is willing to send a PR, please do
Insted of initial scroll position for android, you might look at using ViewPagerAndroid instead, and setting the initialPage property
@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.
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...
Using scrollTo in componentDidMount is working for us in a couple of apps.
New link for the product pain: https://react-native.canny.io/feature-requests/p/androidscrollview-is-missing-initial-scroll-position-for-android
@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 yeah, I agree, it's not an ideal solution but for some cases it's working for us.
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)