【问题标题】:How to use PanResponder inside horizontal FlatList如何在水平 FlatList 中使用 PanResponder
【发布时间】:2019-04-23 00:52:16
【问题描述】:

我想从水平 FlatList 中拖出选定的项目并将其放到屏幕的另一个区域。问题是我的 FlatList 包装器必须有一个固定的高度,当试图将选定的元素拉出时,FlatList 组件会溢出它并且我的元素被隐藏了。

我尝试更改 zIndex 并将溢出设置为可见,但它不能正常工作,因为设置 zIndex 仅适用于兄弟姐妹并且相当错误,溢出:可见根本不影响布局。

截图如下:

这是我的代码:

import React from 'react';
import {
    SafeAreaView,
    View, 
    FlatList, 
    Text,
    StyleSheet,
    Animated,
    PanResponder
} from 'react-native';
import { v4 } from 'uuid';


export default class App extends React.PureComponent{

  state = {
    data: [
      { id: v4(), title: 'Lightcoral', hex: '#eb7474' },
      { id: v4(), title: 'Orchid', hex: '#eb74dc' },
      { id: v4(), title: 'Mediumpurple', hex: '#9a74eb' },
      { id: v4(), title: 'Mediumslateblue', hex: '#8274eb' },
      { id: v4(), title: 'Skyblue', hex: '#74b6eb' },
      { id: v4(), title: 'Paleturquoise', hex: '#93ece2' },
      { id: v4(), title: 'Palegreen', hex: '#93ecb6' },
      { id: v4(), title: 'Khaki', hex: '#d3ec93' }
    ]
  }

  _position = new Animated.ValueXY()

  _panResponder = PanResponder.create({
      onStartShouldSetPanResponder: () => true,
      onPanResponderMove: (event, gesture) => {
        this._position.setValue({ x: gesture.dx, y: gesture.dy})
      },
      onPanResponderRelease: () => {}
    })


  _keyExtractor = (item) => item.id;


  _renderItem = ({item}) => {

    return (
      <Animated.View
        style={this._position.getLayout()}
        {...this._panResponder.panHandlers}  
      >
        <View style={[styles.itemBox, {backgroundColor: `${item.hex}`}]}>
            <Text>{item.title}</Text>
        </View>

      </Animated.View>
    )
  }



  render() {
    const { data } = this.state

    return (
      <SafeAreaView style={styles.safeArea}>
        <View style={styles.container}>


        <View style={styles.targetArea}>
          <Text>Drop HERE!!!</Text>
        </View>


        <View style={{ height: 80, borderColor: 'black', borderWidth: 2 }}>
            <FlatList
                data={data}
                keyExtractor={this._keyExtractor}
                renderItem={this._renderItem}
                horizontal={true}
            />
        </View>

        </View>
      </SafeAreaView>

    );
  }
}

const styles = StyleSheet.create({
  safeArea: {
    flex: 1
  },
  container: {
    flex: 1,
    justifyContent: 'space-between',
    alignItems: 'center',
    backgroundColor: '#fff',
  },
  itemBox: {
    width: 80,
    height: 80,
    alignItems: 'center',
    justifyContent: 'center',
    borderWidth: 1,
    borderColor: '#fff'
  },
  targetArea: {
    height: 150, 
    width: 150, 
    alignItems: 'center',
    justifyContent: 'center',
    borderWidth: 1, 
    borderColor: '#eee',
    backgroundColor: '#F5FCFF',
    marginTop: 40
  }

});

我想在屏幕上移动 flatList 的选定项目并将其放在屏幕的任何其他元素之上,如果我可以简单地以编程方式设置每个元素的 zIndex - 我会结束月亮,但不幸的是,它似乎没有那么简单。请帮帮我我很绝望(((

【问题讨论】:

    标签: react-native react-native-ios react-native-flatlist


    【解决方案1】:

    在 FlatList 样式中将 overflow 设置为 visible

    【讨论】:

      猜你喜欢
      • 2022-08-07
      • 1970-01-01
      • 1970-01-01
      • 2017-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多