Skip to content

Commit

Permalink
feat(android): add onTopReached event for RecyclerView
Browse files Browse the repository at this point in the history
  • Loading branch information
siguangli2018 authored and siguangli committed Jun 3, 2021
1 parent c99ab9f commit 53dc4a5
Showing 1 changed file with 33 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
public class RecyclerViewEventHelper extends OnScrollListener implements OnLayoutChangeListener,
OnAttachStateChangeListener, OverPullListener {

public static final String EVENT_ON_END_REACHED = "onEndReached";
public static final String EVENT_ON_TOP_REACHED = "onTopReached";
public static final String INITIAL_LIST_READY = "initialListReady";
protected final HippyRecyclerView hippyRecyclerView;
private boolean scrollBeginDragEventEnable;
Expand Down Expand Up @@ -177,16 +179,27 @@ public void onScrollStateChanged(RecyclerView recyclerView, int newState) {

@Override
public void onScrolled(@NonNull final RecyclerView recyclerView, int dx, int dy) {
checkSendOnScrollEvent();
if (dx != 0 || dy != 0) {
checkSendOnScrollEvent();
}

checkSendExposureEvent();

if (!recyclerView.canScrollVertically(1)) {
sendOnReachedEvent();
sendOnEndReachedEvent();
}

if (!recyclerView.canScrollVertically(-1)) {
sendOnTopReachedEvent();
}
}

protected void sendOnEndReachedEvent() {
new HippyViewEvent(EVENT_ON_END_REACHED).send(getParentView(), null);
}

protected void sendOnReachedEvent() {
HippyViewEvent onEndReachedEvent = new HippyViewEvent("onEndReached");
onEndReachedEvent.send(getParentView(), null);
protected void sendOnTopReachedEvent() {
new HippyViewEvent(EVENT_ON_TOP_REACHED).send(getParentView(), null);
}

private void checkSendOnScrollEvent() {
Expand Down Expand Up @@ -256,8 +269,20 @@ public void setScrollEventThrottle(int scrollEventThrottle) {

public final HippyMap generateScrollEvent() {
HippyMap contentOffset = new HippyMap();
contentOffset.pushDouble("x", PixelUtil.px2dp(0));
contentOffset.pushDouble("y", PixelUtil.px2dp(hippyRecyclerView.getContentOffsetY()));

float offsetX = hippyRecyclerView.getContentOffsetX();
float offsetY = hippyRecyclerView.getContentOffsetY();

if (offsetX != 0) {
offsetX = PixelUtil.px2dp(offsetX);
}

if (offsetY != 0) {
offsetY = PixelUtil.px2dp(offsetY);
}

contentOffset.pushDouble("x", offsetX);
contentOffset.pushDouble("y", offsetY);
HippyMap event = new HippyMap();
event.pushMap("contentOffset", contentOffset);
return event;
Expand Down Expand Up @@ -350,7 +375,7 @@ public void onViewDetachedFromWindow(View v) {
public void onOverPullStateChanged(int oldState, int newState, int offset) {
LogUtils.d("QBRecyclerViewEventHelper", "oldState:" + oldState + ",newState:" + newState);
if (oldState == OverPullHelper.OVER_PULL_NONE && isOverPulling(newState)) {
sendOnReachedEvent();
sendOnEndReachedEvent();
getOnScrollDragStartedEvent().send(getParentView(), generateScrollEvent());
}
if (isOverPulling(oldState) && isOverPulling(newState)) {
Expand Down

0 comments on commit 53dc4a5

Please sign in to comment.