在React Native中折叠项目

时间:2016-11-20 21:01:30

标签: javascript react-native accordion collapse

我想就如何在React原生中制作可折叠商品提出一些建议。我尝试过使用: react-native-collapsible 甚至: react-native-accordion 。但似乎oblador的人不再使用这个版本的React本机,而另一个则很难使用或自定义。这就是我想做的事情:

What it supposes to look like但是 ends up being like this after icon pressed

我将代码分成2个文件,1个用于自定义视图,另一个用于输入其标题和项目。

accordion.js

class AccordionCustom extends Component{
    constructor(props){
        super(props);

        this.icons = {
            'up'    : require('../image/keyboard_arrow_right_black_192x192.png'),
            'down'  : require('../image/keyboard_arrow_down_black_192x192.png')
        };

        this.state = {
          title: props.title,
          expanded: true,
          animation: new Animated.Value()
        };
    }

    toggle(){
        let
        initialValue    = this.state.expanded? this.state.maxHeight + this.state.minHeight : this.state.minHeight,
        finalValue      = this.state.expanded? this.state.minHeight : this.state.maxHeight + this.state.minHeight;

       this.setState({
           expanded : !this.state.expanded
       });

       this.state.animation.setValue(initialValue);
       Animated.spring(
           this.state.animation,
           {
               toValue: finalValue
           }
       ).start();
    }

    _setMaxHeight(event){
        this.setState({
            maxHeight   : event.nativeEvent.layout.height
        });
    }

    _setMinHeight(event){
        this.setState({
            minHeight   : event.nativeEvent.layout.height
        });
    }

    render(){
        let icon = this.icons['down'];

        if(this.state.expanded){
            icon = this.icons['up'];
        }

        return (
          <Animated.View style={[styles.makeup_container,{height: this.state.animation}]}>
            <View style={styles.makeup_layout}
                  onLayout={this._setMinHeight.bind(this)}>
                <TouchableOpacity style={styles.title_container}>
                  <Text style={styles.makeup_text}>{this.state.title}</Text>
                </TouchableOpacity>
                <TouchableHighlight
                   style={styles.icon_container}
                   onPress={this.toggle.bind(this)}
                   underlayColor="#f1f1f1">
                   <Image
                       style={styles.icon_view}
                       source={icon}
                   ></Image>
               </TouchableHighlight>
            </View>
            <View style={styles.children_container}
                  onLayout={this._setMaxHeight.bind(this)}>
                {this.props.children}
            </View>
          </Animated.View>
        );
    }
}

home.js

<View style={styles.accordion_container}>
                  <AccordionCustom title="Trang Điểm"
                                   style={styles.accordion_padding}>
                    <View style={{flex: 1, flexDirection: 'column'}}>
                      <TouchableOpacity style={styles.accordion_comp}><Text style={styles.makeupComp_text}>Sản Phẩm Cho Mắt</Text></TouchableOpacity>
                      <TouchableOpacity style={styles.accordion_comp}><Text style={styles.makeupComp_text}>Sản Phẩm Cho Mặt</Text></TouchableOpacity>
                      <TouchableOpacity style={styles.accordion_comp}><Text style={styles.makeupComp_text}>Sản Phẩm Cho Môi</Text></TouchableOpacity>
                      <TouchableOpacity style={styles.accordion_comp}><Text style={styles.makeupComp_text}>Sản Phẩm Cho Móng</Text></TouchableOpacity>
                      <TouchableOpacity style={styles.accordion_comp}><Text style={styles.makeupComp_text}>Cọ Và Dụng Cụ Khác</Text></TouchableOpacity>
                    </View>
                  </AccordionCustom>
                </View>

请建议您的某个图书馆或无论如何更正我的代码。

3 个答案:

答案 0 :(得分:0)

您可以尝试使用Native来使用手风琴。

https://docs.nativebase.io/Components.html#accordion-def-headref

样本输出:

enter image description here

答案 1 :(得分:0)

使用react-native-collapsible-view
它似乎完全可以满足您的需求。

答案 2 :(得分:0)

试试 react-accessible-accordion,它非常适合我。

用法

首先,从 npm 中获取包:

npm install --save react-accessible-accordion

然后,导入编辑器并在您的代码中使用它。这是一个基本示例:

import React from 'react';

import {
    Accordion,
    AccordionItem,
    AccordionItemHeading,
    AccordionItemButton,
    AccordionItemPanel,
} from 'react-accessible-accordion';

// Demo styles, see 'Styles' section below for some notes on use.
import 'react-accessible-accordion/dist/fancy-example.css';

export default function Example() {
    return (
        <Accordion>
            <AccordionItem>
                <AccordionItemHeading>
                    <AccordionItemButton>
                        What harsh truths do you prefer to ignore?
                    </AccordionItemButton>
                </AccordionItemHeading>
                <AccordionItemPanel>
                    <p>
                        Exercitation in fugiat est ut ad ea cupidatat ut in
                        cupidatat occaecat ut occaecat consequat est minim minim
                        esse tempor laborum consequat esse adipisicing eu
                        reprehenderit enim.
                    </p>
                </AccordionItemPanel>
            </AccordionItem>
            <AccordionItem>
                <AccordionItemHeading>
                    <AccordionItemButton>
                        Is free will real or just an illusion?
                    </AccordionItemButton>
                </AccordionItemHeading>
                <AccordionItemPanel>
                    <p>
                        In ad velit in ex nostrud dolore cupidatat consectetur
                        ea in ut nostrud velit in irure cillum tempor laboris
                        sed adipisicing eu esse duis nulla non.
                    </p>
                </AccordionItemPanel>
            </AccordionItem>
        </Accordion>
    );
}

Style:演示页面使用的样式

import 'react-accessible-accordion/dist/fancy-example.css';