简体   繁体   English

反应本机拖动部分列表

[英]React native draggle section list

I am want to make a draggable section list.我想制作一个可拖动的部分列表。 Where I can drag one item from the section list and move it to another section.我可以从部分列表中拖动一个项目并将其移动到另一个部分。

I am try to do this using.我正在尝试使用。

react-native-draggable-flatlist

Issue is that when I select an item and drage it.问题是当我 select 一个项目并拖动它时。 It takes the whole list.它需要整个列表。

import React, { FC, useState } from "react";
import { Text, View, StyleSheet, TouchableOpacity } from "react-native";
import DraggableFlatList, {
  RenderItemParams,
  ScaleDecorator,
} from "react-native-draggable-flatlist";
import { GestureHandlerRootView } from "react-native-gesture-handler";
import { Data } from "./data";
import Example from "./example";

interface Item {
  header: string;
  data: string[];
}

const DATA: Item[] = [
  {
    header: "A",
    data: ["A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "A10"],
  },
  {
    header: "B",
    data: ["B1", "B2", "B3", "B4", "B5", "B6", "B7", "B8", "B9", "B10"],
  },
];

interface IList {
  item: string;
  selected: (item: string) => void;
  active: boolean;
}

const List: FC<IList> = ({ item, selected, active }) => {
  const getSelected = () => {
    console.log("item", item);
    selected(item);
  };
  return (
    <TouchableOpacity
      style={[
        styles.draggableContainer,
        {
          backgroundColor: active ? "blue" : "white",
        },
      ]}
      onLongPress={getSelected}
      onPress={getSelected}
    >
      <Text style={styles.text}>{item}</Text>
    </TouchableOpacity>
  );
};

export default function App() {

  const renderContent = (item: Item, drag: () => void, isActive: boolean) => {
    return (
      <View style={styles.item}>
        {item?.data?.map((seat) => (
          <List key={seat} item={seat} selected={drag} active={isActive} />
        ))}
      </View>
    );
  };

  const renderSectionHeader = (section: Item) => (
    <View style={styles.header}>
      <Text style={styles.headerText}>{section.header}</Text>
    </View>
  );

  const renderItem = ({ item, drag, isActive }: RenderItemParams<Item>) => (
    <View style={{ flex: 1 }}>
      <View>{renderSectionHeader(item)}</View>
      <View style={{ flex: 1 }}>{renderContent(item, drag, isActive)}</View>
    </View>
  );

  return (
    <GestureHandlerRootView style={{ flex: 1 }}>
      <View style={styles.container}>
        <DraggableFlatList
          data={DATA}
          renderItem={renderItem}
          keyExtractor={(item, index) => `draggable-item-${item?.header}`}
        />
      </View>
    </GestureHandlerRootView>
  );
}

const styles = StyleSheet.create({
  item: {
    borderRadius: 5,
    marginVertical: 8,
    marginHorizontal: 16,
  },
  header: {
    backgroundColor: "#f9c2ff",
    height: 30,
    justifyContent: "center",
    paddingLeft: 10,
  },
  headerText: {
    fontSize: 16,
    fontWeight: "bold",
  },
  text: {
    fontSize: 18,
  },
  draggableContainer: {
    flexDirection: "column",
    flex: 1,
    paddingVertical: 10,
  },
  draggable: {
    flex: 1,
  },
  container: {
    flex: 1,
  },
});

I have tryed using draxList but I was getting peformace issues.我曾尝试使用 draxList,但遇到了性能问题。 Draggable flat list has the best performance.可拖动平面列表具有最佳性能。

I have so many times react-native-draggable-flatlist and I have the code for this below.我有很多次react-native-draggable-flatlist并且我有下面的代码。 it works for me fine you can update your code like this.它对我很好,你可以像这样更新你的代码。

<DragableFlatlist
        ItemSeparatorComponent={() => (
        <View style={styles.itemSeprator} />
        )}
        data={drag}
        keyExtractor={(item) => item.id}
        nestedScrollEnabled
        onDragEnd={({ data }) => setDrag(data)}
        renderItem={renderItemDrag}
        showsVerticalScrollIndicator={false}
    />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM