Как обрабатывать жест Drag'n Drop в горизонтальном FlatList с фиксированной высотой - PullRequest
1 голос
/ 18 апреля 2019

Я пытаюсь реализовать функциональность Drag'n Drop в своем приложении React Native. Я уже довольно хорошо изучил Gesture Responder System и PanResponder, в частности, а также класс Animated, но я не могу найти правильное решение, связав их с компонентом FlatList.

Проблема в том, что когда я пытаюсь сместить элемент Responder за пределы области видимости, охватывающей компонент ViewL FlatList (height: 80), он визуально скрывается, и родительский компонент явно перекрывает его ..


Вот снимок экрана: http://joxi.ru/82Q0dn1CwwE1Vm


Вот мой код:

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, который находится в горизонтальном режиме и имеет фиксированную высоту, и переместить его в другую область экрана ?? Пожалуйста, помогите мне разобраться с этим, каждый совет очень ценится!

...