[英]How to show scrollToBottom in a ScrollView when user is not at the bottom of screen - React Native
I'm developing a chat app in which I have a ChatScreen
, and suppose the user scrolls up through the chat, now a scrollToBottom
button should appear only if the user is not at bottom (just like popular chat apps), So I tried the following:我正在开发一个聊天应用程序,其中有一个
ChatScreen
,假设用户向上滚动聊天,现在只有当用户不在底部时才会出现scrollToBottom
按钮(就像流行的聊天应用程序一样),所以我尝试了下列的:
First I tried to detect when the user scrolls in a ScrollView
, then using the onScroll
prop nativeEvent
I detected whether or not the user is not at bottom ie he/she scrolled up.首先,我尝试检测用户何时在
ScrollView
中滚动,然后使用onScroll
prop nativeEvent
检测用户是否不在底部,即他/她向上滚动。 Then if it is the case, I show the button which should appear in the bottom right corner of the ChatScreen.然后,如果是这种情况,我会显示应该出现在 ChatScreen 右下角的按钮。
Below is the code for my approach.下面是我的方法的代码。 (Is it possible to return a
View
component inside onScroll
prop if the condition satisfies?) (如果条件满足,是否可以在
onScroll
prop 内返回一个View
组件?)
<ScrollView
ref={scrollViewRef}
onScroll={({nativeEvent}) => {
const isCloseToBottom = nativeEvent.layoutMeasurement.height + nativeEvent.contentOffset.y > nativeEvent.contentSize.height - 30;
if (!isCloseToBottom) {
console.log('show scroll to bottom button')
return (
<TouchableOpacity style={{position:'absolute', bottom:40, right:10, backgroundColor:'grey'}} onPress={() => scrollViewRef.current.scrollToEnd()}>
<Icon ... />
</TouchableOpacity>
)
}
}}
>
...
</ScrollView>
So this is what I tried, although when I debug using console.log('show scroll to bottom button')
as you can see in the code, it gets triggered, but I'm not able to render/see the Button component on the screen.所以这就是我尝试的方法,尽管当我使用
console.log('show scroll to bottom button')
调试时,正如您在代码中看到的那样,它被触发了,但我无法在屏幕。 (I tried setting opacity:1
& zIndex:1
in the style prop of <TouchableOpacity />
but still can't see). (我尝试在
<TouchableOpacity />
的样式属性中设置opacity:1
& zIndex:1
但仍然看不到)。
What am I doing wrong?我究竟做错了什么? Any solution to this problem would be really appreciated.
任何解决此问题的方法都将不胜感激。
Okay, the temporary solution is just create a state and do conditional rendering.好的,临时解决方案就是创建一个 state 并进行条件渲染。 (I think this approach would be not optimum because we would be setting state everytime the user scrolls up or whenever
onScroll
triggers). (我认为这种方法不是最优的,因为我们会在每次用户向上滚动或
onScroll
触发时设置 state)。
const [isScrolling, setIsScrolling] = useState(false);
<ScrollView
ref={scrollViewRef}
onScroll={({nativeEvent}) => {
const isCloseToBottom = nativeEvent.layoutMeasurement.height + nativeEvent.contentOffset.y > nativeEvent.contentSize.height - 30;
if (!isCloseToBottom) {
setIsScrolling(true);
} else setIsScrolling(false);
}}
>
...
</ScrollView>
{
isScrolling && // return your component here...
}
I assume your first render view is always at the bottom.我假设您的第一个渲染视图始终位于底部。 So you could wrap your component with parenthesis and set a conditional first, something like:
所以你可以用括号包裹你的组件并首先设置一个条件,比如:
const [isBottom, setIsBottom] = useState(true)
<>
<ScrollView
onScroll={(e) => {handleScrolling(e)}}
>
return
</ScrollView>
{!isBottom &&
<Touchable style={{ position: 'absolute', right: 20, bottom: 20}}>
<Text>Last messages</Text>
</Touchable>
}
</>
In case you want to add some animation you could use Animated.View
instead.如果您想添加一些 animation,您可以改用
Animated.View
。
Another way to verify if the user is on the bottom is by using the index
but you'll need to use a <FlatList/>
.另一种验证用户是否在底部的方法是使用
index
,但您需要使用<FlatList/>
。 Something like:就像是:
if (index !== data.lenght -1) {
setIsBottom(false)
}
Pass the value on your const renderItem = (index) => {}
在您的
const renderItem = (index) => {}
上传递值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.