React Native Elements ListItem.Accordion items cause SectionList to scroll (much) further than desired

Viewed 2040

I'm using a react-native-elements ListItem.Accordion because everything else in my React Native SectionList uses ListItems (and the documentation seems un-opinionated about this), and I'm very happy with the rendering. Unfortunately, when scrolling down it scrolls beyond the last displayed item until it gets to where the end would be if all the accordion items were fully expanded.

Obviously, when all the accordion items are expanded it stops scrolling when it gets to the end.

How do I get the SectionList to only scroll to what's visible when the accordion items are not expanded?

    const renderSectionHeader = ({ section: { title, data } }) => {
        return { data.length > 0 ? (
            <Text>{title}</Text>
        ) : null };
    }

    const renderSeparator = () => {
        return (
            <View style={{
                height: 1,
                backgroundColor: "#CED0CE",
                marginLeft: "5%",
                width: "90%",
            }} />
        );
    };

    const renderItem = ({ item }) => {
        return (
            <ListItem.Accordion
                content={
                    <>
                        <MaterialIcons.Button
                            name="shopping-basket"
                            style={{paddingLeft:20}}
                            onPress={() => { alert(item.greeting) }} />
                        <ListItem.Content style={{marginLeft:15}}>
                            <ListItem.Title>{item.title}</ListItem.Title>
                            <ListItem.Subtitle>{item.subtitle}</ListItem.Subtitle>
                        </ListItem.Content>
                    </>
                }
                isExpanded={isExpanded}
                onPress={() => {
                    toggleAccordionItem(item.key);
                }}
            >
                <ListItem item={item}>
                    <MaterialIcons.Button
                        name="airport-shuttle"
                        onPress={() => { alert(item.direction) }}>
                        <Text>Show Direction</Text>
                    </MaterialIcons.Button>
                </ListItem>
            </ListItem.Accordion>
        );
    };

    return (
        <SafeAreaView>
            <SectionList
                refreshing={!isRefreshing}
                sections={sections}
                renderItem={renderItem}
                renderSectionHeader={renderSectionHeader}
                ItemSeparatorComponent={renderSeparator}
                keyExtractor={(item) => item.key}
            />
        </SafeAreaView>
    )
1 Answers

Just started using React Native last week, but my workaround for a similar situation was to use the ListItem inside the accordion like this

{isExpanded && <ListItem item={item} />}

But it turns the animation of the accordion clunky. Don't know which is the correct way to address the issue, if someone has experiencied this issue before and know what is the proper way to solve i would appreciate

Related