import React from 'react'; import { SafeAreaView, ScrollView, StatusBar, StyleSheet, Text, Image, TouchableOpacity, View, FlatList, ActivityIndicator, RefreshControl, ViewStyle } from 'react-native'; import { colors, deepCopy, filterSqlRows, onePx, px2dp } from '../../utils'; import KitEmpty from './KitEmpty'; import KitLoadingMore from './KitLoadingMore'; import KitFootLine from './KitFootLine'; interface Props { tabLabel?: string listHeaderElement?: any customRenderItemFun: Function getDataFun: Function startGetData?: Function finishGetData?: Function topOffset?: number filter?: object disableRefresh?: boolean isSubList?: boolean listContentStyle?: ViewStyle listContainerStyle?: ViewStyle onHandleRefresh?: Function listFilter?: Function } interface State { list: Array isEnd: boolean isLoading: boolean refreshing: boolean } export default class MockListView extends React.Component { constructor(props: Props) { super(props); }; public readonly state: Readonly = { list: [], isEnd: false, refreshing: true, isLoading: false, }; public isPullRefreshing = false; public isLoadingMoreData = false; public page = 1; componentDidMount() { this.initData(); } componentWillUnmount() { this.setState = (state, callback) => { return; } }; handleUpdateListData(list) { this.setState({ list: list }) }; handleRefresh() { const { onHandleRefresh } = this.props; onHandleRefresh && onHandleRefresh(); this.page = 1; this.initData(); }; refresh() { this.page = 1; this.initData(); }; initData = async () => { const { filter, getDataFun, startGetData, finishGetData, listFilter } = this.props; console.log("list-view初始化") this.isPullRefreshing = true; this.isLoadingMoreData = true; this.setState({ refreshing: true }) startGetData && startGetData(); const data = await getDataFun(filter ? filter : {}, 1); finishGetData && finishGetData(); let result = filterSqlRows(data); if(listFilter){ console.log("开始筛选") result = listFilter(result); } console.log(result) this.isPullRefreshing = false; this.isLoadingMoreData = false; if (result.length >= 10) { this.setState({ isEnd: false }); this.page = 2; } else { this.setState({ isEnd: true }); } this.setState({ list: result, refreshing: false }) }; getMoreData = async () => { if (this.isLoadingMoreData || this.isPullRefreshing || this.state.isEnd) { return false; } const { filter, getDataFun, startGetData, finishGetData, listFilter } = this.props; console.log("list-view 加载更多中") this.isPullRefreshing = true; this.isLoadingMoreData = true; startGetData && startGetData(); const data = await getDataFun(filter ? filter : {}, this.page); finishGetData && finishGetData(); let result = filterSqlRows(data); if(listFilter){ result = listFilter(result); } this.isPullRefreshing = false; this.isLoadingMoreData = false; this.page = this.page + 1; if (result.length >= 10) { this.setState({ isEnd: false }); } else { this.setState({ isEnd: true }) } let list = this.state.list; list = list.concat(result); this.setState({ list: list }) }; getCurrentListData() { const { list } = this.state; return list; }; render_item({ item, index }) { const { customRenderItemFun } = this.props; const { list } = this.state; return customRenderItemFun(item, index, list) }; render_footer() { const { isSubList = false } = this.props; const { isEnd, list } = this.state; if (list.length == 0 || isSubList) { return null; } if (isEnd) { return ( ) } else { return ( ) } }; render() { const { listHeaderElement, topOffset = 0, disableRefresh = false, listContentStyle, listContainerStyle } = this.props; const { list, isEnd, isLoading, refreshing } = this.state; return ( { this.handleRefresh() }} /> } data={list} style={[styles.container, listContainerStyle]} contentContainerStyle={[styles.content, { paddingTop: topOffset }, listContentStyle]} keyExtractor={(item, index) => index + ''} renderItem={this.render_item.bind(this)} ListHeaderComponent={listHeaderElement ? listHeaderElement : null} ListFooterComponent={this.render_footer.bind(this)} ListEmptyComponent={} onEndReached={this.getMoreData.bind(this)} onEndReachedThreshold={0.2} /> ) } } const styles = StyleSheet.create({ container: { width: '100%', flex: 1, }, content: { width: '100%', justifyContent: 'flex-start', alignItems: 'center' }, remarkLineRow: { width: '100%', height: px2dp(52), flexDirection: 'row', justifyContent: 'center', alignItems: 'center', paddingHorizontal: px2dp(13) }, remarkline: { flex: 1, height: 1, borderBottomWidth: onePx, borderBottomColor: '#d2d2d2' }, remarkLineText: { fontSize: 14, color: '#909399', marginHorizontal: px2dp(6), fontWeight: '400' }, })