Skip to content

Commit

Permalink
Add scrolling events.
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioEstevao committed May 18, 2018
1 parent d88c080 commit 1b7061d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
22 changes: 22 additions & 0 deletions ios/RNRecyclerListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions ios/RNRecyclerListViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions src/RecyclerViewList.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 1b7061d

Please sign in to comment.