Skip to content

Commit 65c2dc4

Browse files
committed
fix initial page in PageCarousel
1 parent d6cd91a commit 65c2dc4

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

src/incubator/TabController/PageCarousel.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,25 @@ class PageCarousel extends Component {
1313
useNativeDriver: true
1414
});
1515

16+
componentDidMount() {
17+
if (Constants.isAndroid) {
18+
setTimeout(() => {
19+
this.scrollToPage(this.context.selectedIndex, false);
20+
}, 0);
21+
}
22+
}
23+
1624
onTabChange = ([index]) => {
17-
const node = this.carousel.current.getNode();
18-
node.scrollTo({x: index * Constants.screenWidth, animated: true});
25+
this.scrollToPage(index, true);
1926
};
2027

28+
scrollToPage = (pageIndex, animated) => {
29+
const node = this.carousel.current.getNode();
30+
node.scrollTo({x: pageIndex * Constants.screenWidth, animated});
31+
}
32+
2133
render() {
22-
const {currentPage} = this.context;
34+
const {selectedIndex, currentPage} = this.context;
2335
return (
2436
<>
2537
<Animated.ScrollView
@@ -30,11 +42,14 @@ class PageCarousel extends Component {
3042
showsHorizontalScrollIndicator={false}
3143
onScroll={this.onScroll}
3244
scrollEventThrottle={200}
45+
contentOffset={{x: selectedIndex * Constants.screenWidth}} // iOS only
3346
/>
3447

3548
<Code>
3649
{() => {
37-
return block([call([currentPage], this.onTabChange)]);
50+
return block([
51+
Animated.onChange(currentPage, call([currentPage], this.onTabChange))
52+
]);
3853
}}
3954
</Code>
4055
</>

src/incubator/TabController/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class TabController extends Component {
5757

5858
_targetPage = new Value(-1);
5959
_currentPage = new Value(this.props.selectedIndex);
60-
_carouselOffset = new Value(0);
60+
_carouselOffset = new Value(this.props.selectedIndex * Math.round(Constants.screenWidth));
6161

6262
getProviderContextValue = () => {
6363
const {itemStates} = this.state;

0 commit comments

Comments
 (0)