react-native - 如何水平显示具有 2 列的 Scrollview 图像
问题描述
嗨,我想水平显示具有 2 列的图像,我正在使用滚动视图,但我不知道该怎么做,我正在使用以下代码
获取api的代码
componentDidMount(){
return fetch('https://www.json-generator.com/api/json/get/ccLAsEcOSq?indent=1')
.then((response) => response.json())
.then((responseJson) => {
this.setState({
isLoading: false,
dataSource: responseJson.book_array,
}, function(){
});
})
.catch((error) =>{
console.error(error);
});
}
渲染代码
render() {
if (this.state.isLoading === true) {
return <ActivityIndicator color={'red'} />;
}
return (
<View style={styles.container}>
<ScrollView horizontal={true}>
{this.state.dataSource.map(item => this.renderItem(item))}
</ScrollView>
</View>
);
} }
renderItem 的代码
renderItem(item) {
return (
<View style={{ margin: 5 }}>
<View style={{
backgroundColor: 'red',
width: 150,
height: 150,
marginBottom: 1,
}}>
<Image style={{ width: 150,height: 150}}
source={{uri: item.image}}/>
</View>
<View style={{
backgroundColor: 'red',
width: 150,
height: 150,
marginBottom: 1,
}}>
<Image style={{ width: 150,height: 150}}
source={{uri: item.image}}/>
</View>
</View>
);}
解决方案
而不是ScrollView
try FlatList
which 提供numColumns
道具,让您允许根据您的选择使用列。
取而代之的是,
<ScrollView horizontal={true}>
{this.state.dataSource.map(item => this.renderItem(item))}
</ScrollView>
用这个,
<FlatList
data={this.state.dataSource}
numColumns={2}
renderItem={this.renderItem}
/>
有关详细信息,FlatList
请参见此处的官方文档
推荐阅读
- r - data.table 中的列槽
- java - Java:在我的程序中将文件数据转换为可用数据时遇到问题。有小费吗?
- mysql - SQL:使用类似列表的数据在 col 中搜索匹配元素的有效方法
- php - 启用 php7.0 时出现“考虑到 php7.0 的冲突 php5:”
- linux - 从 Bash 中的变量中获取特定值
- laravel - Laravel 自定义异常给了我 Undefined property::$headers
- gmail - gmail 邮件 ID 到 url 的映射
- html - UL 中的对角线
- php - 计算没有日期的日期之间的月份差异
- javascript - 在 JavaScript 中删除外部对象数组