Pinch to zoom feature in flatlist reactnative











up vote
0
down vote

favorite












I'm using flickr image search api to render images in grid format using flatlist. Using maximumZoomScale and minimumZoomScale of scrollView(iOS only) I am able to implement zoomin to change grid structure from 3 column to two columns and vice versa, However, on changing this layout,(due to re render) the flatlist again starts from the top even if I have scrolled the image to the bottom. Is there any way to keep the flatlist showing the same item even on dynamically changing the number of columns?



 export default class BrowserHome extends React.Component {
constructor(props) {
super(props);
this.state = {
isLoading: false,
tagParam: "cat",
pageNum: -1,
data: ,
photosObj: "",
totalColumn: 3
};
this.handleScroll = this.handleScroll.bind(this);
}

state = { onEndReachedCalledDuringMomentum: true };

//Lifecycle methods
componentDidMount() {
this.setState({
isLoading: true
});
try {
this.makeRequest();
} catch {
console.log("error has occurred");
}
}

makeRequest = () => {
const { tagParam, pageNum } = this.state;
let url = `https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=${apiKey}&format=json&tags=${tagParam}&per_page=30&page=${pageNum +
1}&nojsoncallback=1`;
fetch(url, {
method: "GET"
})
.then(response => response.json())
.then(responseJSON => {
this.setState({
//data: responseJSON.photos.photo,
data: this.state.data.concat(responseJSON.photos.photo),
isLoading: false,
pageNum: responseJSON.photos.page
});
})
.catch(error => {
console.log(error);
this.setState({ isLoading: false });
throw error;
});
};

render() {
return (
<View
style={{
flex: 1,
height: 200,
justifyContent: "flex-start",
width: screenSize.width,
backgroundColor: "black"
}}
>
<Text>This is browserhome</Text>
<FlatList
minimumZoomScale={0.8}
maximumZoomScale={2}
key={this.state.totalColumn}
onScroll={this.handleScroll}
style={{
width: screenSize.width
}}
numColumns={this.state.totalColumn}
data={this.state.data}
keyExtractor={item => item.id}
bounces={false}
onEndReachedThreshold={0.5}
onMomentumScrollBegin={() => {
this.onEndReachedCalledDuringMomentum = false;
}}
onEndReached={({ distanceFromEnd }) => {
if (!this.onEndReachedCalledDuringMomentum) {
this.loadMoreItem();
this.onEndReachedCalledDuringMomentum = true;
}
}}
renderItem={({ item, index }) => (
<>
<ImageTile
imageURL={this.createImageURL(item)}
column={this.state.totalColumn}
/>
{/* <Text style={{ color: "white" }}>
{index}
{console.log(index)}
</Text> */}
</>
)}
/>
</View>
);
}

loadMoreItem() {
this.makeRequest();
}

handleScroll(event) {
console.log(event.nativeEvent.zoomScale);
if (event.nativeEvent.zoomScale > 1) {
this.setState({
totalColumn: 2
});
} else if (event.nativeEvent.zoomScale < 0.95) {
this.setState({
totalColumn: 3
});
}
}
}









