No Description

RefreshInfiniteListView.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**
  2. * Created by zhanlong on 2016/11/29.
  3. */
  4. 'use strict';
  5. import React, {PureComponent} from "react";
  6. import {FlatList, Text, View, Animated, DeviceEventEmitter, StyleSheet} from "react-native";
  7. import PropTypes from "prop-types";
  8. import ScreenDimensions from './ScreenDimensions'
  9. import NavigationBarHeight from './NavigationBarHeight'
  10. import TabBarHeight from './TabBarHeight'
  11. const TEXT_LIGTH_GRAY_COLOR = '#677b93';//字体灰色
  12. const COLOR_WHITE_GREY = "#F1F1F1";
  13. const COLOR_MIDDLE_GREY = "#888888";
  14. const TABBAR_HEIGHT = 49;
  15. const FONT_SIZE_SUBTITLE = 12;
  16. const AnimatedFlatList = Animated.createAnimatedComponent(FlatList);
  17. const viewHeight = ScreenDimensions.height - NavigationBarHeight.height - TabBarHeight.height - 18;
  18. export default class RefreshInfiniteListView extends PureComponent {
  19. static propTypes = {
  20. ...FlatList.propTypes,
  21. data: PropTypes.array.isRequired,
  22. itemHeight: PropTypes.number,
  23. initialNumToRender: PropTypes.number,
  24. renderEmptyData: PropTypes.func,
  25. onScroll: PropTypes.func.isRequired,
  26. };
  27. static defaultProps = {
  28. data: [],
  29. scrollEnabled: true,
  30. itemHeight: 40,
  31. initialNumToRender: 10,
  32. horizontal:false,
  33. pagingEnabled:false,
  34. showsHorizontalScrollIndicator:true,
  35. renderEmptyData: ()=> {
  36. return (
  37. <View
  38. style={styles.NoDataStyle}>
  39. <Text>没有数据</Text>
  40. </View>
  41. );
  42. }
  43. };
  44. constructor(props) {
  45. super(props);
  46. this.state = {
  47. ...FlatList.defaultProps,
  48. scrollEnabled: this.props.scrollEnabled,
  49. data: this.props.data,
  50. refreshing: false
  51. };
  52. this.rowRefs = [];
  53. }
  54. render() {
  55. var styleArr = [{flex: 1}];
  56. if (this.props.style)styleArr.push(this.props.style);
  57. return (
  58. <FlatList
  59. ref={this.listRef.bind(this)}
  60. refreshing={this.state.refreshing}
  61. onEndReachedThreshold={0.1}
  62. style={styleArr}
  63. showsHorizontalScrollIndicator = {this.props.showsHorizontalScrollIndicator}
  64. pagingEnabled={this.props.pagingEnabled}
  65. numColumns ={this.props.numColumns}
  66. horizontal ={this.props.horizontal}
  67. data={this.state.data}
  68. onScroll={this.onScroll.bind(this)}
  69. initialNumToRender={this.props.initialNumToRender}
  70. getItemLayout={this._getItemLayout.bind(this)}
  71. renderItem={ this._renderItem.bind(this)}
  72. ListHeaderComponent={this._listHeaderComponent.bind(this)}
  73. onRefresh={this.props.onRefresh?this._onRefresh.bind(this):undefined}
  74. onEndReached={this.props.onEndReached?this._onEndReached.bind(this):undefined}
  75. scrollToIndex={this.scrollToIndex.bind(this)}
  76. />
  77. );
  78. }
  79. onScroll(event){
  80. if (this.props.hasOwnProperty("onScroll") && this.props.onScroll != null) {
  81. this.props.onScroll(event.nativeEvent.contentOffset.x)
  82. }
  83. }
  84. _renderItem(data) {
  85. return this.props.renderItem(data.item, data.index);
  86. }
  87. scrollToIndex(params) {
  88. return {animated: params.animated, index: params.index};
  89. }
  90. _listHeaderComponent() {
  91. if (this.state.data.length > 0) {
  92. return null;
  93. } else {
  94. return this.props.renderEmptyData();
  95. }
  96. }
  97. _getItemLayout(data, index) {
  98. return {length: this.props.itemHeight, offset: this.props.itemHeight * index, index};
  99. }
  100. _onRefresh() {
  101. if (this.props.hasOwnProperty("onRefresh") && this.props.onRefresh != null) {
  102. this.props.onRefresh();
  103. }
  104. }
  105. _onEndReached(info) {
  106. if (this.props.hasOwnProperty("onEndReached") && this.props.onEndReached != null) {
  107. this.props.onEndReached();
  108. }
  109. }
  110. listRef(list) {
  111. this.list = list;
  112. }
  113. setRefreshing(value) {
  114. this.setState({
  115. refreshing: value
  116. });
  117. }
  118. setProps(props) {
  119. this.setState(props);
  120. }
  121. setData(data) {
  122. var formatedData = [];
  123. data.forEach((item, index)=> {
  124. item.key = String("index" + index);
  125. formatedData[index] = item;
  126. })
  127. this.setState({
  128. refreshing: false,
  129. data: formatedData
  130. });
  131. }
  132. getData() {
  133. return this.state.data;
  134. }
  135. }
  136. const styles = StyleSheet.create({
  137. View: {
  138. width: ScreenDimensions.width,
  139. height: ScreenDimensions.height - NavigationBarHeight.height - TabBarHeight.height - 18,
  140. marginTop: NavigationBarHeight.height
  141. },
  142. NoDataStyle:{
  143. height:viewHeight*2/3,
  144. width: ScreenDimensions.width,
  145. alignItems:'center',
  146. justifyContent:'center'
  147. }
  148. })