Flatlist scrollToIndex produces whitespace on iOS - react-native

The design of the application I am working on changes the layout based on the width of the device and there is a Flatlist component that calls scrollToIndex on the selected item so that it does not fall out side of the view when the app changes it's layout. If the last item is selected and scrolltToIndex is called, on iOS it will scroll the item all the way to the top and hide all the items above it. That makes it appear like it is the only item left and the only way to bring back the other items is to manually pull down the list. On android it works perfectly i.e it will only scroll an item high enough to fit as many items below it as possible (if more than height of container, it just scrolls it to the top).

From the FlatList>ScrollToIndex docs:
Note: Cannot scroll to locations outside the render window without specifying the getItemLayout prop.
Because FlatList is virtualized (i.e. efficient because it doesn't render the full list offscreen), it doesn't know the location of all of the elements (since they're not pre-rendered, and hence their size is unknown). It needs you to help by providing element sizes through getItemLayout to scroll beyond what's near visible. I suspect "near visible" may vary device to device with different memory settings. If you don't do this, you can end up with gaps in your list since exact placement is unknown.
So, you'll need to implement getItemLayout or use the less performant (and depricated) ListView component.

Related

If i don't know the exact sizes of all the component in the FlatList would getitemlayour be useful for the ones that I know?

I want to optimize the scrolling of the Flatlist so it does not jump as much. I have a relatively random set of items but some items I can predict the exact height. But obviously not offset
Can getitemlayour still be used or it has to be avoided because of the items I am not able to compute for.
I tried to use it but I get A lot of blank and inactive areas. Not sure if i am using it correctly though.
No, getItemLayout only works if you know all the dimensions and offsets of each item. This allows the FlatList to create the native views for each item before they appear on the screen. You're seeing the native views at the size and offset you're setting in getItemLayout.
If you can't change the design of the component to be predictably sized, there are other ways to optimize FlatLists, but this way will not work for you.

What is the best way to create a custom picker for measurement selection in React Native?

I am currently building an app which allows users to select their current weight and waist size. The list is long since, for example, the weight will start from 40kg up to 300kg, and each value will have a decimal point from .1 up to .9, e.g 40.0, 40.1, 40.2, 40.3, ... 40.9, 41.0, the whole numbers will be represented in a longer vertical line, while the decimal values in shorter vertical lines. So as you can imagine, the list is going to be really long. I have already implemented 2 ways: FlatList and Carousel using react-native-snap-carousel. They have their pros and cons, FlatList is much performant than the Carousel, but im having a hard time getting the value of the middle line. While the carousel performs poorly, i do have access to the current selected item. So my question here is: how do i implement this performance-wise and i have access with the currently selected item. Take Note also that i have implement FlatList's onViewableItemsChanged and it still doesn't achieve my goal
here is the screenshot of the UI:
In short:
You can check this library out: https://github.com/veizz/react-native-picker-scrollview
In detail:
There's a way to achieve this with some Flatlist props:
onScrollBeginDrag & onScrollEndDrag: detect when user starts/stops scrolling
onMomentumScrollBegin & onMomentumScrollBegin : detect scroll speed and momentum
scrollTo or scrollToIndex functions which requires Flatlist ref.
onScrollEvent if needed.
My opinion is, (which'll be my approach)
First, set the selectedItem as you like.
Then, using those scroll props above to detect which item(index) it's scrolling to. Select that item. Then scrollTo to selected.
Any point where it exceeds the threshold, use scrollTo or scrollToIndex to the next/previous item.
This is my approach, if you have your own, I'll be glad to discuss.
Happy coding!

single item is showing in recycler view even though the entire screen is empty error?

Even though the entire screen is empty only one element is visible in recycle(r) view list. I have used wrap_content for height but still it is showing single element.But when I scroll that single element second element comes in first's place.Why is it happening ?
Check the item layout of recyclerview,the height of your item maybe is match_parent.It's different from the listview item.

React Native ListView: Scroll to a particular row with variable height

My ListView displays a feed of users, where each row is variable height (similar to Facebook).
A similar question suggests to scroll to rowIndex*rowHeight, but my rows are not the same height.
Any suggestions?
There is no simple way to do this. You can try to use onLayout event and save all rows height. But if part of rows before item you want scroll to was not rendered you can't calculate offset.
One solution in this situation is render all items at once. But there may be performance issue.
Another is scrolling bit by bit and calculate height in runtime.
My advice is redesign your UX to prevent this operation. Or use ScrollView and onLayout if row count is not too big.
UPDATED: FlatList will be added in RN 0.43. It has scrollToItem method.

react-native android - ListView progressive rendering

I am playing with the <ListView> component. My screen is capable of displaying 9 rows from a data set of 32. However I see in the console that the ListView component triggers the renderRow function 32 times (basically it seems to render in the background all the items of my data set).
I was expecting that the function would be triggered only when a new row appears on screen.
Is this a normal behavior?
Or is there an option that can specify under what conditions the renderRow function should be triggered?
Thanks for your suggestions.
Paul
I figured it out. In fact the rows of the list are build ahead of time so that the scroll of the list remains as smooth as possible.
The default value (in version 0.1.8) is 1000 pixels.
My list items were 20px high. 32 items * 20px = 640px. The first ten were rendered and visible anyway so there were remaining 440px to be rendered.
You can override this default setting by using the scrollRenderAheadDistance attribute of <ListView> in order to optimize your rendering if needed.
https://facebook.github.io/react-native/docs/listview.html#scrollrenderaheaddistance