share|improve this question




























    up vote
    0
    down vote

    favorite












    I'm using flickr image search api to render images in grid format using flatlist. Using maximumZoomScale and minimumZoomScale of scrollView(iOS only) I am able to implement zoomin to change grid structure from 3 column to two columns and vice versa, However, on changing this layout,(due to re render) the flatlist again starts from the top even if I have scrolled the image to the bottom. Is there any way to keep the flatlist showing the same item even on dynamically changing the number of columns?



     export default class BrowserHome extends React.Component {
    constructor(props) {
    super(props);
    this.state = {
    isLoading: false,
    tagParam: "cat",
    pageNum: -1,
    data: ,
    photosObj: "",
    totalColumn: 3
    };
    this.handleScroll = this.handleScroll.bind(this);
    }

    state = { onEndReachedCalledDuringMomentum: true };

    //Lifecycle methods
    componentDidMount() {
    this.setState({
    isLoading: true
    });
    try {
    this.makeRequest();
    } catch {
    console.log("error has occurred");
    }
    }

    makeRequest = () => {
    const { tagParam, pageNum } = this.state;
    let url = `https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=${apiKey}&format=json&tags=${tagParam}&per_page=30&page=${pageNum +
    1}&nojsoncallback=1`;
    fetch(url, {
    method: "GET"
    })
    .then(response => response.json())
    .then(responseJSON => {
    this.setState({
    //data: responseJSON.photos.photo,
    data: this.state.data.concat(responseJSON.photos.photo),
    isLoading: false,
    pageNum: responseJSON.photos.page
    });
    })
    .catch(error => {
    console.log(error);
    this.setState({ isLoading: false });
    throw error;
    });
    };

    render() {
    return (
    <View
    style={{
    flex: 1,
    height: 200,
    justifyContent: "flex-start",
    width: screenSize.width,
    backgroundColor: "black"
    }}
    >
    <Text>This is browserhome</Text>
    <FlatList
    minimumZoomScale={0.8}
    maximumZoomScale={2}
    key={this.state.totalColumn}
    onScroll={this.handleScroll}
    style={{
    width: screenSize.width
    }}
    numColumns={this.state.totalColumn}
    data={this.state.data}
    keyExtractor={item => item.id}
    bounces={false}
    onEndReachedThreshold={0.5}
    onMomentumScrollBegin={() => {
    this.onEndReachedCalledDuringMomentum = false;
    }}
    onEndReached={({ distanceFromEnd }) => {
    if (!this.onEndReachedCalledDuringMomentum) {
    this.loadMoreItem();
    this.onEndReachedCalledDuringMomentum = true;
    }
    }}
    renderItem={({ item, index }) => (
    <>
    <ImageTile
    imageURL={this.createImageURL(item)}
    column={this.state.totalColumn}
    />
    {/* <Text style={{ color: "white" }}>
    {index}
    {console.log(index)}
    </Text> */}
    </>
    )}
    />
    </View>
    );
    }

    loadMoreItem() {
    this.makeRequest();
    }

    handleScroll(event) {
    console.log(event.nativeEvent.zoomScale);
    if (event.nativeEvent.zoomScale > 1) {
    this.setState({
    totalColumn: 2
    });
    } else if (event.nativeEvent.zoomScale < 0.95) {
    this.setState({
    totalColumn: 3
    });
    }
    }
    }









    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm using flickr image search api to render images in grid format using flatlist. Using maximumZoomScale and minimumZoomScale of scrollView(iOS only) I am able to implement zoomin to change grid structure from 3 column to two columns and vice versa, However, on changing this layout,(due to re render) the flatlist again starts from the top even if I have scrolled the image to the bottom. Is there any way to keep the flatlist showing the same item even on dynamically changing the number of columns?



       export default class BrowserHome extends React.Component {
      constructor(props) {
      super(props);
      this.state = {
      isLoading: false,
      tagParam: "cat",
      pageNum: -1,
      data: ,
      photosObj: "",
      totalColumn: 3
      };
      this.handleScroll = this.handleScroll.bind(this);
      }

      state = { onEndReachedCalledDuringMomentum: true };

      //Lifecycle methods
      componentDidMount() {
      this.setState({
      isLoading: true
      });
      try {
      this.makeRequest();
      } catch {
      console.log("error has occurred");
      }
      }

      makeRequest = () => {
      const { tagParam, pageNum } = this.state;
      let url = `https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=${apiKey}&format=json&tags=${tagParam}&per_page=30&page=${pageNum +
      1}&nojsoncallback=1`;
      fetch(url, {
      method: "GET"
      })
      .then(response => response.json())
      .then(responseJSON => {
      this.setState({
      //data: responseJSON.photos.photo,
      data: this.state.data.concat(responseJSON.photos.photo),
      isLoading: false,
      pageNum: responseJSON.photos.page
      });
      })
      .catch(error => {
      console.log(error);
      this.setState({ isLoading: false });
      throw error;
      });
      };

      render() {
      return (
      <View
      style={{
      flex: 1,
      height: 200,
      justifyContent: "flex-start",
      width: screenSize.width,
      backgroundColor: "black"
      }}
      >
      <Text>This is browserhome</Text>
      <FlatList
      minimumZoomScale={0.8}
      maximumZoomScale={2}
      key={this.state.totalColumn}
      onScroll={this.handleScroll}
      style={{
      width: screenSize.width
      }}
      numColumns={this.state.totalColumn}
      data={this.state.data}
      keyExtractor={item => item.id}
      bounces={false}
      onEndReachedThreshold={0.5}
      onMomentumScrollBegin={() => {
      this.onEndReachedCalledDuringMomentum = false;
      }}
      onEndReached={({ distanceFromEnd }) => {
      if (!this.onEndReachedCalledDuringMomentum) {
      this.loadMoreItem();
      this.onEndReachedCalledDuringMomentum = true;
      }
      }}
      renderItem={({ item, index }) => (
      <>
      <ImageTile
      imageURL={this.createImageURL(item)}
      column={this.state.totalColumn}
      />
      {/* <Text style={{ color: "white" }}>
      {index}
      {console.log(index)}
      </Text> */}
      </>
      )}
      />
      </View>
      );
      }

      loadMoreItem() {
      this.makeRequest();
      }

      handleScroll(event) {
      console.log(event.nativeEvent.zoomScale);
      if (event.nativeEvent.zoomScale > 1) {
      this.setState({
      totalColumn: 2
      });
      } else if (event.nativeEvent.zoomScale < 0.95) {
      this.setState({
      totalColumn: 3
      });
      }
      }
      }









      share|improve this question















      I'm using flickr image search api to render images in grid format using flatlist. Using maximumZoomScale and minimumZoomScale of scrollView(iOS only) I am able to implement zoomin to change grid structure from 3 column to two columns and vice versa, However, on changing this layout,(due to re render) the flatlist again starts from the top even if I have scrolled the image to the bottom. Is there any way to keep the flatlist showing the same item even on dynamically changing the number of columns?



       export default class BrowserHome extends React.Component {
      constructor(props) {
      super(props);
      this.state = {
      isLoading: false,
      tagParam: "cat",
      pageNum: -1,
      data: ,
      photosObj: "",
      totalColumn: 3
      };
      this.handleScroll = this.handleScroll.bind(this);
      }

      state = { onEndReachedCalledDuringMomentum: true };

      //Lifecycle methods
      componentDidMount() {
      this.setState({
      isLoading: true
      });
      try {
      this.makeRequest();
      } catch {
      console.log("error has occurred");
      }
      }

      makeRequest = () => {
      const { tagParam, pageNum } = this.state;
      let url = `https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=${apiKey}&format=json&tags=${tagParam}&per_page=30&page=${pageNum +
      1}&nojsoncallback=1`;
      fetch(url, {
      method: "GET"
      })
      .then(response => response.json())
      .then(responseJSON => {
      this.setState({
      //data: responseJSON.photos.photo,
      data: this.state.data.concat(responseJSON.photos.photo),
      isLoading: false,
      pageNum: responseJSON.photos.page
      });
      })
      .catch(error => {
      console.log(error);
      this.setState({ isLoading: false });
      throw error;
      });
      };

      render() {
      return (
      <View
      style={{
      flex: 1,
      height: 200,
      justifyContent: "flex-start",
      width: screenSize.width,
      backgroundColor: "black"
      }}
      >
      <Text>This is browserhome</Text>
      <FlatList
      minimumZoomScale={0.8}
      maximumZoomScale={2}
      key={this.state.totalColumn}
      onScroll={this.handleScroll}
      style={{
      width: screenSize.width
      }}
      numColumns={this.state.totalColumn}
      data={this.state.data}
      keyExtractor={item => item.id}
      bounces={false}
      onEndReachedThreshold={0.5}
      onMomentumScrollBegin={() => {
      this.onEndReachedCalledDuringMomentum = false;
      }}
      onEndReached={({ distanceFromEnd }) => {
      if (!this.onEndReachedCalledDuringMomentum) {
      this.loadMoreItem();
      this.onEndReachedCalledDuringMomentum = true;
      }
      }}
      renderItem={({ item, index }) => (
      <>
      <ImageTile
      imageURL={this.createImageURL(item)}
      column={this.state.totalColumn}
      />
      {/* <Text style={{ color: "white" }}>
      {index}
      {console.log(index)}
      </Text> */}
      </>
      )}
      />
      </View>
      );
      }

      loadMoreItem() {
      this.makeRequest();
      }

      handleScroll(event) {
      console.log(event.nativeEvent.zoomScale);
      if (event.nativeEvent.zoomScale > 1) {
      this.setState({
      totalColumn: 2
      });
      } else if (event.nativeEvent.zoomScale < 0.95) {
      this.setState({
      totalColumn: 3
      });
      }
      }
      }






      react-native react-native-ios react-native-flatlist






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 at 5:29

























      asked Nov 21 at 12:24









      Romit Kumar

      11414




      11414





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53411964%2fpinch-to-zoom-feature-in-flatlist-reactnative%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53411964%2fpinch-to-zoom-feature-in-flatlist-reactnative%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Sphinx de Gizeh

          Dijon

          Get global maximum slope