jon*_*zee 13 javascript listview ios segmentedcontrol react-native
什么是实施的最佳实践SegmentedControllIOS与ListView?我尝试了三个解决方案,所有示例都包含SegmentedControllIOS两个段和两个段ListView.我邀请您讨论这三个的表现(也许有人可以提出其他更好的解决方案).从我的角度来看,示例是从最有效的顺序给出的.
class Example extends Component {
constructor(props) {
super(props);
this.state = {
ds1: new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2,}),
ds2: new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2,}),
index: 0,
};
}
render() {
return (
<View>
<SegmentedControlIOS
selectedIndex={this.state.index}
values={['ds1', 'ds2']}
onChange={() => this.setState({index: (this.state.index+1)%2})}
/>
<ListView dataSource={this.state.index ? this.state.ds2 : this.state.ds1} />
</View>
);
}
}
Run Code Online (Sandbox Code Playgroud)
class Example extends Component {
constructor(props) {
super(props);
this.state = {
ds1: new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2,}),
ds2: new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2,}),
index: 0,
};
}
render() {
return (
<View>
<SegmentedControlIOS
selectedIndex={this.state.index}
values={['ds1', 'ds2']}
onChange={() => this.setState({index: (this.state.index+1)%2})}
/>
{this.state.index === 0 ?
(<ListView dataSource={this.state.ds1} />)
:
(<ListView dataSource={this.state.ds2} />)
}
</View>
);
}
}
Run Code Online (Sandbox Code Playgroud)
class Example extends Component {
constructor(props) {
super(props);
this.state = {
ds: new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2,}),
ds1: ['some', 'data'],
ds2: ['some', 'other', 'data'],
index: 0,
};
this.onChange = this.onChange.bind(this);
}
onChange() {
this.setState({
ds: this.state.ds.cloneWithRows(this.state.index ? this.ds1 : this.ds2),
index: (this.state.index+1)%2,
})
}
render() {
return (
<View>
<SegmentedControlIOS
selectedIndex={this.state.index}
values={['ds1', 'ds2']}
onChange={this.onChange}
/>
<ListView dataSource={this.state.ds} />
</View>
);
}
}
Run Code Online (Sandbox Code Playgroud)
第三种方法是最好的。当您使用 时,使用的cloneWithRows函数来了解现在需要重新渲染哪些行。由于第一行中的内容将与该行的第一行中的内容匹配,因此不会重新渲染。ListViewDataSourcerowHasChanged'some'ds1'some'ds2
在情况 1 中,您没有利用DataSource对象的状态,ListView会发现它正在尝试渲染完全不同(可能不可比较)的数据源。
在情况 2 中,您可能会通过切换重量级可滚动组件来获得一些有趣的渲染工件。
| 归档时间: |
|
| 查看次数: |
475 次 |
| 最近记录: |