React Native ListView - 并非呈现所有可见项目
React Native ListView - not all visible items are rendered
作为第一个 React-Native 项目,我只是想创建一个简单的动画。
这是一个 ListView,里面充满了正方形,这些正方形为它们的不透明度设置了动画。
为此,我创建了一个包含 Square 的 ListView,它是一个涂有 3 种颜色之一的 50X50 正方形。
class Square extends Component {
constructor(props) {
super(props);
_defaultTransition = 500;
this.state = {
_rowOpacity : new Animated.Value(0),
targetOpacity: 0
};
setInterval(() => {
this.state.targetOpcacity = this.state.targetOpcacity == 0 ? 1 : 0;
this.animate(this.state.targetOpcacity);
}, Math.random() * 1000);
}
animate(to) {
Animated.timing(
this.state._rowOpacity,
{toValue: to, duration : 500}
).start();
}
componentDidMount() {
this.animate(1);
}
render() {
var rand = Math.random();
var color = rand < 0.3 ? 'powderblue' : rand > 0.6 ? 'skyblue' : 'steelblue';
return (
<Animated.View
style={{height:50, width: 50, backgroundColor: color, opacity: this.state._rowOpacity}}>
{this.props.children}
</Animated.View>
);
}
}
class ReactFirst extends Component {
constructor(props) {
super(props);
var array = Array.apply(null, {length: 1000}).map(Number.call, Number);
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
dataSource: ds.cloneWithRows(array)
};
}
render() {
return (
<ListView contentContainerStyle={styles.list}
dataSource={this.state.dataSource}
renderRow={(rowData) => <Square></Square>}
/>
);
}
}
var styles = StyleSheet.create({
list: {
flexDirection: 'row',
flexWrap: 'wrap'
}
});
结果是,尽管数组有 1000 个单元格,但只有 11 个方块在屏幕上动画。
这意味着,渲染了一行半,屏幕的其余部分是空白的。
我想让整个屏幕充满动画方块。
非常感谢您的帮助,
乔拉.
好的,在尝试了一些事情并返回到文档之后,我找到了:initialListSize 属性,它允许设置渲染行的初始数量。
<ListView contentContainerStyle={styles.list}
dataSource={this.state.dataSource}
initialListSize={size}
renderRow={(rowData) => <Square></Square>}
/>
成功了。
作为第一个 React-Native 项目,我只是想创建一个简单的动画。 这是一个 ListView,里面充满了正方形,这些正方形为它们的不透明度设置了动画。 为此,我创建了一个包含 Square 的 ListView,它是一个涂有 3 种颜色之一的 50X50 正方形。
class Square extends Component {
constructor(props) {
super(props);
_defaultTransition = 500;
this.state = {
_rowOpacity : new Animated.Value(0),
targetOpacity: 0
};
setInterval(() => {
this.state.targetOpcacity = this.state.targetOpcacity == 0 ? 1 : 0;
this.animate(this.state.targetOpcacity);
}, Math.random() * 1000);
}
animate(to) {
Animated.timing(
this.state._rowOpacity,
{toValue: to, duration : 500}
).start();
}
componentDidMount() {
this.animate(1);
}
render() {
var rand = Math.random();
var color = rand < 0.3 ? 'powderblue' : rand > 0.6 ? 'skyblue' : 'steelblue';
return (
<Animated.View
style={{height:50, width: 50, backgroundColor: color, opacity: this.state._rowOpacity}}>
{this.props.children}
</Animated.View>
);
}
}
class ReactFirst extends Component {
constructor(props) {
super(props);
var array = Array.apply(null, {length: 1000}).map(Number.call, Number);
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
dataSource: ds.cloneWithRows(array)
};
}
render() {
return (
<ListView contentContainerStyle={styles.list}
dataSource={this.state.dataSource}
renderRow={(rowData) => <Square></Square>}
/>
);
}
}
var styles = StyleSheet.create({
list: {
flexDirection: 'row',
flexWrap: 'wrap'
}
});
结果是,尽管数组有 1000 个单元格,但只有 11 个方块在屏幕上动画。 这意味着,渲染了一行半,屏幕的其余部分是空白的。
我想让整个屏幕充满动画方块。
非常感谢您的帮助, 乔拉.
好的,在尝试了一些事情并返回到文档之后,我找到了:initialListSize 属性,它允许设置渲染行的初始数量。
<ListView contentContainerStyle={styles.list}
dataSource={this.state.dataSource}
initialListSize={size}
renderRow={(rowData) => <Square></Square>}
/>
成功了。