react-native分组列表SectionList

前端之家收集整理的这篇文章主要介绍了react-native分组列表SectionList前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

学习交流:https://gitee.com/potato512/Learn_ReactNative

react-native学习交流QQ群:806870562


效果

代码示例

  1. import React,{ Component } from 'react';
  2. import {
  3. StyleSheet,View,Text,SectionList
  4. } from 'react-native';
  5.  
  6. type Props = {};
  7. export default class SectionListPage extends Component<Props> {
  8.  
  9. // 定义分组中的每个单元格
  10. listRow = (info) => {
  11. var txt = ' ' + info.item.type;
  12. return <Text style={styles.sectionItem}>{txt}</Text>
  13. };
  14.  
  15. // 定义分组
  16. listSection = (info) => {
  17. var txt = info.section.key;
  18. return <View style={styles.sectionHeader}><Text style={styles.sectionTxtHeader}>{txt}</Text></View>
  19. };
  20.  
  21. render() {
  22.  
  23. // 定义数据源
  24. let sections = [
  25. { key: "UI View",data: [{ type: "View" },{ type: "Text" },{ type: "Image" },{type: "TextInput"},{type: "StyleSheet"},{type: "ScrollView"}] },{ key: "UI Other",data: [{ type: "ActivityIndicator" },{ type: "Alert" },{ type: "Animated" },{ type: "CameraRoll" },{ type: "Clipboard" },{type:"Demensions"},{type:"KeyboardAvoiding"},{type:"Linking"},{type:"Modal"},{type:"PixedRation"},{type:"RefreshControl"},{type:"StatusBar"},{type:"WebView"}] },{ key: "UI List",data: [{ type: "FlatList" },{ type: "SectionList" }] },{ key: "UI iOS",data: [{ type: "ActionSheet" },{ type: "AlertIOS" },{ type: "DatePickerIOS" },{ type: "ImagePickerIOS" },{ type: "NavigatorIOS" },{ type: "ProgressView" },{type:"PushNotificationIOS"},{type:"SegmentControlIOS"},{type:"TabBarIOS"}] },{ key: "UI Android",data: [{ type: "BackHandleAndroid" },{ type: "DatePickerAndroid" },{type:"DrawLayoutAndroid"},{type:"PermissionsAndroid"},{type:"ProgressBarAndroid"},{type:"TimePickerAndroid"},{type:"ToastAndroid"},{type:"ToolbarAndroid"},{type:"ViewAndroid"}] },{ key: "UI userInterface",data: [{ type: "Button" },{ type: "Picker" },{type: "Slider"},{type:"Switch"}] },];
  26.  
  27. return(
  28. <View>
  29. /* 分组列表*/
  30. <SectionList
  31. renderSectionHeader={this.listSection}
  32. renderItem={this.listRow}
  33. ItemSeparatorComponent={() => <View style={{height:0.5,backgroundColor:"#FF0000"}}></View>}
  34. ListHeaderComponent={HeaderComponent}
  35. ListFooterComponent={FooterComponent}
  36. sections={sections}>
  37. </SectionList>
  38.  
  39. </View>
  40. )
  41. }
  42. }
  43.  
  44. // 定义分组列表表头
  45. const HeaderComponent = (() => <View style={styles.headerStyle}><Text style={styles.headerTxtStyle}>组件学习</Text></View>);
  46. // 定义分组列表表尾
  47. const FooterComponent = (() => <View style={styles.footerStyle}><Text style={styles.footerTxtStyle}>组件学习</Text></View>);
  48.  
  49. // 样式定义
  50. const styles = StyleSheet.create({
  51. headerStyle: {
  52. height: 50,paddingHorizontal:20,backgroundColor:"#E6E6FA"
  53. },headerTxtStyle: {
  54. lineHeight: 50,textAlign:"left",fontSize:30,color:"#8A2BE2"
  55. },footerStyle: {
  56. height: 50,backgroundColor:"#000000"
  57. },footerTxtStyle: {
  58. lineHeight:50,textAlign:"right",fontSize:20,color:"#EE82EE"
  59. },sectionHeader: {
  60. height:40,paddingHorizontal:10,justifyContent:"center",// alignItems:"center",},sectionTxtHeader: {
  61. fontSize:26,sectionItem: {
  62. height:30,textAlign: "center",color: "#FFFFFF",fontSize:24,backgroundColor: '#9CEBBC'
  63. }
  64. });

在App.js中的引用

  1. import SectionListPage from "./Components/ListPage/SectionListPage";
  2.  
  3. type Props = {};
  4. export default class App extends Component<Props> {
  5. render() {
  6. return (
  7. <SectionListPage/>
  8. );
  9. }
  10. }

猜你在找的React相关文章