SectionList中包含FlatList实现一行多个item

import React from 'react';
import {SectionList, FlatList, TouchableOpacity, Text, View, Image} from 'react-native';
import {Dimensions} from "react-native";

const {width, height} = Dimensions.get('window');

class MyListItem extends React.PureComponent {

  constructor(props) {
    super(props);
    this.state = {
      isSelected: false
    }
  }

  _onPress = (changeSelected) => {
    this.setState(previousState => (
      {
        isSelected: !previousState.isSelected,
      }
    ));
    changeSelected()
  };

  render() {
    const textColor = 'black';
    const sectionWidth = (width * 0.84 - 4*10)/3;
    const sectionHeight = height/20;
    return (
      <TouchableOpacity onPress={() => this._onPress(this.props.changeSelected)} disabled={this.props.hasSelected && !this.state.isSelected}>
        <View style={{width: sectionWidth, height: sectionHeight, backgroundColor: this.state.isSelected? '#00bfff':'lightgrey',
          alignItems: 'center', justifyContent: 'center', marginHorizontal: 3, borderRadius:15}}>
          <Text style={{textAlign: 'center', color: textColor, fontSize: 12, paddingTop: 2}}>{this.props.title}</Text>
        </View>
      </TouchableOpacity>
    );
  }
}

export default class SelectSec extends React.PureComponent {

  constructor(props) {
    super(props);
    this.state = {
      hasSelected: false
    }
  }

  changeSelected = () => {
    this.setState(previousState => (
      {
        hasSelected: !previousState.hasSelected,
      }
    ));
  };

  _keyExtractor = (item, index) => item.id;

  _ItemSeparatorComponent= () => (
    <View style={{height: 10}}/>
  );

  _renderSectionListItem = ({item}) => (
    <FlatList
      data={item}
      numColumns={3}
      renderItem={this._renderItem}
      keyExtractor={this._keyExtractor}
      ItemSeparatorComponent={this._ItemSeparatorComponent}
    />
  );

  _renderItem = ({item}) => (
    <MyListItem
      id={item.id}
      title={item.title}
      changeSelected={this.changeSelected}
      hasSelected={this.state.hasSelected}
    />
  );

  _renderSectionHeader = ({section}) => (
    <Text style={{fontSize: 18, color: 'black', marginHorizontal: 10, marginTop: 30, marginBottom: 15}}>{section.title}</Text>
  );

  render() {
    const sections = [
      {title: '部门分类', data:[[
        {id:'0', title: '总体'},
        {id:'1', title: '管理信息化部'},
        {id:'2', title: '高性能部'},
        {id:'3', title: '科技云'},
        {id:'4', title: '大数据部'},
        {id:'5', title: '新媒体部'},
        {id:'6', title: '物联网中心'},
        {id:'7', title: '科研信息化部'},
        {id:'8', title: '亚马逊云'},
        {id:'9', title: '矿大附属中学'},
        {id:'10', title: '管理云'},
        {id:'11', title: '宁波材料所'},
        {id:'12', title: '遥地所'},
        ]]
      },
        {title: '部门分类', data:[[
          {id:'0', title: '机器分布'},
          {id:'1', title: '用户分布'},
          {id:'2', title: '存储分布'},
          {id:'3', title: '骨干流量图'},
          {id:'4', title: '机房分布'},
          {id:'5', title: '物联网标识节点'},
          {id:'6', title: '监控设备'},
        ]]
      },
    ];
    return (
      <SectionList
        sections={sections}
        keyExtractor={(item, index) => item + index}
        renderItem={this._renderSectionListItem}
        renderSectionHeader={this._renderSectionHeader}
        numColumns={3}
        columnWrapperStyle={{borderWidth:3, borderColor:'#f4f4f4'}}
      />
    );
  }
}
复制代码
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现吸顶导航和高亮显示当前模块需要进行以下步骤: 1. 在页面添加吸顶导航和对应的模块内容,可以使用`<header>`和`<section>`标签。 2. 在导航绑定点击事件,使用`ref`获取对应的模块节点,然后使用`scrollIntoView()`方法实现滚动到指定模块的效果。 3. 使用`IntersectionObserver`监听每个模块的位置,当某个模块进入视口时,将对应的导航高亮显示。 下面是一个示例代码: ``` <template> <div class="container"> <header ref="header"> <ul> <li v-for="(item, index) in navList" :key="index" @click="scrollToSection(index)" :class="{ active: activeIndex === index }">{{ item }}</li> </ul> </header> <section v-for="(item, index) in sectionList" :key="index" ref="sections"> <h2>{{ item }}</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla nec sapien id dolor commodo accumsan. Nullam auctor, tortor eu elementum lacinia, odio sem eleifend sapien, vel efficitur velit lacus a nisi. Pellentesque sit amet nisi vitae enim pretium feugiat. Maecenas euismod vestibulum nisi, a semper ipsum sollicitudin eu.</p> </section> </div> </template> <script lang="ts"> import { defineComponent, onMounted, ref } from 'vue'; export default defineComponent({ setup() { const navList = ['Section 1', 'Section 2', 'Section 3', 'Section 4']; const sectionList = ['Section 1', 'Section 2', 'Section 3', 'Section 4']; const activeIndex = ref(0); const scrollToSection = (index: number) => { const sections = document.querySelectorAll('section'); sections[index].scrollIntoView({ behavior: 'smooth' }); }; const observer = new IntersectionObserver( (entries) => { entries.forEach((entry) => { if (entry.intersectionRatio > 0.5) { activeIndex.value = Number(entry.target.dataset.index); } }); }, { threshold: 0.5 } ); onMounted(() => { const header = document.querySelector('header'); const sections = document.querySelectorAll('section'); sections.forEach((section, index) => { section.setAttribute('data-index', String(index)); observer.observe(section); }); header?.classList.add('sticky'); }); return { navList, sectionList, activeIndex, scrollToSection, }; }, }); </script> <style scoped> .container { max-width: 800px; margin: 0 auto; padding: 20px; } header { position: relative; z-index: 1; background-color: #fff; padding: 10px; margin-bottom: 20px; border-bottom: 1px solid #e0e0e0; } header.sticky { position: sticky; top: 0; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1); } ul { display: flex; justify-content: space-between; list-style: none; margin: 0; padding: 0; } li { cursor: pointer; padding: 10px; } li.active { font-weight: bold; } section { margin-bottom: 20px; } h2 { margin-top: 0; } </style> ``` 在上面的代码,我们使用了Vue 3的Composition API来管理状态和生命周期。在`setup`函数,我们定义了`navList`和`sectionList`数组来存储导航和模块的内容,以及`activeIndex`变量来跟踪当前活动的模块。然后,我们定义了`scrollToSection`函数来实现点击导航后滚动到指定模块的效果。 在`onMounted`生命周期钩子,我们使用`IntersectionObserver`来监听每个模块的位置,当某个模块进入视口时,将对应的导航高亮显示。我们还使用`classList`来动态添加/删除`sticky`类,实现吸顶导航的效果。 最后,我们将组件导出,可以在其他组件使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值