react-native-viewpager icon indicating copy to clipboard operation
react-native-viewpager copied to clipboard

viewpager.goToPage doesn't animate on Android

Open dslounge opened this issue 10 years ago • 1 comments

Using react-native: ^0.29.2

I'm trying to implement a next button that will advance the viewpager to the next page, but calling viewpager.goToPage(n) doesn't cause the viewpager to change pages.

I stepped through the viewpager code but I wasn't able to quickly figure out if there was a bug. I did confirm that it gets to call the animation here: https://github.com/race604/react-native-viewpager/blob/master/ViewPager.js#L208. The animation "finishes", but it doesn't end up animating anything on screen.

Sample code (commented and deleted some redux stuff):

import React from 'react';
import { View, Text, StyleSheet, TouchableHighlight } from 'react-native';
import { connect } from 'react-redux';
import ViewPager from 'react-native-viewpager';

const imageSrc = require('../../images/toolbar-logo.png');
import Page from './page';

const pages = [
  {
    image: imageSrc,
    headline: '',
    text: 'blaaa.',
    backgroundColor: '#33AC6F'
  },
  {
    image: imageSrc,
    headline: 'headline',
    text: 'blaaa.',
    backgroundColor: '#FFA624'
  },  
];

class WelcomeScene extends React.Component {

  constructor(props) {
    super(props);
    const dataSource = new ViewPager.DataSource({
      pageHasChanged: (p1, p2) => { return p1 !== p2; }
    });

    this.renderPage = this.renderPage.bind(this);
    this.onNextPage = this.onNextPage.bind(this);
    this.viewPagerRef = this.viewPagerRef.bind(this);

    this.state = {
      dataSource: dataSource.cloneWithPages(pages),
      page: 0
    };
  }

  onNextPage() {
    //const currentPage = this.props.page;
    //if (currentPage < pages.length) {
      // const nextPage = currentPage + 1;
      // this.props.setTutorialPage(nextPage);
      this.viewpager.goToPage(1);
    // }
  }

  viewPagerRef(viewpager) {
    this.viewpager = viewpager;
  }

  renderPage(item) {
    return (
      <Page
        image={item.image}
        headline={item.headline}
        text={item.text}
        backgroundColor={item.backgroundColor}
      />
    );
  }

  render() {
    return (
      <View style={styles.component}>
        <ViewPager
          style={styles.viewPager}
          dataSource={this.state.dataSource}
          renderPage={this.renderPage}
          ref={this.viewPagerRef}
        />
        <View style={styles.controls}>
          <View style={styles.controlRow}>
            <View style={styles.controlPanel}>
              <View style={[styles.button]}>
                <Text style={{ color: 'white' }}>Skip</Text>
              </View>
            </View>
            <View style={styles.controlPanel}>
              <TouchableHighlight onPress={this.onNextPage}>
                <View style={[styles.button, styles.nextButton]}>
                  <Text style={{ color: 'white' }}>Next</Text>
                </View>
              </TouchableHighlight>
            </View>
          </View>
        </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  component: {
    flex: 1,
    backgroundColor: 'white'
  },
  viewPager: {
    flex: 1
  },
  controls: {
    position: 'absolute',
    height: 75,
    left: 0,
    bottom: 0,
    right: 0,
  },
  controlRow: {
    flexDirection: 'row'
  },
  controlPanel: {
    flex: 1,
    alignItems: 'center'
  },
  button: {
    borderRadius: 3,
    width: 100,
    height: 30,
    justifyContent: 'center',
    alignItems: 'center'
  },
  nextButton: {
    backgroundColor: 'rgba(0, 0, 255, .5)',
    borderRadius: 3,
    width: 100,
    height: 30
  }
});

WelcomeScene.propTypes = {
  // page: React.PropTypes.number,
  // seen: React.PropTypes.bool,
  // dispatch
  // setTutorialPage: React.PropTypes.func,
  // setTutorialSeen: React.PropTypes.func,
};

export default WelcomeScene;

page.js:

import React from 'react';
import { View, Image, Text, StyleSheet } from 'react-native';

class Page extends React.Component { /* eslint react/prefer-stateless-function: 0 */
  render() {
    const headlinePart = !!this.props.headline ?
      <Text style={styles.headline}>{this.props.headline}</Text>
      : null;

    return (
      <View style={[styles.component, { backgroundColor: this.props.backgroundColor }]}>
        <View style={styles.content}>
          <View style={styles.imageContainer}>
            <Image
              style={styles.image}
              source={this.props.image}
            />
          </View>
          {headlinePart}
          <Text style={styles.text}>{this.props.text}</Text>
        </View>
      </View>
    );
  }
}

Page.propTypes = {
  image: React.PropTypes.number,
  headline: React.PropTypes.string,
  text: React.PropTypes.string,
  backgroundColor: React.PropTypes.string
};

const styles = StyleSheet.create({
  component: {
    flex: 1,
  },
  content: {
    margin: 20,
    justifyContent: 'flex-end'
  },
  imageContainer: {
    alignItems: 'center',
    height: 300,
    justifyContent: 'flex-end',
    marginBottom: 15
  },
  headline: {
    color: 'white',
    fontSize: 22,
    fontWeight: 'bold',
    marginBottom: 15
  },
  text: {
    color: 'white',
    fontSize: 20,
  },
  image: {
  }
});

export default Page;

dslounge avatar Aug 13 '16 22:08 dslounge

Somehow the event.finished is still false. I add a small timeout like 50 miliseconds and it works

luqmansungkar avatar Nov 05 '17 00:11 luqmansungkar