Skip to content

Commit 90bd046

Browse files
committed
feat: support contentInset on Android
1 parent 25d501d commit 90bd046

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollView.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,52 @@ public void setContentOffset(ReadableMap value) {
11091109
}
11101110
}
11111111

1112+
public void setContentInset(ReadableMap insets) {
1113+
int left = 0;
1114+
int top = 0;
1115+
int right = 0;
1116+
int bottom = 0;
1117+
if (insets != null) {
1118+
if (insets.hasKey("left")) {
1119+
left = (int) PixelUtil.toPixelFromDIP(insets.getDouble("left"));
1120+
}
1121+
if (insets.hasKey("top")) {
1122+
top = (int) PixelUtil.toPixelFromDIP(insets.getDouble("top"));
1123+
}
1124+
if (insets.hasKey("right")) {
1125+
right = (int) PixelUtil.toPixelFromDIP(insets.getDouble("right"));
1126+
}
1127+
if (insets.hasKey("bottom")) {
1128+
bottom = (int) PixelUtil.toPixelFromDIP(insets.getDouble("bottom"));
1129+
}
1130+
}
1131+
1132+
setPadding(left, top, right, bottom);
1133+
1134+
// If clipping of subviews is enabled, update the clipping rect
1135+
if (mRemoveClippedSubviews) {
1136+
updateClippingRect();
1137+
}
1138+
1139+
// Force a layout pass to ensure the new padding is applied.
1140+
requestLayout();
1141+
invalidate();
1142+
1143+
// Post a runnable to adjust the scroll position if needed.
1144+
// (Using post() ensures this happens after the layout pass.)
1145+
post(new Runnable() {
1146+
@Override
1147+
public void run() {
1148+
int maxScrollY = getMaxScrollY();
1149+
// If the current scroll offset is now beyond the new maximum,
1150+
// adjust it to the new maximum.
1151+
if (getScrollY() > maxScrollY) {
1152+
scrollTo(getScrollX(), maxScrollY);
1153+
}
1154+
}
1155+
});
1156+
}
1157+
11121158
/**
11131159
* Calls `smoothScrollTo` and updates state.
11141160
*

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewManager.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,12 @@ public void setContentOffset(ReactScrollView view, ReadableMap value) {
327327
view.setContentOffset(value);
328328
}
329329

330+
@ReactProp(name = "contentInset")
331+
public void setContentInset(ReactScrollView view, ReadableMap value) {
332+
view.setContentInset(value);
333+
}
334+
335+
330336
@ReactProp(name = "maintainVisibleContentPosition")
331337
public void setMaintainVisibleContentPosition(ReactScrollView view, ReadableMap value) {
332338
if (value != null) {

0 commit comments

Comments
 (0)