react-native - 反应原生的平面列表行的全局或静态状态
问题描述
是否有可能对于所有行Flatlist
都有一个static state
。我有一个Component
我只想在Flatlist
单击它时显示在 's 的一行上。当我点击其他行时,Flatlist
不Component
应该在上一行呈现,而是在当前点击的行上呈现。
在这里,我有两行 Flatlist。我只想为单击的卡片视图或播放按钮呈现进度条。Flatlist
所以我的逻辑是当我单击 Button 然后我以某种方式将 clicked行的 keyID 放在全局 state
中,这只会呈现单击的 Card View 的进度条。不知何故喜欢这段代码:
{ GlobalState===this.props.key && (<ProgressBar />)}
作为平面列表中每一行渲染的检查器
解决方案
我最终在组件和组件的道具中使用了全局变量。
在:renderItem
_Flatlist
renderItem
中的函数Flatlist
_play = () => {
this.setState({ playState: "playing" });
TrackPlayer.play();
const thisplaying = this.props.item.Key;
ProgressBar.setCurrentPlayer(thisplaying);//Here I call to put the Key of Item to the component's global variable
};
在渲染中使用renderItem
组件Flatlist
<ProgressBar PlayerKey={this.props.item.Key} />
在 Component{ ProgressBar.js
} 中使用的 renderItem 中Flatlist
:
import React from "react";
import { View, Slider, Text} from "react-native";
import TrackPlayer, { ProgressComponent } from "react-native-track-player";
import { formatTime } from "./utils";
global.MineKey = null; //Here I used Global Variable
class ProgressBar extends ProgressComponent {
static setCurrentPlayer = player => {
global.MineKey = player; //Setting global variable on every call from the cardview upon play
};
onSliderEditStart = () => {
TrackPlayer.pause();
};
onSliderEditEnd = () => {
TrackPlayer.play();
};
onSliderEditing = value => {
TrackPlayer.seekTo(value);
};
render() {
const position = formatTime(Math.floor(this.state.position));
const duration = formatTime(Math.floor(this.state.duration));
return global.MineKey == this.props.PlayerKey ? ( //If Global variable key is equal to the Key of RowItem of Flatlist
<View style={{ flexDirection: "row",alignItems: "center", flex: 1,width: "100%"}}>
<Text style={{ color: "white", alignSelf: "center" }}>{position}</Text>
<Slider
onTouchStart={this.onSliderEditStart}
onTouchEnd={this.onSliderEditEnd}
onValueChange={this.onSliderEditing}
value={this.state.position}
maximumValue={this.state.duration}
maximumTrackTintColor="gray"
minimumTrackTintColor="white"
thumbTintColor="white"
style={{
flex: 1,
alignSelf: "center"
}}
/>
<Text style={{ color: "white", alignSelf: "center" }}>{duration}</Text>
</View>
) : null;
}
}
module.exports = ProgressBar;
推荐阅读
- combobox - QML ComboBox 只工作一次
- c# - ApplicationInstance.Start(ServerManager) 和 ServerManager.Start(ApplicationInstance) 的区别
- node.js - 凭证未在 AWS 的 Claudiajs hello 单词示例中加载
- javascript - 为什么我的 Javascript 内容在网页中不可见
- powershell - mkdir 与 New-Item 是相同的 cmdlet 吗?
- hive - MSCK REPAIR 配置单元外部表
- c# - Gridview 中的 TextBox 返回 null 或 'System.Web.UI.WebControls.TextBox'
- node.js - 无法调试 NodeJS 应用程序
- java - android中的java.lang.nullpointer错误
- powershell - 多次替换问题