Reactjs 使用FlatList和动画-React Native使标题向下滚动

Reactjs 使用FlatList和动画-React Native使标题向下滚动,reactjs,react-native,react-native-flatlist,react-native-navigation,Reactjs,React Native,React Native Flatlist,React Native Navigation,我找不到任何与FlatList一起制作动画的soulotion, 当我像在Facebook应用程序中一样向下滚动时,我想隐藏我的标题。 我尝试将FlatList与diffClamp()一起使用,但没有成功, 我不知道我是否能用FlatList完成,但我也需要懒洋洋的, 有人能帮我吗 这是我的标题: import React, { useState } from "react"; import { View, Animated, Text, Dimensions, Toucha

我找不到任何与FlatList一起制作动画的soulotion, 当我像在Facebook应用程序中一样向下滚动时,我想隐藏我的标题。 我尝试将FlatList与diffClamp()一起使用,但没有成功, 我不知道我是否能用FlatList完成,但我也需要懒洋洋的, 有人能帮我吗

这是我的标题:

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;
import React,{useState}来自“React”;
进口{
看法
有生气的
文本,
尺寸,
可触摸不透明度
}从“反应本族语”;
从“反应本机安全区域上下文”导入{SafeAreaView};
从“@expo/vector icons”导入{Ionicons}”;
const Header=props=>{
const params=props.scene.route.params;
常量[人头高度]=使用状态(
params!==未定义&¶ms.changingHeight!==未定义
?参数昌金海
:尺寸。获取(“窗口”)。高度*0.065
);
返回(
{
props.navigation.openDrawer();
}}
>
MyGameenter代码在这里{“”}
);
};
导出默认标题;
这是我的平面列表:

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;
从“React”导入React;
从“react native”导入{View,FlatList,StyleSheet};
从“./data/dummy data”导入{EVENTS};
从“/Event”导入事件;
const renderGridItem=itemData=>{
返回;
};
const ShowEvents=props=>{
返回(
项目id}
数据={EVENTS}
renderItem={renderGridItem}
numColumns={1}
/>
);
};
const styles=StyleSheet.create({
名单:{
弹性:1,
辩护内容:“中心”,
对齐项目:“中心”
}
});
导出默认ShowEvents;
使用

工作示例:

import React,{Component}来自'React';
从“react native”导入{Text,View,StyleSheet,ScrollView,FlatList};
从“expo常量”导入常量;
导出默认类App扩展React.Component{
建造师(道具){
超级(道具);
此.state={
数据:[],
上一次:0,
隐藏:错,
};
}
componentDidMount(){
var数组=[];
对于(变量i=0;i<100;i++){
var a={id:i,value:i};
数组。推送(a);
}
这个.setData(数组);
}
setData(a){
这是我的国家({
数据:a,
});
}
项目({title}){
返回(
);
}
_onScroll(活动){
//console.log('>>>>>>>'+this.state.data);
if(this.state.previous<事件){
这是我的国家({
隐藏:是的,
前:事件,
});
}否则{
这是我的国家({
隐藏:错,
前:事件,
});
}
console.log(事件);
}
render(){
返回(
{this.state.hide==true?空:(
滚动时隐藏我
)}
这个.\u onScroll(e.nativeEvent.contentOffset.y)}
data={this.state.data}
renderItem={({item})=>(
)}
keyExtractor={item=>item.id}
/>
);
}
}
constyles=StyleSheet.create({});
导入React,{useState}来自“React”;
从“react native”导入{视图、平面列表、样式表、平台、维度};
从“./data/dummy data”导入{EVENTS};
从“/Event”导入事件;
常数头八=()=>{
if(Platform.OS===“android”&&Dimensions.get(“window”)。高度<600)
返回尺寸。获取(“窗口”)。高度*0.075+20;
else if(Platform.OS==“android”)
返回尺寸。获取(“窗口”)。高度*0.058+20;
否则返回尺寸。获取(“窗口”)。高度*0.01+20;
};
const renderGridItem=itemData=>{
返回;
};
const ShowEvents=props=>{
const[previous,SetPrevious]=useState(false);
const[hide,SetHide]=useState(false);
_onScroll=event=>{
常量headerHeight=headerHeight();
如果(事件>人头高度){
SetHide(真);
props.navigation.setParams({
隐藏:真的
});
前一事件;
}否则如果(事件<0.1){
SetHide(假);
props.navigation.setParams({
隐藏:假
});
前一事件;
}
};
返回(
_onScroll(e.nativeEvent.contentOffset.y)}
keyExtractor={(项,索引)=>item.id}
数据={EVENTS}
renderItem={renderGridItem}
numColumns={1}
/>
);
};
const styles=StyleSheet.create({
名单:{
弹性:1,
辩护内容:“中心”,
对齐项目:“中心”
}
});
导出默认ShowEvents;

我添加了一些更改:嗨,我有一个非常类似的问题,我有一个标题和一个平面列表,当我开始滚动页面时,标题和平面列表开始滚动。如何实现当我开始滚动页面时,页眉开始滚动,然后只有在页眉完全滚动后,平面列表才开始滚动?
onScroll={(e) => console.log(e.nativeEvent.contentOffset.y)}
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({});
import React, { useState } from "react";
import { View, FlatList, StyleSheet, Platform, Dimensions } from "react-native";

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

const HeaderHeight = () => {
  if (Platform.OS === "android" && Dimensions.get("window").height < 600)
    return Dimensions.get("window").height * 0.075 + 20;
  else if (Platform.OS === "android")
    return Dimensions.get("window").height * 0.058 + 20;
  else return Dimensions.get("window").height * 0.01 + 20;
};

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

const ShowEvents = props => {
  const [previous, SetPrevious] = useState(false);
  const [hide, SetHide] = useState(false);

  _onScroll = event => {
    const headerHeight = HeaderHeight();
    if (event > headerHeight) {
      SetHide(true);
      props.navigation.setParams({
        hide: true
      });
      SetPrevious(event);
    } else if (event < 0.1) {
      SetHide(false);
      props.navigation.setParams({
        hide: false
      });
      SetPrevious(event);
    }
  };

  return (
    <View style={styles.list}>
      <FlatList
        onScroll={e => _onScroll(e.nativeEvent.contentOffset.y)}
        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;