Skip to content

Commit

Permalink
fix(android): implement preloadItemNumber for ListView
Browse files Browse the repository at this point in the history
  • Loading branch information
iPel authored and zoomchan-cxj committed Oct 11, 2022
1 parent a5e9e00 commit c978c85
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,11 @@ public class HippyRecyclerListAdapter<HRCV extends HippyRecyclerView> extends Ad
protected int positionToCreateHolder;
protected PullFooterRefreshHelper footerRefreshHelper;
protected PullHeaderRefreshHelper headerRefreshHelper;
protected PreloadHelper preloadHelper;

public HippyRecyclerListAdapter(HRCV hippyRecyclerView, HippyEngineContext hpContext) {
this.hpContext = hpContext;
this.hippyRecyclerView = hippyRecyclerView;
hippyItemTypeHelper = new HippyItemTypeHelper(hippyRecyclerView);
preloadHelper = new PreloadHelper(hippyRecyclerView);
}

/**
Expand Down Expand Up @@ -471,14 +469,6 @@ private boolean isPullHeader(int position) {
return false;
}

public PreloadHelper getPreloadHelper() {
return preloadHelper;
}

public void setPreloadItemNumber(int preloadItemNumber) {
preloadHelper.setPreloadItemNumber(preloadItemNumber);
}

@Override
public void getItemLayoutParams(int position, LayoutParams lp) {
if (lp == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public void setscrollEventThrottle(HRW view, int scrollEventThrottle) {

@HippyControllerProps(name = "preloadItemNumber")
public void setPreloadItemNumber(HRW view, int preloadItemNumber) {
getAdapter(view).setPreloadItemNumber(preloadItemNumber);
view.getRecyclerViewEventHelper().setPreloadItemNumber(preloadItemNumber);
}

@HippyControllerProps(name = "suspendViewListener", defaultType = HippyControllerProps.NUMBER, defaultNumber = 0)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.HippyOverPullHelper;
import androidx.recyclerview.widget.HippyOverPullListener;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.OnScrollListener;

Expand Down Expand Up @@ -71,7 +72,7 @@ public class RecyclerViewEventHelper extends OnScrollListener implements OnLayou
private ViewTreeObserver viewTreeObserver;
private OnPreDrawListener preDrawListener;
private boolean isLastTimeReachEnd;

private int preloadItemNumber;

public RecyclerViewEventHelper(HippyRecyclerView recyclerView) {
this.hippyRecyclerView = recyclerView;
Expand Down Expand Up @@ -219,14 +220,22 @@ private void checkSendReachEndEvent() {
* 竖向滑动,内容已经到达最下边
*/
private boolean isVerticalReachEnd() {
return !hippyRecyclerView.canScrollVertically(1);
RecyclerView.LayoutManager manager;
if (preloadItemNumber > 0 && (manager = hippyRecyclerView.getLayoutManager()) instanceof LinearLayoutManager) {
return ((LinearLayoutManager) manager).findLastVisibleItemPosition() >= manager.getItemCount() - preloadItemNumber;
}
return !hippyRecyclerView.canScrollVertically(1);
}

/**
* 水平滑动,内容已经到达最右边
*/
private boolean isHorizontalReachEnd() {
return !hippyRecyclerView.canScrollHorizontally(1);
RecyclerView.LayoutManager manager;
if (preloadItemNumber > 0 && (manager = hippyRecyclerView.getLayoutManager()) instanceof LinearLayoutManager) {
return ((LinearLayoutManager) manager).findLastVisibleItemPosition() >= manager.getItemCount() - preloadItemNumber;
}
return !hippyRecyclerView.canScrollHorizontally(1);
}

protected void sendOnReachedEvent() {
Expand Down Expand Up @@ -408,4 +417,13 @@ public void onOverPullStateChanged(int oldState, int newState, int offset) {
private boolean isOverPulling(int newState) {
return newState == HippyOverPullHelper.OVER_PULL_DOWN_ING || newState == HippyOverPullHelper.OVER_PULL_UP_ING;
}

/**
* @param preloadItemNumber 提前多少条Item,通知前端加载下一页数据
*/
public void setPreloadItemNumber(int preloadItemNumber) {
this.preloadItemNumber = preloadItemNumber;
checkSendReachEndEvent();
}

}

0 comments on commit c978c85

Please sign in to comment.