Adarsh Sreeram
Adarsh Sreeram

Reputation: 962

How to select the center item in a scroll view from a list of items

I have 2 scroll views in my app from both of the ScrollViews I should select 1 item each that appears at the center of the scroll and then proceed for my next activity in the code

So how can I do this is there any prop to select the center item at the list

This is my js file which shows 2 ScrollViews:

import React, { Component } from 'react';
import { Image, Text, View, TouchableWithoutFeedback, Animated } from 'react-native';
import { Actions } from 'react-native-router-flux';
import TeamScroll from './TeamScroll';

const a = require('./Images/over3_selected.png');
const b = require('./Images/over3.png');
const c = require('./Images/over5_selected.png');
const d = require('./Images/over5.png');
const e = require('./Images/over10_selected.png');
const f = require('./Images/over10.png');

 class ChallengeScreen extends Component {
   state = {
     threePressed: false,
     fivePressed: false,
     tenPressed: false
   }
   componentWillMount() {
        this.slide1 = new Animated.Value(0);
        this.slide2 = new Animated.Value(0);
        this.slide3 = new Animated.Value(0);

         this.ball1();
         this.ball2();
         this.ball3();
       }
       ball1() {
             Animated.timing(
               this.slide1, {
                delay: 100,
               toValue: -140,
               duration: 700,
             }
             ).start();
           }

       ball2() {
                 Animated.timing(
                   this.slide2, {
                   delay: 200,
                   toValue: -160,
                   duration: 900,
                 }
                 ).start();
               }
         ball3() {
                     Animated.timing(
                         this.slide3, {
                         delay: 300,
                         toValue: -180,
                         duration: 1100,
                       }
                       ).start();
                       }


  render() {
  return (
<Image
    source={require('./Images/bg_inner.png')} style={styles.backgroundStyle}>

    <Text style={styles.chooseteamtextStyle}>
    CHOOSE YOUR TEAM
    </Text>
    <Image source={require('./Images/team-logo-band.png')} style={styles.band1Style}>
    <TeamScroll />
    </Image>

    <Text style={styles.opponentStyle}>
    YOUR OPPONENT
    </Text>
    <Image source={require('./Images/team-logo-band.png')} style={styles.band2Style}>
    <TeamScroll />
    </Image>

    <Text style={styles.overstextStyle}>
    OVERS
    </Text>
    <View style={styles.viewStyle}>

     <TouchableWithoutFeedback
     onPress={() => playFunc(3, this.props.challenge)}
     onPressIn={() => {
       this.setState({ threePressed: true });
     }}
     onPressOut={() => {
       this.setState({ threePressed: false });
     }}
     >
    <Animated.Image source={this.state.threePressed ? a : b}
    style={[styles.over3Style, { transform: [{ translateY: this.slide1 }] }]} />
    </ TouchableWithoutFeedback>

    <TouchableWithoutFeedback
    onPress={() => playFunc(5, this.props.challenge)}
    onPressIn={() => {
      this.setState({ fivePressed: true });
    }}
    onPressOut={() => {
      this.setState({ fivePressed: false });
    }}>
    <Animated.Image source={this.state.fivePressed ? c : d}
    style={[styles.over5Style, { transform: [{ translateY: this.slide2 }] }]} />
</ TouchableWithoutFeedback>

    <TouchableWithoutFeedback
    onPress={() => playFunc(10, this.props.challenge)}
    onPressIn={() => {
      this.setState({ tenPressed: true });
    }}
    onPressOut={() => {
      this.setState({ tenPressed: false });
    }}>
    <Animated.Image source={this.state.tenPressed ? e : f}
    style={[styles.over10Style, { transform: [{ translateY: this.slide3 }] }]} />
    </ TouchableWithoutFeedback>

    </View>
</ Image>
);
}
}
function playFunc(num, param) {
if (num === 3 && param === 'Computer') {
  Actions.screen4();
}
else if (num === 5 && param === 'Computer') {
  Actions.screen4();
}
else if (num === 10 && param === 'Computer') {
  Actions.screen4();
}
else if (num === 3 && param === 'Team') {
  Actions.screen3();
}
else if (num === 5 && param === 'Team') {
  Actions.screen3();
}
else if (num === 10 && param === 'Team') {
  Actions.screen3();
}
}
const styles = {
  viewStyle: {

    flexDirection: 'row',
    justifyContent: 'flex-start'
  },
backgroundStyle: {
   flex: 1,
   width: undefined,
   height: undefined,
   flexWrap: 'wrap',

 },
 chooseteamtextStyle: {
 textAlign: 'center',
   marginTop: 80,
   fontSize: 20,
   color: 'white',
   fontFamily: 'Switzerland-Cond-Black-Italic',
   transform: [{ rotate: '-14deg' }]
 },
 band1Style: {
 marginTop: -10,
resizeMode: 'stretch',
width: 380,
height: 150,
 },
 opponentStyle: {
   textAlign: 'center',
   marginTop: -10,
   fontSize: 20,
   color: 'white',
   fontFamily: 'Switzerland-Cond-Black-Italic',
   transform: [{ rotate: '-15deg' }]
 },
 band2Style: {
resizeMode: 'stretch',
marginTop: -10,
width: 380,
height: 150,
 },
 overstextStyle: {
   textAlign: 'center',
   bottom: 10,
   fontSize: 20,
   color: 'white',
   fontFamily: 'Switzerland-Cond-Black-Italic',
   transform: [{ rotate: '-15deg' }]
 },
 over3Style: {
   flexDirection: 'row',
alignItems: 'flex-start',
width: 80,
height: 80,
top: 170,
left: 70,
 },
 over5Style: {
   flexDirection: 'row',
alignItems: 'center',
width: 80,
height: 80,
bottom: -170,
left: 70
 },
 over10Style: {
   flexDirection: 'row',
alignItems: 'flex-end',
width: 80,
height: 80,
 top: 170,
right: -70
 }
 };
