如何使用Firebase数据库在ListView的末尾添加“加载更多”(本机)

时间:2018-12-13 01:53:19

标签: javascript listview react-native firebase-realtime-database expo

我有一个Firebase数据库,其中包含很多组件,并且通常需要很长时间才能一次加载我正在使用Expo构建的应用程序中的活动。

我当时想在每5个项目后用“查看更多”或“加载更多”来对活动进行分页会更快一些,但是我不知道该怎么做。我已经尝试过:size: 1, firstVisibleRow: 0, leastVisibleRow: 0, greatestVisibleRow: 0, itemsToShow: 1, rowsToDisplay : 1, expanded: false,在代码中的各个位置,但没有一个起作用。

请问有人可以帮我使用ListView解决此问题吗?我在下面附上主要代码:

import React from 'react';

import {
  ListView,
  StyleSheet,
  Text,
  View,
  TouchableHighlight,
  AlertIOS
} from 'react-native';
import {
  Constants
} from 'expo';
import firebase from './firebase';

const StatusBar = require('../Firebase/StatusBar');
const ActionButton = require('../Firebase/ActionButton');
const ListItem = require('../Firebase/ListItemEventsNew');
const styles = require('../Firebase/styles.js');

export default class Events_new extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      dataSource: new ListView.DataSource({
        rowHasChanged: (row1, row2) => row1 !== row2,
      }),
      //size: 1,
      //firstVisibleRow: 0,
      //leastVisibleRow: 0,
      //greatestVisibleRow: 0,
      //itemsToShow: 1,
      //rowsToDisplay : 1,
      //expanded: false
    };
    this.itemsRef = this.getRef().child('-Events');
  }

  getRef() {
    return firebase.database().ref();
  }

  listenForItems(itemsRef) {
    itemsRef.on('value', (snap) => {

      // get children as an array
      var items = [];
      snap.forEach((child) => {
        items.push({
          headline: child.val().headline,
          _key: child.key
        });
      });

      this.setState({
        dataSource: this.state.dataSource.cloneWithRows(items),
        //itemsToShow: 1,
        //rowsToDisplay : 1,
        //expanded: false
      });

    });
  }

  componentDidMount() {
    this.listenForItems(this.itemsRef);
  }

  render() {
    return ( <
      View style = {
        styles.container
      } >
      <
      StatusBar title = "Events" / >
      <
      ListView dataSource = {
        this.state.dataSource
      }
      renderRow = {
        this._renderItem.bind(this)
      }
      enableEmptySections = {
        true
      }
      style = {
        styles.listview
      }
      />

      <
      /View>
    )
  }

  // <ActionButton onPress={this._addItem.bind(this)} title="Add" /> pt butonul Add

  _addItem() {
    AlertIOS.prompt(
      'Add New Item',
      null, [{
          text: 'Cancel',
          onPress: () => console.log('Cancel Pressed'),
          style: 'cancel'
        },
        {
          text: 'Add',
          onPress: (text) => {
            this.itemsRef.push({
              title: text
            })
          }
        },
      ],
      'plain-text'
    );
  }

  _renderItem(item) {

    const onPress = () => {
      AlertIOS.alert(
        'Complete',
        null, [{
            text: 'Complete',
            onPress: (text) => this.itemsRef.child(item._key).remove()
          },
          {
            text: 'Cancel',
            onPress: (text) => console.log('Cancelled')
          }
        ]
      );
    };

    return ( <
      ListItem item = {
        item
      }
      onPress = {
        onPress
      }
      />
    );
  }

}

1 个答案:

答案 0 :(得分:1)

您可以检测到 滚动到达底部 => 获取更多数据 => 更改状态 => 显示更多行

onEndReached={this.loadMoreActivities}

loadMoreActivities = () => {
  // fetch data here
}

您应使用debounce进行优化。关键字debounce lodash