简体   繁体   English

当用户不在屏幕底部时如何在 ScrollView 中显示 scrollToBottom - React Native

[英]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.

相关问题 无法在特定屏幕上的 React Native 中隐藏或显示选项卡底栏 - Not able to hide or show tab bottom bar in React Native on particular screen React Native Scrollview 不会停留在底部 - React Native Scrollview doesn't stay at the bottom 无法在 React Native 中滚动到 ScrollView 的底部 - Cannot scroll to bottom of ScrollView in React Native React Native:为滚动视图底部的文本添加不透明度 - React Native: Add opacity to text at the bottom of scrollview 如何在ScrollView内修复屏幕上的视图-React Native - How to fix a View on screen inside a ScrollView - react native 在反应原生的底部选项卡导航器中更改屏幕时如何更改图标和文本的颜色? - How to change color of icon as well as text when changing screen in bottom tab navigator in react native? 当用户在 React Native 中导航到特定屏幕时,如何跟踪用户的屏幕时间? - How can I track screen time of user when a user navigate to specific screen in react native? 如何增加 ScrollToBottom 以填充屏幕的高度? - How to grow ScrollToBottom to fill the height of the screen? 如何在本机反应中隐藏特定屏幕上的底部导航栏? - How to hide bottom navigation bar on a specific screen in react native? 如何在 React Native 的某个屏幕上删除底部任务栏? - How to remove bottom taskbar on a certain screen in React Native?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM