react native中手风琴组件react-native-collapsible的使用方法

有一个需求需要在app上实现一个比较美观的手风琴效果,这里推荐react-native-collapsible这个开源的组件,GitHub地址:https://github.com/oblador/react-native-collapsible

一、组件demo

我实现的手风琴效果图如下:

二、组件API

使用组件主要还是学习组件相关的api以及api对应的功能,react-native-collapsible组件的API如下:

三、demo详细代码 

手风琴组件详细代码:

import React, {Component, useState} from 'react';
import Accordion from 'react-native-collapsible/Accordion';
import {View, Text, SafeAreaView} from "react-native";



export default function Test(){
    const [activeSections,setActiveSections] = useState([5]);
    const SECTIONS = [
        {
            title: 'Title1',
            content: 'Content1',
            bg:'#9085DE',
        },
        {
            title: 'Title2',
            content: 'Content2',
            bg:'#8698EE',
        },
        {
            title: 'Title3',
            content: 'Content3',
            bg:'#79B0FF',
        },
        {
            title: 'Title4',
            content: 'Content4',
            bg:'#52B6E6',
        },
        {
            title: 'Title5',
            content: 'Content5',
            bg:'#49CCA4',
        },
        {
            title: 'Title6',
            content: 'Content6',
            bg:'#28978B',
        },
    ];

    //手风琴的onChange方法
    const _updateSections = (activeSections) => {
        console.log(activeSections,'activeSections')
        if(activeSections.length==0){
            setActiveSections([5])
        }else{
            setActiveSections(activeSections)
        }

    };
    //渲染每个内容
    const  _renderContent = (section) => {
        return (
            <View style={{flexDirection:'row',justifyContent:'center'}}>
                <View style={{backgroundColor:section.bg,height:480,width:'100%',borderLeftWidth:2,borderRightWidth:2,borderColor:'#fff'}}>
                    <Text>{section.content}</Text>
                </View>
            </View>
        );
    };
    //渲染每个标题
    const  _renderHeader = (section) => {
        return (
            <View style={{flexDirection:'row',justifyContent:'center'}}>
                <View style={{height:60,backgroundColor:section.bg,width:"100%",borderTopRightRadius:20,borderTopLeftRadius:20,marginTop:-10,borderColor:'#fff',borderTopWidth:2,borderLeftWidth:2,borderRightWidth:2}}>
                    <Text style={{color:'#fff',fontSize:18,lineHeight:45,paddingLeft:10}}>{section.title}</Text>
                </View>
            </View>
        );
    };

    return(
        <View style={{height:'100%',backgroundColor:'#fff'}}>
            <SafeAreaView>
                <Accordion
                    duration={500}//手风琴关闭打开时间
                    align={'center'}//过渡时的对齐方式
                    underlayColor={'#00000000'}//点击标题的背景色
                    sections={SECTIONS}//展示的数据,是个数字
                    activeSections={activeSections}//手风琴打开的索引值
                    renderHeader={_renderHeader}//渲染标题
                    renderContent={_renderContent}//渲染内容
                    onChange={_updateSections}//改变方法,获取改变的索引值
                />
            </SafeAreaView>
        </View>
    )
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
React Native ,我们可以使用基本的组件和自定义组件来构建应用程序界面。以下是使用基本组件和自定义组件的示例: 使用基本组件: ```jsx import React from 'react'; import { View, Text, StyleSheet } from 'react-native'; const App = () => { return ( <View style={styles.container}> <Text style={styles.text}>Hello, React Native!</Text> </View> ); }; const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', }, text: { fontSize: 20, fontWeight: 'bold', }, }); export default App; ``` 使用自定义组件: ```jsx import React from 'react'; import { View, Text, StyleSheet } from 'react-native'; const MyComponent = ({ title, message }) => { return ( <View style={styles.container}> <Text style={styles.title}>{title}</Text> <Text style={styles.message}>{message}</Text> </View> ); }; const App = () => { return ( <View style={styles.container}> <MyComponent title="Welcome" message="This is a custom component in React Native." /> </View> ); }; const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', }, title: { fontSize: 24, fontWeight: 'bold', }, message: { fontSize: 16, marginVertical: 10, }, }); export default App; ``` 在上面的示例,我们定义了一个名为`MyComponent`的自定义组件,并将其作为子组件添加到`App`组件。在`MyComponent`组件,我们可以使用`props`来接收来自父组件的数据,并根据需要渲染内容。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值