I want to create a ScrollView
that has two columns containing Views
of asymmetrical heights. I do not think this is possible to do using flexbox, so I'm playing around with it using absolute values (discouraged per docs).
All subviews appear as I expect. I can scroll but when I release my finger the ScrollView
returns to the top. I've specified the height of every view in the hierarchy. How can I fix?
Keep in mind that ScrollViews must have a bounded height in order to work, since they contain unbounded-height children into a bounded container (via a scroll interaction). In order to bound the height of a ScrollView, either set the height of the view directly (discouraged) or make sure all parent views have bounded height.
Code (try on snack.expo.io):
import React, { Component } from 'react';
import { View, ScrollView, Dimensions } from 'react-native';
const { width, height } = Dimensions.get('window');
const gutterSize = 10;
const columns = 2;
const colWidth = (width / columns) - (gutterSize * columns) + (gutterSize / 2);
const style = {width: colWidth, height: 100, backgroundColor: '#0f0', marginLeft: gutterSize, marginTop: gutterSize, position: 'absolute'};
export default class App extends Component {
render() {
return (
<View style={{height: height, backgroundColor: '#00f'}}>
<ScrollView style={{backgroundColor: '#f00', height: 3000, position: 'absolute', width: width, top: 20}}>
<View style={[style, {height: 100}]} />
<View style={[style, {height: 120, left: 155}]} />
<View style={[style, {height: 190, top: 110}]} />
<View style={[style, {height: 120, left: 155, top: 130}]} />
<View style={[style, {height: 190, left: 155, top: 260}]} />
<View style={[style, {height: 80, top: 310}]} />
<View style={[style, {height: 280, top: 400}]} />
<View style={[style, {height: 300, left: 155, top: 460}]} />
</ScrollView>
</View>
);
}
}