reactjs - 如何将不同的图像作为背景上传到平面列表中的卡片视图
问题描述
再会!我正在使用 API 调用来获取项目详细信息。从 API 我只会得到项目 ID、项目名称、项目描述。我想为卡片中的平面列表视图提供不同的图像作为背景。截至目前,我从 API 获得了 10 个项目。我的项目文件夹中有十张图像。我以数组格式存储并导入到我的主要组件中。但我无法将不同的图像作为背景。任何人都可以帮助你实现这一目标。提前致谢。
//Code to get the images
renderImage = ({ item }) => (
<View>
<ImageBackground source={CardImages[item]} />
</View>
);
//Main Component:
<FlatList
numColumns={numColumns}
data={data}
renderItem={({ item, index }) =>
return (
<Card
onPress={() =>
this.props.navigation.push("ItemDet", {
data: item,
})
}
style={{
margin: 8,
borderRadius: 16,
width: config.deviceWidth * 0.43,
height: config.deviceWidth * 0.54,
overflow: "hidden",
justifyContent: "space-evenly",
marginLeft: 14,
}}
>
<ImageBackground
style={{
height: config.deviceWidth * 0.54,
width: config.deviceWidth * 0.43,
resizeMode: "cover",
}}
renderImage={this.renderImage}//here is the call for dynamic image background
>
</ImageBackground>
</Card>
/>
解决方案
你的代码很奇怪,所以我决定用我的方式修复它的语法,希望你能在这里找到一些有用的东西。
const CardImages = {
firstImage: require('./your-first-image.png'),
secondImage: require('./your-second-image.png'),
thirdImage: require('./your-third-image.png'),
};
return (
<FlatList
numColumns={numColumns}
data={data}
renderItem={({ item, index }) => {
return (
<Card
onPress={() =>
this.props.navigation.push('ItemDet', {
data: item,
})
}
style={{
margin: 8,
borderRadius: 16,
width: config.deviceWidth * 0.43,
height: config.deviceWidth * 0.54,
overflow: 'hidden',
justifyContent: 'space-evenly',
marginLeft: 14,
}}>
<ImageBackground
style={{
height: config.deviceWidth * 0.54,
width: config.deviceWidth * 0.43,
resizeMode: 'cover',
}}
source={CardImages[item]}
/>
</Card>
);
}}
/>
);
推荐阅读
- javascript - 在所有标签中查找单词并在 jQuery 中替换它
- python - 在 Python 中使用 openCV 对矩阵执行 blob 检测
- css - 来自侧边栏关闭(X)图标的警报模式
- asp.net - 为什么我的身份验证状态提供程序不起作用?
- r - 在 R 中复制记录并为每个重复记录输入具有不同值的新列
- c++ - 如何将代码分成多个.cpp文件C++
- c# - 带有实体框架的 Blazor webassembly 失败,并显示“不支持 ContentType;支持的类型是 'application/json'”
- node.js - 在作为firebase函数运行的快速应用程序中获取完整的url——包括函数名?
- word-wrap - iText7 html 到 pdf 不能打断长词
- git - 阻止 git 跟踪临时打开的 Microsoft Office 文件