【发布时间】:2020-06-14 02:59:40
【问题描述】:
目标:有一个下拉视图,随着时间的推移动画高度扩展。需要注意的是:一旦视图展开,它需要能够动态处理是否存在额外的视图数据。如果存在,将呈现几个额外的文本组件。
问题:就目前而言,动画父级的高度为固定高度。所以当additionalContent 被渲染时,它超出了父级的边界,其高度是固定的。我不想不明确设置父级的高度,因为那样我就无法按照我想要的方式为该方面设置动画。我想按原样保持高度动画,并在存在additionalContent 时动态调整父级以包含子级
const ListItem = (props) => {
const [checkInModal, setCheckInModal] = useState(false);
const [animatedHeight, setAnimatedHeight] = useState(new Animated.Value(0))
const [animatedOpacity] = useState(new Animated.Value(0))
const [dynamicHeight, setDynamicHeight] = useState(0);
const [expanded, setExpanded] = useState(false);
const toggleDropdown = () => {
if (expanded == true) {
// collapse dropdown
Animated.timing(animatedHeight, {
toValue: 0,
duration: 200,
}).start()
} else {
// expand dropdown
Animated.timing(animatedHeight, {
toValue: 100,
duration: 200,
}).start()
}
setExpanded(!expanded)
}
const renderAdditionalContent = () => {
setDynamicHeight(75);
if (someVariable == true) {
return (
<View> <Text> Some Content </Text> </View>
)
}
}
const interpolatedHeight = animatedHeight.interpolate({
inputRange: [0, 100],
outputRange: [75, 225]
})
const interpolatedOpacity = animatedOpacity.interpolate({
inputRange: [0, 100],
outputRange: [0.0, 1.0]
})
return (
<Animated.View
style={[styles.container, { height: interpolatedHeight + dynamicHeight }]}
>
<View style={{ flexDirection: 'row', justifyContent: 'space-between', }}>
<View style={styles.leftContainer}>
<View style={{ flexDirection: 'row', alignItems: 'center' }}>
<Text style={styles.title}>{props.title}</Text>
</View>
<Text style={styles.subtitle}>{time()}</Text>
</View>
<View style={styles.rightContainer}>
<TouchableOpacity onPress={() => toggleDropdown()} style={styles.toggleBtn}>
<Image source={require('../assets/img/chevron-down.png')} resizeMode={'contain'} style={styles.chevron} />
</TouchableOpacity>
</View>
</View>
{expanded == true ? (
<Animated.View style={[styles.bottomContainer, { opacity: interpolatedOpacity }]}>
<Components.BodyText text="Subject:" style={{ fontFamily: Fonts.OPENSANS_BOLD }} />
<Components.BodyText text={props.subject} />
{ renderAdditionalContent() }
</Animated.View>
) : null}
</Animated.View>
);
};
const styles = StyleSheet.create({
container: {
backgroundColor: '#fff',
borderRadius: 25,
width: width * 0.95,
marginBottom: 5,
marginHorizontal: 5,
paddingVertical: 15,
paddingHorizontal: 15
},
leftContainer: {
justifyContent: 'space-between',
},
rightContainer: {
flexDirection: 'row',
alignItems: 'center'
},
title: {
fontFamily: Fonts.OPENSANS_BOLD,
fontSize: 20,
color: '#454A66'
},
subtitle: {
color: '#454A66',
fontSize: 14
},
typeIcon: {
height: 25,
width: 25
},
chevron: {
height: 15,
width: 15
},
toggleBtn: {
borderWidth: 1,
borderColor: Colors.PRIMARY_DARK,
borderRadius: 7,
paddingTop: 4,
paddingBottom: 2.5,
paddingHorizontal: 4,
marginLeft: 10
},
bottomContainer: {
marginVertical: 20
},
buttonContainer: {
flexDirection: 'row',
width: 250,
justifyContent: 'space-between',
alignSelf: 'center',
marginVertical: 20
},
noShadow: {
elevation: 0,
shadowOffset: {
width: 0,
height: 0
},
shadowRadius: 0,
}
});
export default ListItem;
如何做到这一点?到目前为止,我尝试创建一个状态变量 dynamicHeight 并将其设置在呈现其他内容的函数中,但这没有奏效。
这是小吃:https://snack.expo.io/P6WKioG76
澄清编辑:renderAdditionalContent 函数渲染附加内容(显然),该内容可以是从一行字符到多行的任何位置。无论字符数如何,组件的主父容器都需要将所有子容器都放在其范围内。就目前而言,如果附加内容渲染的内容过多,则内容会溢出组件的主父容器的边框,这是必须避免的。显然,这可以通过简单地不给主组件容器指定高度来完成。但我们的想法是拥有动画高度并正确包装子内容
有什么建议吗?
【问题讨论】:
-
你能创建一个工作演示吗?那么我们可以为您提供更多帮助
-
@VivekDoshi 检查零食^
-
展开点击时抛出错误
-
@VivekDoshi 现在看。部分错误是我问这个问题的原因。我注释掉了将具有动态大小内容的部分
-
好的,那么预期的行为是什么?
标签: reactjs react-native react-animated