Skip to content

Commit b50d3fe

Browse files
fix(android): initialPage for dynamic content
1 parent 791ce7a commit b50d3fe

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

android/src/main/java/com/reactnativepagerview/NestedScrollableHost.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class NestedScrollableHost : FrameLayout {
2323
constructor(context: Context) : super(context)
2424
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
2525
public var initialIndex: Int? = null
26+
public var didSetInitialIndex = false
2627
private var touchSlop = 0
2728
private var initialX = 0f
2829
private var initialY = 0f

android/src/main/java/com/reactnativepagerview/PagerViewViewManager.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ class PagerViewViewManager : ViewGroupManager<NestedScrollableHost>() {
9696
// https://github.com/facebook/react-native/issues/17968#issuecomment-697136929
9797
refreshViewChildrenLayout(parent)
9898
}
99+
100+
if (!host.didSetInitialIndex && host.initialIndex == index) {
101+
host.didSetInitialIndex = true
102+
setCurrentItem(parent, index, false)
103+
}
99104
}
100105

101106
override fun getChildCount(parent: NestedScrollableHost) = getViewPager(parent).adapter?.itemCount ?: 0
@@ -148,9 +153,9 @@ class PagerViewViewManager : ViewGroupManager<NestedScrollableHost>() {
148153
//https://github.com/callstack/react-native-pager-view/issues/456
149154
//Initial index should be set only once.
150155
if (host.initialIndex === null) {
156+
host.initialIndex = value
151157
view.post {
152-
setCurrentItem(view, value, false)
153-
host.initialIndex = value
158+
host.didSetInitialIndex = true
154159
}
155160
}
156161
}

0 commit comments

Comments
 (0)