react-native-swiper icon indicating copy to clipboard operation
react-native-swiper copied to clipboard

Infinite scroll possibile?

Open Johan-dutoit opened this issue 9 years ago • 8 comments

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?

Johan-dutoit avatar Jan 17 '17 12:01 Johan-dutoit

+1

RichardLindhout avatar Feb 13 '17 09:02 RichardLindhout


_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}>

RichardLindhout avatar Feb 13 '17 11:02 RichardLindhout

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 avatar Feb 16 '17 10:02 vnil

@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).

Johan-dutoit avatar Mar 03 '17 16:03 Johan-dutoit

We need a better solution for this. My solution is a dirty work around which is far from a stable solution.

RichardLindhout avatar Mar 04 '17 02:03 RichardLindhout

On Android I fixed an infinife vieuwpager some time ago. http://stackoverflow.com/questions/20584129/viewpager-in-android-to-switch-between-days-endlessly

RichardLindhout avatar Mar 04 '17 02:03 RichardLindhout

@Johan-dutoit can you post the example please of you checking when to load the data? :)

tomislavHonig avatar Mar 16 '17 09:03 tomislavHonig

@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 });
  }

Tritty avatar Sep 07 '18 15:09 Tritty