Skip to content

Commit

Permalink
fix(android): relayout waterfall when scroll idle and back top
Browse files Browse the repository at this point in the history
  • Loading branch information
siguangli2018 committed Nov 4, 2024
1 parent 248b541 commit efe22eb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import android.content.Context;
import android.graphics.Rect;
import android.util.Log;
import android.view.ViewConfiguration;
import android.widget.FrameLayout;
import androidx.annotation.NonNull;
Expand Down Expand Up @@ -49,6 +50,7 @@
public class HippyRecyclerView<ADP extends HippyRecyclerListAdapter> extends HippyRecyclerViewBase
implements IHeaderAttachListener, HippyViewHolderAbandonListener, HippyNestedScrollTarget2 {

private static final String TAG = "HippyRecyclerView";
private static int DEFAULT_ITEM_VIEW_CACHE_SIZE = 4;
private static final int INVALID_POINTER = -1;
protected ADP listAdapter;
Expand Down Expand Up @@ -253,18 +255,27 @@ private void scrollToInitContentOffset() {
* 刷新数据
*/
public void setListData() {
LogUtils.d("HippyRecyclerView", "itemCount =" + listAdapter.getItemCount());
LayoutManager layoutManager = getLayoutManager();
int currentNodeCount = listAdapter.getRenderNodeCount();
if (layoutManager instanceof StaggeredGridLayoutManager) {
listAdapter.notifyItemRangeChanged(renderNodeCount, listAdapter.getRenderNodeCount() - renderNodeCount);
LogUtils.d(TAG, "setListData: lastNodeCount " + renderNodeCount + ", currentNodeCount " + currentNodeCount);
int[] firstVisibleItem = null;
firstVisibleItem = ((HippyStaggeredGridLayoutManager) layoutManager).findFirstVisibleItemPositions(firstVisibleItem);
if (renderNodeCount >= currentNodeCount && firstVisibleItem != null
&& (firstVisibleItem[0] == 0 || firstVisibleItem[0] == 1)) {
LogUtils.d(TAG, "setListData: firstVisibleItem[0] " + firstVisibleItem[0]);
listAdapter.notifyDataSetChanged();
} else if (renderNodeCount < currentNodeCount) {
listAdapter.notifyItemRangeInserted(renderNodeCount, listAdapter.getRenderNodeCount() - renderNodeCount);
}
} else {
listAdapter.notifyDataSetChanged();
}
if (overPullHelper != null) {
overPullHelper.enableOverPullUp(!listAdapter.hasFooter());
overPullHelper.enableOverPullDown(!listAdapter.hasHeader());
}
renderNodeCount = listAdapter.getRenderNodeCount();
renderNodeCount = currentNodeCount;
if (renderNodeCount > 0 && mInitialContentOffset > 0) {
scrollToInitContentOffset();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import androidx.recyclerview.widget.HippyStaggeredGridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.Adapter;
import androidx.recyclerview.widget.RecyclerView.LayoutManager;
import androidx.recyclerview.widget.RecyclerView.OnScrollListener;

Expand Down Expand Up @@ -56,6 +57,7 @@
public class RecyclerViewEventHelper extends OnScrollListener implements OnLayoutChangeListener,
OnAttachStateChangeListener, HippyOverPullListener {

private static final int WATERFALL_SCROLL_RELAYOUT_THRESHOLD = 4;
protected final HippyRecyclerView hippyRecyclerView;
private boolean scrollBeginDragEventEnable;
private boolean scrollEndDragEventEnable;
Expand Down Expand Up @@ -179,6 +181,21 @@ protected HippyViewEvent getOnScrollDragEndedEvent() {
return onScrollDragEndedEvent;
}

private void relayoutWaterfallIfNeeded() {
LayoutManager layoutManager = hippyRecyclerView.getLayoutManager();
if (layoutManager instanceof HippyStaggeredGridLayoutManager) {
int[] firstVisibleItem = null;
firstVisibleItem = ((HippyStaggeredGridLayoutManager) layoutManager).findFirstVisibleItemPositions(firstVisibleItem);
if (firstVisibleItem != null && (firstVisibleItem[0] <= WATERFALL_SCROLL_RELAYOUT_THRESHOLD)) {
Adapter adapter = hippyRecyclerView.getAdapter();
if (adapter != null) {
adapter.notifyDataSetChanged();
hippyRecyclerView.dispatchLayout();
}
}
}
}

@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
int oldState = currentState;
Expand All @@ -190,6 +207,9 @@ public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
sendDragEndEvent(oldState, currentState);
sendFlingEvent(newState);
sendFlingEndEvent(oldState, currentState);
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
relayoutWaterfallIfNeeded();
}
}

@Override
Expand Down

0 comments on commit efe22eb

Please sign in to comment.