使用FlatList向下滚动标题并进行动画处理-React Native

问题描述 投票:0回答:1

我没有发现要用FlatList制作动画的灵魂,我想像在Facebook应用中一样向下滚动时隐藏标题。我尝试过将FlatList与diffClamp()结合使用时没有成功,我不知道我是否可以用FlatList做到这一点,但我还需要LazyLoading,有人可以帮助我吗?

这是我的标题:

import React, { useState } from "react";
import {
  View,
  Animated,
  Text,
  Dimensions,
  TouchableOpacity
} from "react-native";
import { SafeAreaView } from "react-native-safe-area-context";
import { Ionicons } from "@expo/vector-icons";

const Header = props => {
  const params = props.scene.route.params;
  const [headerHeight] = useState(
    params !== undefined && params.changingHeight !== undefined
      ? params.changingHeight
      : Dimensions.get("window").height * 0.065
  );
  return (
    <SafeAreaView style={{ backgroundColor: "rgb(152,53,349)" }}>
      <Animated.View
        style={{
          width: Dimensions.get("window").width,
          height: headerHeight,
          flexDirection: "row",
          backgroundColor: "rgb(152,53,349)"
        }}
      >
        <TouchableOpacity
          onPress={() => {
            props.navigation.openDrawer();
          }}
        >
          <View
            style={{
              paddingVertical: "15%",
              justifyContent: "center",
              paddingHorizontal: 25
            }}
          >
            <Ionicons name="ios-menu" size={30} color={"white"} />
          </View>
        </TouchableOpacity>
        <View style={{ justifyContent: "center", marginLeft: "23%" }}>
          <Text
            style={{
              fontSize: 20,
              fontWeight: "bold",
              textAlign: "center",
              color: "white"
            }}
          >
            MyGameenter code here{" "}
          </Text>
        </View>
      </Animated.View>
    </SafeAreaView>
  );
};

export default Header;

这是我的FlatLIst:

import React from "react";
import { View, FlatList, StyleSheet } from "react-native";

import { EVENTS } from "../data/dummy-data";
import Event from "./Event";

const renderGridItem = itemData => {
  return <Event item={itemData.item} />;
};

const ShowEvents = props => {
  return (
    <View style={styles.list}>
      <FlatList
        keyExtractor={(item, index) => item.id}
        data={EVENTS}
        renderItem={renderGridItem}
        numColumns={1}
      />
    </View>
  );
};

const styles = StyleSheet.create({
  list: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center"
  }
});

export default ShowEvents;
reactjs react-native react-native-flatlist react-native-navigation
1个回答
0
投票

使用

onScroll={(e) => console.log(e.nativeEvent.contentOffset.y)}

工作示例:https://snack.expo.io/@msbot01/privileged-candies

import React, { Component } from 'react';
import { Text, View, StyleSheet, ScrollView, FlatList } from 'react-native';
import Constants from 'expo-constants';

export default class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      DATA: [],
      previous: 0,
      hide: false,
    };
  }

  componentDidMount() {
    var array = [];
    for (var i = 0; i < 100; i++) {
      var a = { id: i, value: i };
      array.push(a);
    }
    this.setData(array);
  }

  setData(a) {
    this.setState({
      DATA: a,
    });
  }

  Item({ title }) {
    return (
      <View
        style={{
          width: '100%',
          height: 30,
          marginTop: 5,
          backgroundColor: 'green',
          justifyContent: 'center',
          alignItems: 'center',
        }}>
        <Text />
      </View>
    );
  }

  _onScroll(event) {
    // console.log('>>>>>>>>>>>'+this.state.data);
    if (this.state.previous < event) {
      this.setState({
        hide: true,
        previous: event,
      });
    } else {
      this.setState({
        hide: false,
        previous: event,
      });
    }

    console.log(event);
  }

  render() {
    return (
      <View style={{ flex: 1 }}>
        {this.state.hide == true ? null : (
          <View
            style={{
              width: '100%',
              height: 50,
              justifyContent: 'center',
              alignItems: 'center',
            }}>
            <Text>Hide me while scrolling</Text>
          </View>
        )}
        <FlatList
          onScroll={e => this._onScroll(e.nativeEvent.contentOffset.y)}
          data={this.state.DATA}
          renderItem={({ item }) => (
            <View
              style={{
                width: '100%',
                height: 30,
                marginTop: 5,
                backgroundColor: 'green',
                justifyContent: 'center',
                alignItems: 'center',
              }}>
              <Text />
            </View>
          )}
          keyExtractor={item => item.id}
        />
      </View>
    );
  }
}

const styles = StyleSheet.create({});
© www.soinside.com 2019 - 2024. All rights reserved.