Skip to content

Commit

Permalink
fix(android): waterfall scroll offset after dispatch layout
Browse files Browse the repository at this point in the history
  • Loading branch information
siguangli2018 committed Oct 16, 2024
1 parent 151a060 commit 839c5d0
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,6 @@ export default class ListExample extends React.Component {
getItemKey={this.getItemKey}
getItemStyle={this.getItemStyle}
getHeaderStyle={this.getHeaderStyle}
contentInset={contentInset}
/>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,10 @@ public void getItemOffsets(@NonNull Rect outRect, @NonNull View view,
lp.leftMargin = 0;
return;
}
ViewHolder vh = lp.mViewHolder;
int margin = mItemSpacing;
HippyRecyclerListAdapter adapter = (HippyRecyclerListAdapter) parent.getAdapter();
if (lp.isFullSpan()) {
margin = 0;
} else if (vh != null) {
int headerCount = adapter.hasHeader() ? 1 : 0;
if (!adapter.hasBannerView() && vh.getLayoutPosition() < (mNumberOfColumns + headerCount)) {
margin = 0;
}
}
if (HippyListUtils.isVerticalLayout(parent)) {
lp.bottomMargin = mItemSpacing;
lp.topMargin = margin;
} else {
lp.rightMargin = mItemSpacing;
lp.leftMargin = margin;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
import com.tencent.mtt.hippy.views.hippylist.HippyRecyclerListAdapter;
import com.tencent.mtt.hippy.views.hippylist.PullRefreshContainer;
import com.tencent.renderer.node.ListItemRenderNode;
import com.tencent.renderer.node.PullFooterRenderNode;
import com.tencent.renderer.node.PullHeaderRenderNode;
import com.tencent.renderer.node.RenderNode;
import com.tencent.renderer.node.WaterfallItemRenderNode;

Expand Down Expand Up @@ -63,22 +61,22 @@ public void layoutDecoratedWithMargins(@NonNull View child, int left, int top, i
(StaggeredGridLayoutManager.LayoutParams) child.getLayoutParams();
final int spanIndex = lp.getSpanIndex();
final boolean isFullSpan = lp.isFullSpan();
int lf = left;
int rt = right;
if (isFullSpan) {
child.layout(left, top, right, bottom);
} else {
int lf = mRecyclerView.getPaddingLeft() + spanIndex * (lp.width
lf = mRecyclerView.getPaddingLeft() + spanIndex * (lp.width
+ mItemDecoration.getColumnSpacing());
int rt = mRecyclerView.getPaddingLeft() + (spanIndex + 1) * lp.width
rt = mRecyclerView.getPaddingLeft() + (spanIndex + 1) * lp.width
+ spanIndex * mItemDecoration.getColumnSpacing();
child.layout(lf, top, rt, bottom);
}
super.layoutDecoratedWithMargins(child, lf, top, rt, bottom);
int size;
if (child instanceof PullRefreshContainer) {
size = getOrientation() == RecyclerView.VERTICAL ? bottom - top : right - left;
} else {
size = getOrientation() == RecyclerView.VERTICAL ? lp.height
+ mItemDecoration.getItemSpacing()
: lp.width + mItemDecoration.getItemSpacing();
size = getOrientation() == RecyclerView.VERTICAL ? lp.height : lp.width;
}
RenderNode childNode = RenderManager.getRenderNode(child);
if (childNode instanceof WaterfallItemRenderNode) {
Expand Down Expand Up @@ -162,11 +160,7 @@ private int computeScrollOffset() {
private int getChildSize(@NonNull ListItemRenderNode child) {
Integer size = itemSizeMaps.get(child.getId());
if (size == null) {
if (child.isPullFooter() || child.isPullHeader()) {
size = getItemSizeFromAdapter(child);
} else {
size = getItemSizeFromAdapter(child) + mItemDecoration.getItemSpacing();
}
size = getItemSizeFromAdapter(child);
}
return Math.max(size, 0);
}
Expand Down Expand Up @@ -280,6 +274,7 @@ int getItemSizeFromAdapter(ListItemRenderNode node) {
ItemLayoutParams layoutInfo = (ItemLayoutParams) adapter;
resetLayoutParams();
layoutInfo.getItemLayoutParams(node, ITEM_LAYOUT_PARAMS);
LogUtils.d(TAG, "getItemSizeFromAdapter id " + node.getId() + ", height " + ITEM_LAYOUT_PARAMS.height);
if (getOrientation() == RecyclerView.VERTICAL) {
if (ITEM_LAYOUT_PARAMS.height >= 0) {
int size = ITEM_LAYOUT_PARAMS.height + ITEM_LAYOUT_PARAMS.bottomMargin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import com.tencent.mtt.hippy.views.list.IRecycleItemTypeChange;
import com.tencent.mtt.hippy.views.refresh.HippyPullFooterView;
import com.tencent.mtt.hippy.views.refresh.HippyPullHeaderView;
import com.tencent.mtt.hippy.views.waterfall.HippyWaterfallItemView;
import com.tencent.mtt.hippy.views.waterfall.HippyWaterfallView;
import com.tencent.renderer.node.ListItemRenderNode;
import com.tencent.renderer.node.PullFooterRenderNode;
import com.tencent.renderer.node.PullHeaderRenderNode;
Expand Down Expand Up @@ -223,7 +225,11 @@ protected void setLayoutParams(View itemView, int position) {
childLp.height = isVertical ? childNode.getHeight() : MATCH_PARENT;
childLp.width = isVertical ? MATCH_PARENT : childNode.getWidth();
} else {
childLp.height = childNode.getHeight();
if ((itemView instanceof HippyWaterfallItemView) && (hippyRecyclerView instanceof HippyWaterfallView)) {
childLp.height = childNode.getHeight() + ((HippyWaterfallView) hippyRecyclerView).getItemSpacing();
} else {
childLp.height = childNode.getHeight();
}
childLp.width = childNode.getWidth();
}
itemView.setLayoutParams(childLp);
Expand Down Expand Up @@ -342,6 +348,9 @@ private int getRenderNodeHeight(@NonNull ListItemRenderNode childNode) {
}
return 0;
}
if ((childNode instanceof WaterfallItemRenderNode) && (hippyRecyclerView instanceof HippyWaterfallView)) {
return childNode.getHeight() + ((HippyWaterfallView) hippyRecyclerView).getItemSpacing();
}
return childNode.getHeight();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public boolean onFling(int velocityX, int velocityY) {
return isOverPulling();
}
});
setOverScrollMode(OVER_SCROLL_NEVER);
}

public void setColumnSpacing(int columnSpacing) {
Expand All @@ -46,6 +47,14 @@ public void setColumnSpacing(int columnSpacing) {
}
}

public int getItemSpacing() {
RecyclerView.ItemDecoration decoration = getItemDecorationAt(0);
if (decoration instanceof HippyGridSpacesItemDecoration) {
return ((HippyGridSpacesItemDecoration) decoration).getItemSpacing();
}
return 0;
}

public void setItemSpacing(int itemSpacing) {
RecyclerView.ItemDecoration decoration = getItemDecorationAt(0);
if (decoration instanceof HippyGridSpacesItemDecoration) {
Expand Down

0 comments on commit 839c5d0

Please sign in to comment.