Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(android): check send reach end event #4100

Merged
merged 4 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ export default class ListExample extends React.Component {
interItemSpacing={interItemSpacing}
numberOfItems={dataSource.length}
contentInset={contentInset}
preloadItemNumber={4}
preloadItemNumber={12}
style={{ flex: 1 }}
onScroll={this.onScroll}
renderBanner={this.renderBanner}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ public boolean hasPullHeader() {
return headerRefreshHelper != null;
}

public boolean hasPullFooter() {
return footerRefreshHelper != null;
}

public boolean hasBannerView() {
ListItemRenderNode node;
if (hasPullHeader()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@ public void setListData() {
overPullHelper.enableOverPullUp(!listAdapter.hasFooter());
overPullHelper.enableOverPullDown(!listAdapter.hasHeader());
}
if (currentNodeCount > renderNodeCount) {
getRecyclerViewEventHelper().onListDataChanged();
}
renderNodeCount = currentNodeCount;
if (renderNodeCount > 0 && mInitialContentOffset > 0) {
scrollToInitContentOffset();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
public class RecyclerViewEventHelper extends OnScrollListener implements OnLayoutChangeListener,
OnAttachStateChangeListener, HippyOverPullListener {

private static final String TAG = "RecyclerViewEventHelper";
private static final int WATERFALL_SCROLL_RELAYOUT_THRESHOLD = 4;
protected final HippyRecyclerView hippyRecyclerView;
private boolean scrollBeginDragEventEnable;
Expand All @@ -79,7 +80,7 @@ public class RecyclerViewEventHelper extends OnScrollListener implements OnLayou
private boolean isInitialListReadyNotified = false;
private ViewTreeObserver viewTreeObserver;
private OnPreDrawListener preDrawListener;
private boolean isLastTimeReachEnd;
private boolean hasEndReached = false;
private int preloadItemNumber;
private Rect reusableExposureStateRect = new Rect();

Expand Down Expand Up @@ -185,7 +186,8 @@ private void relayoutWaterfallIfNeeded() {
LayoutManager layoutManager = hippyRecyclerView.getLayoutManager();
if (layoutManager instanceof HippyStaggeredGridLayoutManager) {
int[] firstVisibleItem = null;
firstVisibleItem = ((HippyStaggeredGridLayoutManager) layoutManager).findFirstVisibleItemPositions(firstVisibleItem);
firstVisibleItem = ((HippyStaggeredGridLayoutManager) layoutManager).findFirstVisibleItemPositions(
firstVisibleItem);
if (firstVisibleItem != null && (firstVisibleItem[0] <= WATERFALL_SCROLL_RELAYOUT_THRESHOLD)) {
Adapter adapter = hippyRecyclerView.getAdapter();
if (adapter != null) {
Expand Down Expand Up @@ -225,6 +227,10 @@ protected boolean scrollHappened(int dx, int dy) {
return dx != 0 || dy != 0;
}

public void onListDataChanged() {
hasEndReached = false;
}

/**
* 检查是否已经触底,发生onEndReached事件给前端 如果上次是没有到底,这次滑动底了,需要发事件通知,如果上一次已经是到底了,这次到底不会发事件
*/
Expand All @@ -235,10 +241,10 @@ private void checkSendReachEndEvent() {
} else {
isThisTimeReachEnd = isVerticalReachEnd();
}
if (!isLastTimeReachEnd && isThisTimeReachEnd) {
if (!hasEndReached && isThisTimeReachEnd) {
sendOnReachedEvent();
}
isLastTimeReachEnd = isThisTimeReachEnd;
hasEndReached = isThisTimeReachEnd;
}

private int findLastVisibleItemMaxPosition() {
Expand Down Expand Up @@ -295,6 +301,7 @@ private boolean isHorizontalReachEnd() {
}

protected void sendOnReachedEvent() {
LogUtils.d(TAG, "sendOnReachedEvent: ");
EventUtils.sendComponentEvent(getParentView(), EventUtils.EVENT_RECYCLER_END_REACHED, null);
EventUtils.sendComponentEvent(getParentView(), EventUtils.EVENT_RECYCLER_LOAD_MORE, null);
}
Expand Down Expand Up @@ -404,14 +411,32 @@ public HashMap<String, Object> generateWaterfallViewScrollEvent() {
first = positions[i];
}
}
scrollEvent.put("firstVisibleRowIndex", first);
positions = layoutManager.findLastVisibleItemPositions(null);
int end = positions[0];
for (int i = 0; i < positions.length; ++i) {
if (end < positions[i]) {
end = positions[i];
}
}
Adapter adapter = hippyRecyclerView.getAdapter();
if (adapter instanceof HippyRecyclerListAdapter) {
HippyRecyclerListAdapter listAdapter = ((HippyRecyclerListAdapter) adapter);
int count = listAdapter.getItemCount();
// Android includes a pull header and a pull footer when calculating the position of an item. In order to
// align with iOS, if a pull header is included, the first item and last item position needs to be
// subtracted by 1
if (listAdapter.hasPullHeader()) {
first = Math.max(0, (first - 1));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里计算是否可加点注释

end = Math.max(0, (end - 1));
count -= 1;
}
// For align with iOS, if a pull footer is included, the last item position needs to be
// subtracted by 1
if (listAdapter.hasPullFooter() && (end == (count - 1))) {
end = Math.max(0, (end - 1));
}
}
scrollEvent.put("firstVisibleRowIndex", first);
scrollEvent.put("lastVisibleRowIndex", end);
ArrayList<Object> rowFrames = new ArrayList<>();
int total = hippyRecyclerView.getChildCount();
Expand Down
Loading