几天前,我陷入了同样的问题。但现在这段代码对我有用
您的列表视图代码
<ListView
style={styles.container}
dataSource={this.state.dataSource}
renderRow={(data) =>
<View style={{ flex: 1, flexDirection: "column" }}>
<View style={{ flex: 1, flexDirection: "column" }}>
<View style={{ flex: 1, flexDirection: "row", alignItems: "center", justifyContent: "center", marginBottom: 0 }}>
<Text style={{ fontSize: 16, color: "#fff", padding: 5, textAlign: "center", width: 220, paddingLeft: 40, paddingRight: 40, borderStyle: "solid", borderRadius: 100, borderColor: "#fff", borderWidth: 1 }}>
{data.name}
</Text>
</View>
<TouchableHighlight
onPress={() => this.updateIndex(data._id)}>
<View style={{ alignItems: "center", justifyContent: "center", marginBottom: 5 }}>
{this.state.categoryIndex == data._id &&
<Text style={{ color: "#fff" }}>
<IonicIcons name="ios-arrow-up" style={{ fontSize: 20, marginTop: 10, padding: 0 }} />
<Text> Less</Text>
</Text>
}
{this.state.categoryIndex != data._id &&
<Text style={{ color: "#fff" }}>
<IonicIcons name="ios-arrow-down" style={{ fontSize: 20, marginTop: 10, padding: 0 }} />
<Text> More</Text>
</Text>
}
</View>
</TouchableHighlight>
</View>
{this._renderCancel(data)}
</View>
}
您需要调用 updateIndex() 函数。这个函数只会更新需要打开子视图的状态ID
this.state.categoryIndex == data._id 将打开 ListView,如果 this.state.categoryIndex != data._id 它将关闭您的子 ListView
我的 updateIndex() 函数看起来像
updateIndex(index) {
if (this.state.showSubcategory && this.state.categoryIndex == index) {
this.setState({
showSubcategory: !this.state.showSubcategory,
categoryIndex: false
});
} else {
this.setState({
showSubcategory: true,
categoryIndex: index
});
}
}