是否可以将项目拖到水平FlatList的范围之外?

时间:2019-04-24 15:51:28

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

我正在使用React Native库处理FotoBook应用程序。在我让用户从设备库中选择图像之后,我将它们以水平模式放入FlatList组件中。那么如何将选定的图像拖放到屏幕的另一部分

我正在使用PanResponder和Animated API,它可以很好地转换响应者的坐标,但是当我尝试超出水平FlatList的范围时,它就被隐藏了。我试图将溢出设置为可见并更改zIndex,但它不起作用

以下是屏幕截图:

Screenshot of app before dragging FlatList items Screenshot of app after dragging FlatList items

这是我的代码:

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
  }

});

0 个答案:

没有答案