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

How to jump to certain Image/Index? #125

Open
N3rdArtiste opened this issue Mar 17, 2019 · 6 comments
Open

How to jump to certain Image/Index? #125

N3rdArtiste opened this issue Mar 17, 2019 · 6 comments

Comments

@N3rdArtiste
Copy link

I want to have a button which takes user to specified index on being pressed.
Please someone throw light on how this can be done.
Thanks

@N3rdArtiste
Copy link
Author

N3rdArtiste commented Mar 19, 2019

It's not perfect solution but it works. what simply it does is that it Rerenders the gallery component with new initialPage value.

constructor(props){
super(props)
this.state = {
  num:0,
  key:0
}
  }

  renderImageSlider(index,images){
    return (<Gallery
      style={{ flex: 1 }}
      initialPage={index}
      images={images}
    />)
  }

  render() {
const images ={[
          { source: require('yourApp/image.png'), dimensions: { width: 150, height: 150 } },
          { source: { uri: 'http://i.imgur.com/XP2BE7q.jpg' } },
          { source: { uri: 'http://i.imgur.com/5nltiUd.jpg' } },
          { source: { uri: 'http://i.imgur.com/6vOahbP.jpg' } },
          { source: { uri: 'http://i.imgur.com/kj5VXtG.jpg' } }
        ]}
    return (
     <View style={{width:'100%',height:250}} key={this.state.key}>
        {this.renderImageSlider(this.state.num,images)}
        </View>
        <View>
        <Button onPress={()=>{{this.setState({num:images.length-1,key:Math.random()})}}} />
    );
  }

@ptdede
Copy link

ptdede commented Mar 25, 2019

use three backtick ` to convert code style 💃

@ptdede
Copy link

ptdede commented Mar 25, 2019

Oh btw, you can use

// somewhere in your code (except render method without callback)
// .scrollToPage(index, false) -> (image index, animate or not)
this._gallery && this._gallery.getViewPagerInstance().scrollToPage(index, false)

<Gallery
      ref={(c) => { this._gallery = c }}
      images={this.state.images}
      onPageSelected={this.handleOnGallerySwiped}
/>

@nerdartist
Instead of rerender the whole component 👍

@sourbrew
Copy link

sourbrew commented Nov 6, 2019

Thank you for that little snippet.

@christopher-18
Copy link

The script added by @ptdede works for me. But is there any method by which I can detect whether I have reached end of the array and is trying to swipe again so that i can implement the loop feature in this library.

@samipshah100
Copy link

@bhavna-18 you can get size of array of your images. you can compare that against onPageSelected prop

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

No branches or pull requests

5 participants