[英]How to open screen wiith click on item in flatlist in react native?
I want to open screen when i press one of this items (images).当我按下其中一项(图像)时,我想打开屏幕。 I need to open Player screen in my app, so it have to be different screen for each item.我需要在我的应用程序中打开播放器屏幕,因此每个项目必须是不同的屏幕。 Is there way to do that?有没有办法做到这一点? Please help me.请帮我。 I cant find any solution on Google.我在谷歌上找不到任何解决方案。 Here is my code:这是我的代码:
Here are my flatlist items (images):这是我的平面清单项目(图像):
class ReadingComponent extends Component {
constructor(props: any) {
super(props);
}
render() {
let categories = [
{
name : "Category 1",
img : require("../Assets/Slika.jpg"),
},
{
name : "Category 2",
img : require("../Assets/Slika.jpg"),
},
{
name : "Category 3",
img : require("../Assets/Slika.jpg"),
},
];
Flatlist:平面列表:
return (
<View style={styles.container}>
<FlatList
data={categories}
showsHorizontalScrollIndicator={false}
numColumns={categories.length / 5}
showsVerticalScrollIndicator={false}
renderItem = {({item, index}) => {
return (
<TouchableWithoutFeedback>
<Surface style={styles.surface} >
<ImageBackground
source={item.img}
style={styles.img}
blurRadius={0.5}>
<Icon name="music" color="#fff" size={22}/>
<Text style={styles.name}>{item.name}</Text>
</ImageBackground>
</Surface>
</TouchableWithoutFeedback>
);
}}
/>
</View>
);
}
}
2 things. 2 件事。 First, you very rarely want to use TouchableWithoutFeedback.首先,您很少想使用 TouchableWithoutFeedback。 Users expect feedback when they touch something that they can interact with - such as a button.用户在触摸可以与之交互的东西(例如按钮)时期望得到反馈。
If you are 100% sure you want it to be without feedback, then continue with that, but I think you likely will want TouchableOpacity instead.如果你 100% 确定你希望它没有反馈,那么继续这样做,但我认为你可能会想要 TouchableOpacity。
The second thing is you are almost there.第二件事是你快到了。 You need to add an onPress
prop to your touchable tag.您需要将onPress
道具添加到您的可触摸标签。 You will want to update your data source so that each ' item
' in it contains the information you need to be able to navigate to your correct screen.您将需要更新您的数据源,以便其中的每个“ item
”都包含您需要能够导航到正确屏幕的信息。 It may be as simple as including another variable named ' screenName
' or the like, or you could get as complicated as adding an entire function that you pass into your onPress
event handler.它可能就像包含另一个名为“ screenName
”等的变量一样简单,或者您可能会变得像添加传递给onPress
事件处理程序的整个 function 一样复杂。 Either way, you know your code better than I, but you just need to add a little data to your categories
array.无论哪种方式,您都比我更了解您的代码,但您只需要向您的categories
数组添加一些数据。
EDIT: Here is an example using the screenName approach mentioned above.编辑:这是使用上述 screenName 方法的示例。
let categories = [
{
name : "Category 1",
img : require("../Assets/Slika.jpg"),
screenName: "PlayerScreen",
},
<TouchableOpacity
onPress={() => {
// navigate to screen named item.screenName
}}
>
Here is update of code.这是代码的更新。
Flatlist items:平面清单项目:
let categories = [
{
name : "Category 1",
img : require("../Assets/Slika.jpg"),
screenName : "Player",
},
onPress:按下:
<TouchableOpacity onPress={() => item.screenName}}>
Here is solution of this problem, but still dont working, because Class have to be converted to Function.这是此问题的解决方案,但仍然无法正常工作,因为 Class 必须转换为 Function。
Here is my Component screen.这是我的组件屏幕。 Here is Class but I want to convert that to Function...这是 Class 但我想将其转换为 Function ...
Here is that Class component I want to convert to function:这是我要转换为 function 的 Class 组件:
class ReadingComponent extends Component {
constructor(props: any) {
super(props);
}
redirectTo(screenName: any) {
useNavigation().navigate(`${screenName}`);
}
Here are FlatList items:以下是 FlatList 项目:
render() {
let categories = [
{
name : "Category 1",
img : require("../Assets/Slika.jpg"),
screenName : "Player",
},
{
name : "Category 2",
img : require("../Assets/Slika.jpg"),
screenName : "Player1",
},
];
Here is return and flatlist details:这是退货和平面清单的详细信息:
return (
<View style={styles.container}>
<FlatList
data={categories}
showsHorizontalScrollIndicator={false}
numColumns={categories.length / 5}
showsVerticalScrollIndicator={false}
renderItem = {({item, index}) => {
return (
<TouchableOpacity
onPress={() => this.redirectTo(item.screenName)}>
</TouchableOpacity>
);
}}
/>
</View>
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.