Is it possible to keep a ScrollView scrolled to the bottom?

前端 未结 16 1319
误落风尘
误落风尘 2020-12-04 17:13

For a chat-like app, I want to keep a ScrollView component scrolled to the bottom, because newest messages appear under the older ones. Can we adjust the scroll

16条回答
  •  清歌不尽
    2020-12-04 17:50

    This method is a little bit hacky but I couldn't find a better way

    1. First add a ref to your ScrollView.
    2. Second add a dummy View before closing your ScrollView tag
    3. Get the y position of that dummy View
    4. Whenever you want to scroll to bottom scroll to the position of the dummy View

    var footerY; //Y position of the dummy view
    render() {    
        return (
          
              
                // This is where all the things that you want to display in the scroll view goes
                
                // Below is the dummy View
                 {
                  footerY = e.nativeEvent.layout.y;
                  }}/>
              
                  
              //Below is the button to scroll to the bottom    
               { this.refs._scrollView.scrollTo(footerY); }}>
                Scroll to Bottom
              
            
          
        );
      }

提交回复
热议问题