【问题标题】:React-Native: Reverse zIndex in FlatListReact-Native:在 FlatList 中反转 zIndex
【发布时间】:2019-07-03 05:00:28
【问题描述】:

我有一个 FlatList,其中包含一些顶部元素应该与底部元素重叠的元素。为此,我想反转 zIndex,但 FlatList 不断覆盖我的 zIndex。

在下面的代码中,我尝试使用 zIndex: 0 - index 反转 zIndex,但它不起作用

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

export default class App extends React.Component {
  _renderPost({ index }) {
    return <View style={[styles.item, { zIndex: 0 - index, }]} />;
  }
  render() {
    return <FlatList data={[1, 2, 3, 4, 5,]} renderItem={this._renderPost} />;
  }
}

const styles = StyleSheet.create({
  item: { 
    height: 200, 
    borderWidth:2, 
    borderBottomLeftRadius:50, 
    borderBottomRightRadius:50, 
    marginBottom: -50, 
    backgroundColor:"white",
  },
});

link to Expo Snack

【问题讨论】:

    标签: android ios react-native z-index react-native-flatlist


    【解决方案1】:

    我没有在 zIndex 的帮助下做到这一点,因为问题似乎是从索引中设置 zIndex,它似乎不起作用。

    我设法做到这一点的方法是反转 FlatList,并使用一种用于反转列 flex 方向的样式,以便它实际上也滚动到顶部。请注意,这实际上也会以相反的顺序显示帖子,因此需要翻转底层数组以实现所需的结果

    import React, { Component } from "react";
    import { FlatList, Text, View, StyleSheet } from "react-native";
    
    export default class App extends React.Component {
      _renderPost({ index }) {
        return <View style={styles.item} />;
      }
      render() {
        return <FlatList style={styles.container} inverted data={[1, 2, 3, 4, 5,]} renderItem={this._renderPost}/>;
      }
    }
    
    const styles = StyleSheet.create({
      item: { 
        height: 200, 
        borderWidth:2, 
        borderBottomLeftRadius:50, 
        borderBottomRightRadius:50, 
        marginBottom: -50, 
        backgroundColor:"white",
      },
      container: {
        flexDirection: 'column-reverse'
      }
    });

    【讨论】:

    • 反转整个列表理论上是可行的,但是列表保持在底部对齐,这会产生比仅仅反转列表输入并将初始滚动设置到顶部更多的问题,所以这个解决方案不真的不适合我。
    • @GabrielWinkler warp FlatList inside View 为我解决了这个问题。列表保持在顶部对齐。
    猜你喜欢
    • 2016-11-17
    • 2017-06-16
    • 2018-01-06
    • 2018-04-05
    • 2018-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多