export default ChallengeScreen;

and this is the js file where I have applied ScrollView:

import React, { Component } from 'react';
import { Image, Text, View, TouchableWithoutFeedback, Animated } from 'react-native';
import { Actions } from 'react-native-router-flux';
import TeamScroll from './TeamScroll';

const a = require('./Images/over3_selected.png');
const b = require('./Images/over3.png');
const c = require('./Images/over5_selected.png');
const d = require('./Images/over5.png');
const e = require('./Images/over10_selected.png');
const f = require('./Images/over10.png');

 class ChallengeScreen extends Component {
   state = {
     threePressed: false,
     fivePressed: false,
     tenPressed: false
   }
   componentWillMount() {
  this.slide1 = new Animated.Value(0);
  this.slide2 = new Animated.Value(0);
  this.slide3 = new Animated.Value(0);

  this.ball1();
  this.ball2();
  this.ball3();
  }
 ball1() {
 Animated.timing(
 this.slide1, {
 delay: 100,
 toValue: -140,
 duration: 700,
 }
 ).start();
 }

 ball2() {
Animated.timing(
 this.slide2, {
 delay: 200,
 toValue: -160,
duration: 900,
}
 ).start();
}
 ball3() {
 Animated.timing(
   this.slide3, {
 delay: 300,
 toValue: -180,
 duration: 1100,
 }
 ).start();
 }


  render() {
  return (
<Image
    source={require('./Images/bg_inner.png')} style={styles.backgroundStyle}>

    <Text style={styles.chooseteamtextStyle}>
    CHOOSE YOUR TEAM
    </Text>
    <Image source={require('./Images/team-logo-band.png')} style={styles.band1Style}>
    <TeamScroll />
    </Image>

    <Text style={styles.opponentStyle}>
    YOUR OPPONENT
    </Text>
    <Image source={require('./Images/team-logo-band.png')} style={styles.band2Style}>
    <TeamScroll />
    </Image>

    <Text style={styles.overstextStyle}>
    OVERS
    </Text>
    <View style={styles.viewStyle}>

     <TouchableWithoutFeedback
     onPress={() => playFunc(3, this.props.challenge)}
     onPressIn={() => {
       this.setState({ threePressed: true });
     }}
     onPressOut={() => {
       this.setState({ threePressed: false });
     }}
     >
    <Animated.Image source={this.state.threePressed ? a : b}
    style={[styles.over3Style, { transform: [{ translateY: this.slide1 }] }]} />
    </ TouchableWithoutFeedback>

    <TouchableWithoutFeedback
    onPress={() => playFunc(5, this.props.challenge)}
    onPressIn={() => {
      this.setState({ fivePressed: true });
    }}
    onPressOut={() => {
      this.setState({ fivePressed: false });
    }}>
    <Animated.Image source={this.state.fivePressed ? c : d}
    style={[styles.over5Style, { transform: [{ translateY: this.slide2 }] }]} />
</ TouchableWithoutFeedback>

    <TouchableWithoutFeedback
    onPress={() => playFunc(10, this.props.challenge)}
    onPressIn={() => {
      this.setState({ tenPressed: true });
    }}
    onPressOut={() => {
      this.setState({ tenPressed: false });
    }}>
    <Animated.Image source={this.state.tenPressed ? e : f}
    style={[styles.over10Style, { transform: [{ translateY: this.slide3 }] }]} />
    </ TouchableWithoutFeedback>

    </View>
</ Image>
);
}
}
function playFunc(num, param) {
if (num === 3 && param === 'Computer') {
  Actions.screen4();
}
else if (num === 5 && param === 'Computer') {
  Actions.screen4();
}
else if (num === 10 && param === 'Computer') {
  Actions.screen4();
}
else if (num === 3 && param === 'Team') {
  Actions.screen3();
}
else if (num === 5 && param === 'Team') {
  Actions.screen3();
}
else if (num === 10 && param === 'Team') {
  Actions.screen3();
}
}
const styles = {
  viewStyle: {

    flexDirection: 'row',
    justifyContent: 'flex-start'
  },
backgroundStyle: {
   flex: 1,
   width: undefined,
   height: undefined,
   flexWrap: 'wrap',

 },
 chooseteamtextStyle: {
 textAlign: 'center',
   marginTop: 80,
   fontSize: 20,
   color: 'white',
   fontFamily: 'Switzerland-Cond-Black-Italic',
   transform: [{ rotate: '-14deg' }]
 },
 band1Style: {
 marginTop: -10,
resizeMode: 'stretch',
width: 380,
height: 150,
 },
 opponentStyle: {
   textAlign: 'center',
   marginTop: -10,
   fontSize: 20,
   color: 'white',
   fontFamily: 'Switzerland-Cond-Black-Italic',
   transform: [{ rotate: '-15deg' }]
 },
 band2Style: {
resizeMode: 'stretch',
marginTop: -10,
width: 380,
height: 150,
 },
 overstextStyle: {
   textAlign: 'center',
   bottom: 10,
   fontSize: 20,
   color: 'white',
   fontFamily: 'Switzerland-Cond-Black-Italic',
   transform: [{ rotate: '-15deg' }]
 },
 over3Style: {
   flexDirection: 'row',
alignItems: 'flex-start',
width: 80,
height: 80,
top: 170,
left: 70,
 },
 over5Style: {
   flexDirection: 'row',
alignItems: 'center',
width: 80,
height: 80,
bottom: -170,
left: 70
 },
 over10Style: {
   flexDirection: 'row',
alignItems: 'flex-end',
width: 80,
height: 80,
 top: 170,
right: -70
 }
 };
