我组装了一个简单的 React-native 应用程序来从远程服务获取数据,并将其加载到 FlatList 中。当用户点击一个项目时,它应该被突出显示并且应该保留选择。我相信这样一个微不足道的操作应该不难。我不确定我错过了什么。
import React, { Component } from 'react';
import {
StyleSheet,
Text,
View,
FlatList,
ActivityIndicator,
Image,
TouchableOpacity,
} from 'react-native';
export default class BasicFlatList extends Component {
constructor(props) {
super(props);
this.state = {
loading: false,
data: [],
page: 1,
seed: 1,
error: null,
refreshing: false,
selectedItem:'null',
};
}
componentDidMount() {
this.makeRemoteRequest();
}
makeRemoteRequest = () => {
const {page, seed} = this.state;
const url = `https://randomuser.me/api/?seed=${seed}&page=${page}&results=20`;
this.setState({loading: true});
fetch(url)
.then(res => res.json())
.then(res => {
this.setState({
data: page === 1 ? res.results : [...this.state.data, ...res.results],
error: res.error || null,
loading: false,
refreshing: false
});
})
.catch(error => {
this.setState({error, loading: false});
});
};
onPressAction = (rowItem) => {
console.log('ListItem was selected');
console.dir(rowItem);
this.setState({
selectedItem: rowItem.id.value
});
}
renderRow = (item) => {
const isSelectedUser = this.state.selectedItem === item.id.value;
console.log(`Rendered item - ${item.id.value} for ${isSelectedUser}`);
const viewStyle = isSelectedUser ? styles.selectedButton : styles.normalButton;
return(
<TouchableOpacity style={viewStyle} onPress={() => this.onPressAction(item)} underlayColor='#dddddd'>
<View style={styles.listItemContainer}>
<View>
<Image source={{ uri: item.picture.large}} style={styles.photo} />
</View>
<View style={{flexDirection: 'column'}}>
<View style={{flexDirection: 'row', alignItems: 'flex-start',}}>
{isSelectedUser ?
<Text style={styles.selectedText}>{item.name.first} {item.name.last}</Text>
: <Text style={styles.text}>{item.name.first} {item.name.last}</Text>
}
</View>
<View style={{flexDirection: 'row', alignItems: 'flex-start',}}>
<Text style={styles.text}>{item.email}</Text>
</View>
</View>
</View>
</TouchableOpacity>
);
}
render() {
return(
<FlatList style={styles.container}
data={this.state.data}
renderItem={({ item }) => (
this.renderRow(item)
)}
/>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
marginTop: 50,
},
selectedButton: {
backgroundColor: 'lightgray',
},
normalButton: {
backgroundColor: 'white',
},
listItemContainer: {
flex: 1,
padding: 12,
flexDirection: 'row',
alignItems: 'flex-start',
},
text: {
marginLeft: 12,
fontSize: 16,
},
selectedText: {
marginLeft: 12,
fontSize: 20,
},
photo: {
height: 40,
width: 40,
borderRadius: 20,
},
});
当用户点击列表中的一个项目时,“onPress”方法会被调用,其中包含有关所选项目的信息。但是Flatlist中的highlight item的下一步并没有发生。 ‘UnderlayColor’ 也无济于事。
任何帮助/建议将不胜感激。
原文由 Krishnan Sriram 发布,翻译遵循 CC BY-SA 4.0 许可协议
代替
this.state.selectedItem
并设置/检查rowItem.id.value
,我建议使用带有键:值对的 Map 对象,如 RN FlatList 文档示例所示: https:// facebook.github.io/react-native/docs/flatlist.html 。还请查看 js Map 文档: https ://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map。@jI-V 推荐的 extraData 属性将确保在 this.state.selected 在选择时发生重新渲染。
您的 onPressAction 显然会从下面的示例中有所改变,具体取决于您是要在任何给定时间限制选择的数量还是不允许用户切换选择等。
此外,虽然没有任何必要,但我喜欢为
renderItem
组件使用另一个类或纯组件;最终看起来像下面这样: