最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Drag and Drop between two draggable Flatlist-React Native - Stack Overflow

programmeradmin2浏览0评论

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 mon 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(), please let me know if there are any better option

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 mon 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./puterjazz/react-native-draggable-flatlist), please let me know if there are any better option

Share Improve this question edited Oct 5, 2023 at 15:02 Per Quested Aronsson 12.2k8 gold badges58 silver badges77 bronze badges asked Nov 23, 2019 at 6:31 Saikat SahaSaikat Saha 8382 gold badges19 silver badges46 bronze badges 2
  • 1 I would remend you to use react-native-gesture-handler. It would be great if you provided some image of your design or minimal code! – OriHero Commented Nov 30, 2019 at 13:11
  • 1 I don't have any design as of now it's very simple, the top portion will be a blank dropbox and the bottom one is a draggable flatlist, I have to drag the item from bottom to top and vice-versa and also all flatlist item should have a right side menu on click to which I can move the item to the another flatlist – Saikat Saha Commented Nov 30, 2019 at 14:19
Add a ment  | 

2 Answers 2

Reset to default 3

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!

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 ponent

发布评论

评论列表(0)

  1. 暂无评论