export default ChallengeScreen;

TeamScroll.js

import React, { Component } from 'react';
import { ScrollView, View, Image } from 'react-native';

class TeamScroll extends Component {
  render() {
    return (
<ScrollView
horizontal
style={styles.scrollViewStyle}
    showsHorizontalScrollIndicator={false}>
  <View style={styles.viewStyle}>

    <View style={styles.cheStyle}>
      <Image
       source={require('./Images/logo-chepauk.png')}
       style={styles.logoStyle} />
    </View>

    <View style={styles.dinStyle}>
      <Image
       source={require('./Images/logo-dindigul.png')}
      style={styles.logoStyle}  />
    </View>

    <View style={styles.kanStyle}>
      <Image
       source={require('./Images/logo-kanchi.png')}
      style={styles.logoStyle} />
    </View>

    <View style={styles.karStyle}>
      <Image
       source={require('./Images/logo-karaikudi.png')}
      style={styles.logoStyle} />
    </View>

    <View style={styles.kovStyle}>
      <Image
       source={require('./Images/logo-kovai.png')}
      style={styles.logoStyle} />
    </View>

    <View style={styles.madStyle}>
      <Image
       source={require('./Images/logomadurai.png')}
      style={styles.logoStyle} />
    </View>

    <View style={styles.thiStyle}>
      <Image
       source={require('./Images/logothiruvallur.png')}
      style={styles.logoStyle} />
    </View>

    <View style={styles.tutStyle}>
      <Image
       source={require('./Images/logotuti.png')}
      style={styles.logoStyle} />
    </View>

  </View>
</ScrollView>

    );
  }
}
const styles = {
viewStyle: {
 flexDirection: 'row',
 justifyContent: 'space-around',
// position: 'relative',
 transform: [{ rotate: '-4deg' }]
},

logoStyle: {
  width: 90,
  height: 90,
  position: 'relative',
  marginLeft: 10,
  marginRight: 30,
  marginTop: 20
},
cheStyle: {
  bottom: 20,
transform: [{ rotate: '16deg' }]
},
dinStyle: {
  bottom: 15,
  transform: [{ rotate: '16deg' }]
},
kanStyle: {
  marginTop: -5,
  transform: [{ rotate: '16deg' }]
},
karStyle: {
  top: 10,
  transform: [{ rotate: '17deg' }]
},
kovStyle: {
  top: 10,
  transform: [{ rotate: '16deg' }]
},
madStyle: {
  marginTop: 25,
  transform: [{ rotate: '16deg' }]
},
thiStyle: {
  marginTop: 35,
  transform: [{ rotate: '16deg' }]
},
tutStyle: {
  marginTop: 45,
  transform: [{ rotate: '16deg' }]
},
scrollViewStyle: {
  transform: [{ rotate: '-12deg' }]
}
};
export default TeamScroll;

