简体   繁体   English

如何在 React Native 中仅扩展列表项手风琴中的一项?

[英]How to expand only one item from a list item accordion in react native?

I started to learn React Native lately and I'm trying to build my first app, but i'm facing this issue and I don't know how to solve it, I used library React Native Elements and ListItem.Accordion and I create this element:我最近开始学习 React Native,我正在尝试构建我的第一个应用程序,但我遇到了这个问题,我不知道如何解决它,我使用库 React Native Elements 和 ListItem.Accordion 并创建了这个元素:

{meals.map((item, index) => {
        return (
          <ListItem.Accordion 
          theme={{ colors: { primary: "#4169e1" } }}
          style={tw`px-3 rounded py-3 text-white`}
          containerStyle={{ backgroundColor: "#000000", borderRadius: "10px" }}
          content={
            <>
              <Text style={tw`text-5xl mr-2`}>☀️</Text>
              <ListItem.Content>
                <ListItem.Title style={tw`font-bold text-white`}>{item.title}</ListItem.Title>
                <Text style={tw`text-white`}>480 calories</Text>
              </ListItem.Content>
            </>
          }
          key={index}
          index={index}
          noRotation
          isExpanded={expanded}
          icon={
            <Icon
              style={tw`mt-3 p-2`}
              size={35}
              name="add-circle-outline"
              color="white"
              type="material"
            />
          }
          onPress={() => {
            setExpanded(!expanded);
          }}
        >
          <ListItem
            containerStyle={{ backgroundColor: "#000000", borderRadius: "10px" }}
            style={tw`px-3 mt-[-25]`}
            onPress={() => console.log("hi")}
            bottomDivider
          >
            <ListItem.Content>
              <ListItem.Title>
                <Divider width={1} style={tw`w-full`} />
                <View style={tw`justify-around items-center flex-row w-full`}>
                  <View>
                    <Text style={tw`text-gray-400`}>Fats</Text>
                    <Text style={tw`font-bold text-white`}>28.4</Text>
                  </View>
                  <View>
                    <Text style={tw`text-gray-400`}>Carbs</Text>
                    <Text style={tw`font-bold text-white`}>42.32</Text>
                  </View>
                  <View>
                    <Text style={tw`text-gray-400`}>Prot</Text>
                    <Text style={tw`font-bold text-white`}>60,45</Text>
                  </View>
                  <View>
                    <Text style={tw`text-gray-400`}>RDC</Text>
                    <Text style={tw`font-bold text-white`}>12%</Text>
                  </View>
                </View>
              </ListItem.Title>
              <ListItem.Subtitle style={tw`p-2`}>
                <View style={tw`py-3`}>
                  <View
                    style={tw`flex-row py-5 items-center justify-between w-full`}
                  >
                    <View style={tw`flex-row ml-2`}>
                      <Icon
                        name="restaurant-menu"
                        size={50}
                        color="white"
                        type="material"
                      />
                      <View style={tw`ml-3 justify-center`}>
                        <Text style={tw`font-bold text-white`}>Pepperoni Sandwich</Text>
                        <Text style={tw`text-white`}>2 pieces</Text>
                      </View>
                    </View>
                    <Icon name="edit" size={30} color="white" type="material" />
                  </View>
                </View>
              </ListItem.Subtitle>
            </ListItem.Content>
          </ListItem>
        </ListItem.Accordion>
        )
      })}
    </View>
  );
};

When I press, all items are expanded, when I want to achieve is displaying only the item that I select, can anyone give me some advice please?当我按下时,所有项目都会展开,当我要实现的是只显示我select的项目时,谁能给我一些建议吗? Thanks in advance:)提前致谢:)

Take a look at what you're doing here:看看你在这里做什么:

isExpanded={expanded}

You have one boolean value to indicate whether any and all lists are "expanded".您有一个boolean 值来指示是否所有列表都已“扩展”。 A boolean value has exactly two states, true or false. boolean 值恰好有两种状态,真或假。 So your list has exactly two states, all expanded or all collapsed.所以你的列表正好有两种状态,全部展开或全部折叠。

Instead, store these states as multiple boolean values.相反,将这些状态存储为多个boolean 值。 If you can modify the meals array, that would be the ideal place to put it as an additional property on the objects in that array.如果您可以修改meals数组,那将是将其作为该数组中对象的附加属性放置的理想位置。 So you might instead have this:所以你可能会这样:

isExpanded={item.expanded}

And update it with something like this:并用这样的东西更新它:

onPress={() => {
  setMeals(meals.map(m =>
    m.id === item.id ?
    {...m, expanded: !m.expanded} :
    m
  ));
}}

Note of course that this is all based on assumptions about your meals array (such as having an id on items) and is for illustration purposes only.当然请注意,这都是基于对您的meals数组的假设(例如在项目上有一个id )并且仅用于说明目的。 The idea here is that each object in the meals array has an expanded property, and the event handler simply updates that property for the specific object being used here.这里的想法是meals数组中的每个 object 都有一个expanded属性,事件处理程序只需为此处使用的特定 object 更新该属性。


Alternatively you could keep the list of "expanded items" in a separate array.或者,您可以将“扩展项目”列表保存在单独的数组中。 Keeping two arrays synchronized in terms of length and sorting can be difficult, but you could just have an array of "currently expanded items":保持两个 arrays 在长度和排序方面同步可能很困难,但你可以只拥有一个“当前扩展项目”的数组:

const [expandedItems, setExpandedItems] = useState([]);

Then just add/remove from that array.然后只需从该数组中添加/删除。 So to use it you'd do this:所以要使用它你会这样做:

isExpanded={expandedItems.includes(item.id)}

And you'd update it something like this:你会像这样更新它:

onPress={() => {
  if (isExpanded.includes(item.id)) {
    setIsExpanded(isExpanded.filter(i => i !== item.id));
  } else {
    setIsExpanded([...isExpanded, item.id]);
  }
}}

The idea here is to just have an array of ID values indicating which elements of the list are "expanded".这里的想法是只有一个 ID 值数组,指示列表的哪些元素被“扩展”。 And you'd remove it from the list to collapse or add it to the list to expand.您可以将其从列表中删除以折叠或将其添加到列表以展开。


Any way you structure it, the overall concept is still the same.无论您以何种方式构建它,总体概念仍然是相同的。 In order to track the states of these individual elements separately, you need to have separate values for them.为了单独跟踪这些单独元素的状态,您需要为它们设置单独的值。

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

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