Drag and Drop between two draggable Flatlist-React Native

后端 未结 2 1834
醉话见心
醉话见心 2021-02-10 16:39

I\'m struggling to create a requirement for my React-Native application where I am having a blank dropbox(on dropping a flatlist item it should convert into a draggable flatlist

相关标签:
2条回答
  • 2021-02-10 16:46

    I have created the minimal example with react-native-gesture-handler let me know if it helps :

    let dropzoneHeight = 200;
    let itemHeight = 60;
    
    let FlatItem = ({ item }) => {
      let translateX = new Animated.Value(0);
      let translateY = new Animated.Value(0);
      let onGestureEvent = Animated.event([
        {
          nativeEvent: {
            translationX: translateX,
            translationY: translateY,
          },
        },
      ]);
      let _lastOffset = { x: 0, y: 0 };
      let onHandlerStateChange = event => {
        if (event.nativeEvent.oldState === State.ACTIVE) {
          _lastOffset.x += event.nativeEvent.translationX;
          _lastOffset.y += event.nativeEvent.translationY;
          translateX.setOffset(_lastOffset.x);
          translateX.setValue(0);
          translateY.setOffset(_lastOffset.y);
          translateY.setValue(0);
    
          //TODO here you have to check if the item is inside of container and if it is do some calculations to relocate it to your container.
        }
      };
      return (
        <PanGestureHandler
          onGestureEvent={onGestureEvent}
          onHandlerStateChange={onHandlerStateChange}>
          <Animated.View
            style={[styles.item, { transform: [{ translateX }, { translateY }] }]}>
            <Text>{item.id}</Text>
          </Animated.View>
        </PanGestureHandler>
      );
    };
    
    let data = [
      { key: 1, id: 1 },
      { key: 2, id: 2 },
      { key: 3, id: 3 },
      { key: 4, id: 4 },
    ];
    export default class App extends React.Component {
      render() {
        return (
          <View style={styles.container}>
            <FlatList
              data={data}
              ListHeaderComponent={() => <View style={styles.dropzone} />}
              renderItem={props => <FlatItem {...props} />}
              style={{ flex: 1 }}
            />
          </View>
        );
      }
    }
    

    Here is the snack if you want to test!

    0 讨论(0)
  • 2021-02-10 16:53

    More performant way using declarative api from react-native-reanimated

    <FlatList data={new Array(10).fill(0)}
      renderItem={({item, index}) => <GestureItem key={index} item={item} />}
     />
    
    
    const GestureItem = ({item})=> {
      const safex = new A.Value(0)
      const safeY = new A.Value(0)
      const draX = new Value(0)
      const dragY = new Value(0)
      const panState = new Value(State.UNDETERMINED)
      const onGestureEvent = A.event([
          {
            nativeEvent: {
              translationX: dragX,
              translationY: dragY,
              state: panState,
            },
          },
        ]);
    
       const transX = cond(
          eq(panState, State.ACTIVE),
          dragX,
          set(safeX, add(safeX, dragX))
        );
    
       const transY = cond(
          eq(panState, State.ACTIVE),
          dragY,
          set(safeY, add(safeY, dragY))
        );
      return (
        <PanGestureHandler 
          onGestureEvent={onGestureEvent}
          onHandlerStateChange={onGestureEvent}>
    
          <A.View style={{width: 100, height : 100, transform :[{translateX: transX}, {translateY: transY}]}} >
         </A.View>
        </PanGestureHandler>
    
    
      )
    }
    

    For swaping list items and their indexes use Code component

    0 讨论(0)
提交回复
热议问题