Infinite scroll possibile?
I was able to monitor the index change to determine whether or not I need to download the next 'page', however the control doesn't update with the new data.
Is this possible?
+1
_handleChange(e, state) {
const { lastIndex, index } = this.state
let newIndex = index
if (lastIndex === 2 && state.index === 0) {
newIndex += 1 // forward
} else if (lastIndex === 0 && state.index === 2) {
newIndex -= 1 // backward
} else if (lastIndex > state.index) {
newIndex -= 1 // backward
} else if (lastIndex < state.index) {
newIndex += 1 // forward
}
this.setState({ lastIndex: state.index, index: newIndex })
this.props.dispatch(load(newIndex))
}
<Swiper style={styles.wrapper} showsPagination={false} loadMinimal onMomentumScrollEnd={this._handleChange}>
This only seems to work if the swiper really stops between swipes. Would love to see a solution that supports fast swipes where slides does not need to stop. Any ideas?
@vnil the only important part of that snippet is the load/setState sections. I ultimately load my data when my index is about 2/3 away from the last one and in the then of the load I call setState with the current index (set state will trigger a render and add the new items).
We need a better solution for this. My solution is a dirty work around which is far from a stable solution.
On Android I fixed an infinife vieuwpager some time ago. http://stackoverflow.com/questions/20584129/viewpager-in-android-to-switch-between-days-endlessly
@Johan-dutoit can you post the example please of you checking when to load the data? :)
@RichardLindhout
Thank you very much!!! Your example code help me a lot~!!
But I made some changes cause your solution is not completely working for me, here's my code:
// component
<Swiper onMomentumScrollEnd={this.onScrollEnd}>
{
_.map(yourDataList, (data, index) => {
return xxxxxx;
})
}
</Swiper>
// event handler
private onScrollEnd = (e, state) => {
const { lastIndex, currentIndex, dataList } = this.state;
let newIndex = currentIndex;
// because state.index is relative index,so I use offset and device width to get the absolute index
// offset is always an integer multiple of device width
const index = state.offset.x / SCREEN_WIDTH;
if (lastIndex < index) { // forward
newIndex += 1;
dataList.push(xxxx);
this.setState({
dataList
});
} else if (lastIndex > index) { // backward
newIndex -= 1;
}
this.setState({ lastIndex: index, currentIndex: newIndex });
}