gpt4 book ai didi

reactjs - 如何使用 Animated 使列表 (FlatList) 自动滚动元素?

转载 作者:行者123 更新时间:2023-12-04 14:41:43 29 4
gpt4 key购买 nike

我有一个水平的 FlatList,每次到达末尾时,它都会自动向列表中添加新元素,因此它有点像一个无限列表。我希望应用程序自己自动滚动列表,而用户仍然必须能够来回滚动。这就是我要走的路

export default class ImageCarousel extends Component {
constructor(props) {
super(props);

this.scrollX = 0;
this.offset = new Animated.Value(0);
this.scrollTo = this.scrollTo.bind(this);
this.handleScroll = this.handleScroll.bind(this);
this.stopAnimation = this.stopAnimation.bind(this);
// Listener to call the scrollToOffset function
this.offset.addListener(this.scrollTo);
}

_scroller() {
toValue = this.scrollX + 10; // Scroll 10 pixels in each loop
this.animation = Animated.timing(
this.offset,
{
toValue: toValue,
duration: 1000, // A loop takes a second
easing: Easing.linear,
}
);
this.animation.start(() => this._scroller()); //Repeats itself when done
}

scrollTo(e) {
this.carousel.scrollToOffset({offset: e.value});
}

handleScroll(event) {
// Save the x (horizontal) value each time a scroll occurs
this.scrollX = event.nativeEvent.contentOffset.x;
}

componentDidMount() {
this._scroller();
}
render() {
return (
<View>
<FlatList
ref={el => this.carousel = el}
data={someData}
renderItem={renderFunction}
horizontal={true}
keyExtractor={someKeyFunction}
onEndReached={loadMoreElementsFunction}
onScroll={this.handleScroll}
/>
</View>
);
}
}

它的工作原理是它会自动滚动列表,但问题是我无法手动滚动列表,因为滚动位置会由 scrollTo 监听器不断更新。我试图添加一个 onPress 回调以在按下 FlatList 时禁用动画,但是我无法让它工作。

最佳答案

  • 这是我的数据。

  • Blockquote


    state = { 
    link: [
    'https://image.shutterstock.com/image-vector/online-exam-computer-web-app-260nw-1105800884.jpg',
    'https://image.shutterstock.com/image-vector/online-exam-computer-web-app-260nw-1105800884.jpg',
    'https://image.shutterstock.com/image-vector/online-exam-computer-web-app-260nw-1105800884.jpg',
    'https://image.shutterstock.com/image-vector/online-exam-computer-web-app-260nw-1105800884.jpg',
    ],};

    1. Define FlatList Ref

    flatList = createRef();

    1. FlatList component

       <FlatList
    style={{flex: 1}}
    data={this.state.link}
    keyExtractor={this._keyExtractor.bind(this)}
    renderItem={this._renderItem.bind(this)}
    horizontal={true}
    flatListRef={React.createRef()}
    ref={this.flatList}
    />

    1. Next slide

      _goToNextPage = () => {
    if (CurrentSlide >= this.state.link.length-1) CurrentSlide = 0;
    this.flatList.current.scrollToIndex({
    index: ++CurrentSlide,
    animated: true,
    });
    };

    1. Start and stop Interval

      _startAutoPlay = () => {
    this._timerId = setInterval(this._goToNextPage, IntervalTime);
    };



    _stopAutoPlay = () => {
    if (this._timerId) {
    clearInterval(this._timerId);
    this._timerId = null;
    }
    };

    1. Associated function

    componentDidMount() {
    this._stopAutoPlay();
    this._startAutoPlay();
    }

    componentWillUnmount() {
    this._stopAutoPlay();
    }

    _renderItem({item, index}) {
    return <Image source={{uri: item}} style={styles.sliderItems} />;
    }

    _keyExtractor(item, index) {
    return index.toString();
    }

    Full Code:


    import React, {Component, createRef} from 'react';
    import {
    Text,
    View,
    ScrollView,
    Image,
    StyleSheet,
    Dimensions,
    FlatList,
    } from 'react-native';

    let CurrentSlide = 0;
    let IntervalTime = 4000;

    export default class Slider extends Component {
    flatList = createRef();

    // TODO _goToNextPage()
    _goToNextPage = () => {
    if (CurrentSlide >= this.state.link.length-1) CurrentSlide = 0;

    this.flatList.current.scrollToIndex({
    index: ++CurrentSlide,
    animated: true,
    });
    };

    _startAutoPlay = () => {
    this._timerId = setInterval(this._goToNextPage, IntervalTime);
    };

    _stopAutoPlay = () => {
    if (this._timerId) {
    clearInterval(this._timerId);
    this._timerId = null;
    }
    };


    componentDidMount() {
    this._stopAutoPlay();
    this._startAutoPlay();
    }

    componentWillUnmount() {
    this._stopAutoPlay();
    }

    // TODO _renderItem()
    _renderItem({item, index}) {
    return <Image source={{uri: item}} style={styles.sliderItems} />;
    }

    // TODO _keyExtractor()
    _keyExtractor(item, index) {
    // console.log(item);
    return index.toString();
    }
    state = {
    link: [
    'https://image.shutterstock.com/image-vector/online-exam-computer-web-app-260nw-1105800884.jpg',
    'https://image.shutterstock.com/image-vector/online-exam-computer-web-app-260nw-1105800884.jpg',
    // 'https://picsum.photos/200/300',
    'https://image.shutterstock.com/image-vector/online-exam-computer-web-app-260nw-1105800884.jpg',
    'https://image.shutterstock.com/image-vector/online-exam-computer-web-app-260nw-1105800884.jpg',
    ],
    };

    render() {
    return (
    <View style={{marginTop: 10, marginBottom: 10}}>
    <FlatList
    style={{
    flex: 1,
    // TODO Remove extera global padding
    // marginLeft: -size.padding,
    // marginRight: -size.padding,
    }}
    data={this.state.link}
    keyExtractor={this._keyExtractor.bind(this)}
    renderItem={this._renderItem.bind(this)}
    horizontal={true}
    flatListRef={React.createRef()}
    ref={this.flatList}
    />
    </View>
    );
    }
    }

    const styles = StyleSheet.create({
    sliderItems: {
    marginLeft: 5,
    marginRight: 5,
    height: 200,
    width: Dimensions.get('window').width,
    },
    });

    关于reactjs - 如何使用 Animated 使列表 (FlatList) 自动滚动元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44475202/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com