javascript - 如何从项目列表中选择 ScrollView 中的中心项目

标签 javascript reactjs listview react-native scrollview

我的应用程序中有来自两个 ScrollView 的 2 个 ScrollView ,我应该选择每个出现在滚动中心的项目,然后继续我在代码中的下一个事件

如果有任何 Prop 可以选择列表中的中心项目,我该怎么做

这是我的 js 文件,其中显示了 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;

这是我应用 ScrollView 的 js 文件:

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;

团队滚动

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;

最佳答案

我已经整理好了,我满足了所有这些要求我为此使用了一个 React Native 模块

react-native-snap-carousel

我可以控制屏幕上出现的元素数量,它也可以通过动画滚动,我还可以使用我添加的每个图像的索引进行选择

查看此内容以获取有关 snap-carousel 的详细说明: https://github.com/archriss/react-native-snap-carousel

我只通过使用它更改了我的 teamscroll 组件,我还可以将一些 Prop 传递给我应用程序中的其他文件

这是我使用 snap-carousel 的 TeamScroll.js 文件

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;

关于javascript - 如何从项目列表中选择 ScrollView 中的中心项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45568086/

相关文章:

javascript - 运行扩展类中的构造函数

javascript - 如何链接/组合 Observables

javascript - 如何在React Router中生成动态路由?

javascript - 相当于react中的directory、mozdirectory、webkitdirectory?

android - 如何避免在 getView() 中设置内联 onClickListener

javascript - 如何使用 jQuery 使用 data-filters 属性检索 "Result One"?

javascript - 获取具有多个正文的文档中的第一个正文标记

reactjs - 使用 'babel' 加载程序时,Node.js 的 Firebase 库会中断。具体是: "Uncaught TypeError: Cannot read property ' navigator' of undefined"

android - 如何将 AsyncTask 与 ListView 适配器一起使用

安卓。在 fragment : NullPointerException 中使用 SimpleCursorAdapter、SQLite 和 ListView