Upvotes: 1

Views: 4789

Answers (1)

Adarsh Sreeram
Adarsh Sreeram

Reputation: 962

I have sorted it out and I met all those requirements I used a react native module for this

react-native-snap-carousel

I could control the number of elements appearing on the screen and it is also scrollable with animations and I could also select using the index of each image that I have added

check this for a detailed description about snap-carousel: https://github.com/archriss/react-native-snap-carousel

I have only changed my teamscroll component by using this and I could also pass some props to other files in my app

here's my TeamScroll.js file using snap-carousel

import React, { Component } from 'react';
import { View, Image, Dimensions } from 'react-native';
import Carousel from 'react-native-snap-carousel';

const { height, width } = Dimensions.get('window');

class TeamScroll extends Component {
  render() {
    return (
      <View style={{ top: 50, }}>
      <View style={{ transform: [{ rotate: '-12deg' }] }}>
      <Carousel
      inactiveSlideOpacity={0.7}
      inactiveSlideScale={0.65}
      firstItem={1}
      sliderWidth={width}
      itemWidth={width / 3} >
      <Image
      source={require('./Images/logo-chepauk.png')}
      style={styles.logoStyle}  />
      <Image
      source={require('./Images/logo-dindigul.png')}
      style={styles.logoStyle}  />
      <Image
       source={require('./Images/logo-kanchi.png')}
      style={styles.logoStyle}  />
      <Image
      source={require('./Images/logo-karaikudi.png')}
      style={styles.logoStyle}  />
      <Image
      source={require('./Images/logo-kovai.png')}
      style={styles.logoStyle}  />
      <Image
      source={require('./Images/logomadurai.png')}
      style={styles.logoStyle}  />
      <Image
      source={require('./Images/logothiruvallur.png')}
      style={styles.logoStyle}  />
      <Image
      source={require('./Images/logotuti.png')}
      style={styles.logoStyle}  />
      </Carousel>
      </View>
      </View>
);
}
}
const styles = {
  logoStyle: {
    width: width / 3,
    height: width / 3
    }
};
export default TeamScroll;

Upvotes: 4

Related Questions