Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var ViewPager = require('react-native-viewpager');
* **`onChangePage`**: page change callback,
* **`renderPageIndicator`**: render custom ViewPager indicator.
* **`initialPage`**: show initially some other page than first page.
* **`selectedPage`**: allow parent component to change page of pager.

## Page Transition Animation Controls

Expand All @@ -49,6 +50,8 @@ var ViewPager = require('react-native-viewpager');
<ViewPager
dataSource={this.state.dataSource}
renderPage={this._renderPage}
selectedPage={this.state.currentPage}
onChangePage={(page)=>this.setState({currentPage:page})} //sync parent state with component state;
animation = {(animatedValue, toValue, gestureState) => {
// Use the horizontal velocity of the swipe gesture
// to affect the length of the transition so the faster you swipe
Expand Down
18 changes: 12 additions & 6 deletions ViewPager.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var ViewPager = React.createClass({
PropTypes.func,
PropTypes.bool
]),
selectedPage:PropTypes.number,
isLoop: PropTypes.bool,
locked: PropTypes.bool,
autoPlay: PropTypes.bool,
Expand Down Expand Up @@ -141,20 +142,25 @@ var ViewPager = React.createClass({
this._autoPlayer = null;
}
}

var controledPageChanged = Boolean(nextProps.selectedPage && nextProps.selectedPage !== this.state.currentPage);
if (nextProps.dataSource) {
var maxPage = nextProps.dataSource.getPageCount() - 1;
var constrainedPage = Math.max(0, Math.min(this.state.currentPage, maxPage));
this.setState({
currentPage: constrainedPage,
});

var constrainedPage = Math.max(0, Math.min(controledPageChanged ? nextProps.selectedPage : this.state.currentPage, maxPage));

if (!nextProps.isLoop) {
this.state.scrollValue.setValue(constrainedPage > 0 ? 1 : 0);
}

this.childIndex = Math.min(this.childIndex, constrainedPage);
this.fling = false;

if (controledPageChanged) {
this.goToPage( nextProps.selectedPage );
} else {
this.setState({
currentPage: constrainedPage,
});
}
}

},
Expand Down