Drag and Drop between two draggable Flatlist-React Native

亡梦爱人 提交于 2020-05-10 21:13:08

问题


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) and a draggable flatlist from where I have to drag and drop to the blank dropbox and vice-versa.

The items in both the flatlists should have a right side menu on click to which should show a option to move to the item to the other flatlist.

I know it's a very common scenario but as I'm quite new to React-Native so I am struggling to get any library or create the same by myself.

I'm using React-Native with Redux and Typescript

I'm using react-native-draggable-flatlist for Flatlist(https://github.com/computerjazz/react-native-draggable-flatlist), please let me know if there are any better option


回答1:


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!




回答2:


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



来源:https://stackoverflow.com/questions/59005165/drag-and-drop-between-two-draggable-flatlist-react-native

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!