Tree View:
/**
* create: 2018-10-23 (10:40 早)
* email : nur01@qq.com
*/
import React, {Component} from 'react'
import {
View,
FlatList,
Platform,
LayoutAnimation,
UIManager,
Animated,
Text,
Image,
TouchableOpacity
} from "react-native";
export default class NurTreeView extends Component {
propTypes: {
data: PropTypes.isRequired,//array([])
onItemClick: PropTypes.isRequired,//func
titleKey: PropTypes.string,
};
/**
* render flatList item
* @param item
* @param index
* @param style
* @returns {*}
*/
renderItem(item, index, style) {
return (<View
style={[, style]}>
<TouchableOpacity
onPress={() => this.props.onItemClick && this.props.onItemClick(item)}
style={{
justifyContent: 'space-between',
flexDirection: 'row',
alignItems: 'center',
}}
>
<View style={[{
flexDirection: 'row',
alignItems: 'center'
}]}>
<Image
style={[{tintColor: 'white', width: 32, height: 32, margin: 8, marginLeft: 16}]}
source={require('../res/images/ic_avatar.png')}/>
<Text style={{color: 'white'}}>{item[this.props.titleKey]}</Text>
</View>
<Image
style={[{width: 12, height: 12, tintColor: 'white'}]}
source={!item.isOpen ? require('../res/images/ic_add.png') :
require('../res/images/ic_reduce.png')}/>
</TouchableOpacity>
{/**items*/}
{item.isOpen ? this.renderFlatList(item.data,
({item, index}) => this.renderItem(item, index, {
paddingLeft: 20,
})) : null}
</View>);
}
/**
* render FlatList
*/
renderFlatList(data, renderItem) {
return <FlatList
data={data}
renderItem={renderItem}
extraData={this.props}
/>
}
render() {
return (<View style={[{flex: 1}, ...this.props]}>
{this.renderFlatList(this.props.data,
({item, index}) => this.renderItem(item, index, {
marginTop: 8,
backgroundColor: index % 2 === 0 ? '#006c9b' : '#009a4e',
paddingRight: 16
}))}
</View>)
}
}
使用方式:
import React, {Component} from 'react'
import {View} from "react-native";
import NurTreeView from "../../utils/NurTreeView";
export default class TreeViewPage extends Component {
// 构造
constructor(props) {
super(props);
let _data = [];
for (let i = 0; i < 20; i++) {
_data.push({name: 'this is a title' + i, isOpen: false, data: []})
}
// 初始状态
this.state = {
data: _data,
};
}
/**
* 点击item
* */
onMoreClick(item) {
item.isOpen = !item.isOpen;
item.data = [
{
name: "item name",
isOpen: false,
data: []
},
{
name: "item name",
isOpen: false,
data: []
},
];
this.setState({
test: 0,
})
}
render() {
return (<View style={{flex: 1}}>
{/**tree view*/}
<NurTreeView
data={this.state.data}
onItemClick={(item) => this.onMoreClick(item)}
titleKey={'name'}/>
</View>)
}
}