react-native-pager-view icon indicating copy to clipboard operation
react-native-pager-view copied to clipboard

infinite scroll

Open hengkx opened this issue 3 years ago • 11 comments

As dev, I would like to have ability to have infinite scroll behavior.

hengkx avatar Jul 14 '20 04:07 hengkx

Hey, For now, we don't support this kind of functionality. PR are welcome.

troZee avatar Jul 16 '20 11:07 troZee

#30

hengkx avatar Jul 16 '20 15:07 hengkx

How do we about implementing it customly. This is much required!

dhirajanand014 avatar Oct 20 '20 16:10 dhirajanand014

I insert the first page to the end of pages aray: const pages = [<Page1 />, <Page2 />, <Page3 />, <Page1 />];. Then I track selected page using onPageSelected callback, and when selectedPage >= pages.length - 1 I call pagerViewRef.current?.setPageWithoutAnimation(0);.

That creates an illusion of infinite scroll, it doesn't work if you want to go swipe back from the first page to see the last, but it must be achievable in the same way, setting the initialPage={1}.

A proper implementation would be nice though.

binchik avatar Apr 19 '21 10:04 binchik

To continue of @binchik steps:

const slides = [slide1, slide2, slide3]
  const firstSlide = slides[0]
  const lastSlide = slides[slides.length - 1]
  const loopingSlides = [lastSlide, ...slides, firstSlide]
  const viewPagerRef = useRef()

  return (
    <>
      <ViewPager
        initialPage={1}
        ref={viewPagerRef}
        onPageSelected={event => {
          const currentPage = event.nativeEvent.position
          const reachedFakeLastSlide = currentPage === 0
          const reachedFakeFirstSlide = currentPage === loopingSlides.length - 1
          
          if (reachedFakeFirstSlide) {
            viewPagerRef.current.setPageWithoutAnimation(1)
          } else if (reachedFakeLastSlide) {
            viewPagerRef.current.setPageWithoutAnimation(loopingSlides.length - 2)
          } else {
            setPage(currentPage)
          }
        }}
        style={styles.container}
      >
        {loopingSlides}
      </ViewPager>
      <Indicators length={3} active={page - 1} />
    </>
  )

ahmedam55 avatar Apr 21 '21 09:04 ahmedam55

To continue of @binchik steps:

const slides = [slide1, slide2, slide3]
  const firstSlide = slides[0]
  const lastSlide = slides[slides.length - 1]
  const loopingSlides = [lastSlide, ...slides, firstSlide]
  const viewPagerRef = useRef()

  return (
    <>
      <ViewPager
        initialPage={1}
        ref={viewPagerRef}
        onPageSelected={event => {
          const currentPage = event.nativeEvent.position
          const reachedFakeLastSlide = currentPage === 0
          const reachedFakeFirstSlide = currentPage === loopingSlides.length - 1
          
          if (reachedFakeFirstSlide) {
            viewPagerRef.current.setPageWithoutAnimation(1)
          } else if (reachedFakeLastSlide) {
            viewPagerRef.current.setPageWithoutAnimation(loopingSlides.length - 2)
          } else {
            setPage(currentPage)
          }
        }}
        style={styles.container}
      >
        {loopingSlides}
      </ViewPager>
      <Indicators length={3} active={page - 1} />
    </>
  )

There is no example for loop implementation with this component. I tried to apply this code to my project, but I couldn't run. Any idea?

  render() {

  const slides = this.state.slideData;
  const viewPagerRef = useRef<Container2 | null>(null);
    const firstSlide = slides[0]
  const lastSlide = slides[slides.length - 1]
  const loopingSlides = [lastSlide, ...slides, firstSlide]
const Container2 = styled(PagerView)`
	height: ${height}px;
	width: 100%;
`
<Container2
			orientation='horizontal'
			initialPage={1}
			onPageSelected={(e) => {
				
 const currentPage = e.nativeEvent.position
          const reachedFakeLastSlide = currentPage === 0
          const reachedFakeFirstSlide = currentPage === loopingSlides.length - 1
          
          if (reachedFakeFirstSlide) {
            viewPagerRef.current.setPageWithoutAnimation(1)
          } else if (reachedFakeLastSlide) {
            viewPagerRef.current.setPageWithoutAnimation(loopingSlides.length - 2)
          }

          

    this.setState({ selectedd: e.nativeEvent.position })
  //  console.log(this.state.selectedd);
   // alert('aaas') 
  }
  }
  		ref={viewPagerRef}>					
{item.subdata.map((subitem, indexx) => {
				return (
<View key={indexx} style={{ justifyContent: 'center', width: '100%', }}>					

oarsoy avatar May 20 '21 22:05 oarsoy

@binchik it's not good, it will cause blink

CPPAlien avatar Jul 20 '21 06:07 CPPAlien

I did a investigation and it should be implemented using below tutorials/code:

Android: https://medium.com/mobile-app-development-publication/android-bi-direction-infinite-viewpager-2-scrolling-1a729e4ee773

iOS: https://github.com/igroomgrim/Infinite-scroll-with-uipageviewcontroller

troZee avatar Aug 10 '21 10:08 troZee

Those who are looking for infinite loop function. Check this https://github.com/computerjazz/react-native-infinite-pager.

SymntxHomendra51 avatar Sep 22 '22 15:09 SymntxHomendra51