react-native-image-gallery icon indicating copy to clipboard operation
react-native-image-gallery copied to clipboard

How to jump to certain Image/Index?

Open N3rdArtiste opened this issue 6 years ago • 6 comments

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 avatar Mar 17 '19 13:03 N3rdArtiste

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()})}}} />
    );
  }

N3rdArtiste avatar Mar 19 '19 05:03 N3rdArtiste

use three backtick ` to convert code style 💃

ptdede avatar Mar 25 '19 06:03 ptdede

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 👍

ptdede avatar Mar 25 '19 06:03 ptdede

Thank you for that little snippet.

sourbrew avatar Nov 06 '19 13:11 sourbrew

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.

christopher-18 avatar Mar 16 '20 13:03 christopher-18

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

samipshah100 avatar Apr 07 '20 03:04 samipshah100