From 1b7061d5bd43bbc33fc4d392384de5bf65d679c1 Mon Sep 17 00:00:00 2001 From: Sergio Estevao Date: Fri, 18 May 2018 12:05:50 +0100 Subject: [PATCH] Add scrolling events. --- ios/RNRecyclerListView.swift | 22 ++++++++++++++++++++++ ios/RNRecyclerListViewManager.m | 2 ++ src/RecyclerViewList.ios.js | 14 ++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/ios/RNRecyclerListView.swift b/ios/RNRecyclerListView.swift index c3a6e2f85d740..c6a3f1fbead23 100644 --- a/ios/RNRecyclerListView.swift +++ b/ios/RNRecyclerListView.swift @@ -3,6 +3,10 @@ import UIKit @objc class RecyclerListView: UIView { + public var onVisibleItemsChange: RCTBubblingEventBlock? + public var firstVisibleIndex = -1 + public var lastVisibleIndex = -1 + fileprivate var data = [RecyclerListItemView]() let cellIdentifier = "RecycleCell" @@ -23,6 +27,7 @@ import UIKit func commonInit() { tableView.dataSource = self + tableView.delegate = self tableView.register(WrapperCell.self, forCellReuseIdentifier: cellIdentifier) tableView.rowHeight = UITableViewAutomaticDimension tableView.estimatedRowHeight = 80 @@ -81,6 +86,23 @@ extension RecyclerListView: UITableViewDataSource { } } +extension RecyclerListView: UITableViewDelegate { + + func scrollViewDidScroll(_ scrollView: UIScrollView) { + + guard let visibleRows = tableView.indexPathsForVisibleRows, + let firstIndex = visibleRows.first?.row, + let lastIndex = visibleRows.last?.row else { + return + } + if firstVisibleIndex != firstIndex || lastVisibleIndex != lastIndex { + firstVisibleIndex = firstIndex + lastVisibleIndex = lastIndex + onVisibleItemsChange?(["firstIndex": firstIndex, "lastIndex": lastIndex]) + } + } +} + extension RecyclerListView { @objc var dataSize: Int { diff --git a/ios/RNRecyclerListViewManager.m b/ios/RNRecyclerListViewManager.m index e452546ebaa71..662b972bc7990 100644 --- a/ios/RNRecyclerListViewManager.m +++ b/ios/RNRecyclerListViewManager.m @@ -79,6 +79,8 @@ - (void)executeBlock:(ListViewBlock)block onNode:(NSNumber *)node { view.dataSize = [RCTConvert NSInteger:json]; } +RCT_EXPORT_VIEW_PROPERTY(onVisibleItemsChange, RCTBubblingEventBlock) + RCT_EXPORT_MODULE() @end diff --git a/src/RecyclerViewList.ios.js b/src/RecyclerViewList.ios.js index a156c3d83d973..ff63c191ba5a2 100644 --- a/src/RecyclerViewList.ios.js +++ b/src/RecyclerViewList.ios.js @@ -289,6 +289,20 @@ class RecyclerViewList extends React.PureComponent { return this._shouldUpdateAll || this._shouldUpdateKeys.includes(itemKey); } + _handleVisibleItemsChange = ({nativeEvent}) => { + var firstIndex = nativeEvent.firstIndex; + var lastIndex = nativeEvent.lastIndex; + this.setState({ + firstVisibleIndex: firstIndex, + lastVisibleIndex: lastIndex, + }); + + const { onVisibleItemsChange } = this.props; + if (onVisibleItemsChange) { + onVisibleItemsChange(nativeEvent); + } + } + _notifyItemMoved(currentPosition, nextPosition) { UIManager.dispatchViewManagerCommand( ReactNative.findNodeHandle(this),