Skip to content

Commit

Permalink
fix(Android): 修复List组件在item高度不同时,滚动高度计算错误的问题;scroll事件中,当state为end时,不重…
Browse files Browse the repository at this point in the history
…置OffsetX/Y和dx/dy
  • Loading branch information
bbssyyuui committed Mar 21, 2023
1 parent 58c8f76 commit 73acbf0
Showing 1 changed file with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ public class List extends HMBase<SmartRefreshLayout> {
private ScrollEvent scrollEvent = new ScrollEvent();

private RecyclerView.OnScrollListener mOnScrollListener = new RecyclerView.OnScrollListener() {
int offsetX = 0;
int offsetY = 0;

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if (!mEventManager.contains(ScrollEvent.HM_EVENT_TYPE_SCROLL)) {
Expand All @@ -100,8 +103,13 @@ public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
return;
}

int offsetX = recyclerView.computeHorizontalScrollOffset();
int offsetY = recyclerView.computeVerticalScrollOffset();
// 当RecyclerView的每个item高度都相同时,computeXXX方法能正确获取到滚动的距离,
// 但是当每个item高度不一致时,获取到的值是不正确的,是基于item的平均高度,因此先改成dy累加方式。
// 但是当RecyclerView的item有变多或变少时,可能产生误差。
// int offsetX = recyclerView.computeHorizontalScrollOffset();
// int offsetY = recyclerView.computeVerticalScrollOffset();
offsetX += dx;
offsetY += dy;

scrollEvent.setType(ScrollEvent.HM_EVENT_TYPE_SCROLL);
scrollEvent.setState(ScrollEvent.HM_SCROLL_STATE_SCROLL);
Expand All @@ -127,10 +135,6 @@ public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
isScrollStarted = false;
scrollEvent.setType(ScrollEvent.HM_EVENT_TYPE_SCROLL);
scrollEvent.setState(ScrollEvent.HM_SCROLL_STATE_ENDED);
scrollEvent.setOffsetX(0);
scrollEvent.setOffsetY(0);
scrollEvent.setDx(0);
scrollEvent.setDy(0);
scrollEvent.setTimestamp(System.currentTimeMillis());
mEventManager.dispatchEvent(ScrollEvent.HM_EVENT_TYPE_SCROLL, scrollEvent);
refreshNodeTree();
Expand Down

0 comments on commit 73acbf0

Please sign in to comment.