Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Infinite swiping with lazy loading #7

Closed
lalatgithub opened this issue May 7, 2017 · 7 comments
Closed

[Feature Request] Infinite swiping with lazy loading #7

lalatgithub opened this issue May 7, 2017 · 7 comments
Labels

Comments

@lalatgithub
Copy link
Contributor

lalatgithub commented May 7, 2017

I am trying to call Swiper with a list which has only single item but it displays it 2 times. not sure what i am doing wrong or havn't understand it well.

const cards = [ { id: 4, videoFile: '/data/assets/vid.mp4' , name: 'Sovereign Light Cafe'} ]

renderContent(video) {
    return (
      <View style={{ flex: 1 }} >
          <Video source={{ uri: video.videoFile }} />
       </View>
    );
  }

<Swiper
           cards={cards}
           renderCard={this.renderContent}
           onSwipedTop={this.swipedTop}
           onSwipedBottom={this.swipedBottom}
           infinite
           style={{ width, height }}
/>

when i first load it, there are 2 videos playing on the top of each other.
screenshot_2017-05-07-23-56-29

@alexbrillant
Copy link
Owner

What react native version are you using? I cannot reproduce with 0.43.3
I tried having only one item in the cards props, and the second view wasn't being rendered as expected.

@alexbrillant
Copy link
Owner

alexbrillant commented May 7, 2017

I found the solution : set the infinite props to false. Also, make sure that the value of the cards props is not an empty array.

The infinite prop makes swiping infinite when set the true.
It is the default value so you can just remove it like this :

<Swiper
     cards={cards}
     renderCard={this.renderContent}
     onSwipedTop={this.swipedTop}
     onSwipedBottom={this.swipedBottom}
     style={{ width, height }} />

@lalzada Is this good now?

@lalatgithub
Copy link
Contributor Author

lalatgithub commented May 7, 2017

Thanks for quick support..

Yes.. removing infinite did the trick but the actual problem is still there..

I am replacing that single item in list when user swipe (onSwipedTop and onSwipedBottom). The main purpose is to load next video when user swipe on screen... Lazy loaded videos

if i have single item in list at the start and i remove the infinite then after swipe there is nothing left on screen.

OR

i am keeping that list empty and render my next video inside renderCard() function when user swipe but keeping infinite in this scenario is still creating the same problem.. duplicating same video.

this scenario is best fit for onSwipe() instead of onSwiped() so one could change the cards list just before swipe. so after one item is swiped there would be next item in list..

@lalatgithub
Copy link
Contributor Author

ohh i see that the next video is playing when i swipe on screen but its not showing on screen actually.. Don't know the the problem is exactly...

@alexbrillant alexbrillant changed the title renderContent call two times for a list of single item Infinite swiping with lazy loading May 7, 2017
@alexbrillant alexbrillant changed the title Infinite swiping with lazy loading [Feature Request] Infinite swiping with lazy loading May 7, 2017
@alexbrillant alexbrillant reopened this May 7, 2017
@alexbrillant
Copy link
Owner

alexbrillant commented May 7, 2017

Here is an exemple of inifinite swiping with lazy loading.

As you can see, you can keep the cards in the state of the component containing the Swiper, and add elements on the onSwiped events...
@lalzada let me know if you have any questions.

import React, { Component } from "react"
import Swiper from "react-native-deck-swiper"
import { StyleSheet, View, Text, Image, Button } from "react-native"

export default class Exemple extends Component {
  constructor(props) {
    super(props)
    this.state = {
      cards: [1, 2],
      swipedAllCards: false,
      isSwipingBack: false,
      cardIndex: 0
    }
  }

  renderCard = (card) => {
    return (
      <View style={styles.card}>
        <Text style={styles.text}>{card}</Text>
      </View>
    )
  }

  onSwiped = () => {
    const {cards} = this.state
    const newCardNumber = cards[cards.length - 1] + 1
    this.setState({
      cards: [...this.state.cards, newCardNumber]
    })
  }

  render() {
    return (
      <View style={styles.container}>
        <Swiper
          ref={swiper => {
            this.swiper = swiper
          }}
          onSwiped={this.onSwiped}
          cards={this.state.cards}
          cardIndex={this.state.cardIndex}
          cardVerticalMargin={80}
          renderCard={this.renderCard} />
      </View>
    )
  }
}

const styles = StyleSheet.create({
  box1: {
    flex: 1
  },
  container: {
    flex: 1,
    backgroundColor: "#F5FCFF"
  },
  card: {
    flex: 1,
    borderRadius: 4,
    borderWidth: 2,
    borderColor: "#E8E8E8",
    justifyContent: "center",
    backgroundColor: "white"
  },
  text: {
    textAlign: "center",
    fontSize: 50,
    backgroundColor: "transparent"
  },
  done: {
    textAlign: "center",
    fontSize: 30,
    color: "white",
    backgroundColor: "transparent"
  }
}

@safciplak
Copy link

newCardNumber

i tried but no way. still i didn't add new card. could you please to me?

@hbarylskyi
Copy link

Example above should work with #285. @safciplak you can try that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants