【问题标题】:How do I make Scrollview auto scroll when the content size changes in React Native当 React Native 中的内容大小发生变化时,如何使 Scrollview 自动滚动
【发布时间】:2018-01-18 01:42:08
【问题描述】:

我目前有一个垂直的 ScrollView,里面有很多可扩展的卡片。当其中一张卡片展开时,其高度将增加 Y,因此 ScrollView 内容大小的高度将增加 Y。

我想让ScrollView在内容大小增加时自动滚动Y,当内容大小减小时滚动-Y。我应该如何做到这一点?

【问题讨论】:

    标签: react-native


    【解决方案1】:

    您可以使用“onContentSizeChange”,这将在您更改视图后返回新的高度。这对我有用。

    <ScrollView
          ref={scrollView => {
            this.scrollView = scrollView;
          }}
          onContentSizeChange={(contentWidth, contentHeight) => {
            console.log('contentHeight :==> \n', contentHeight);
            this.scrollView.scrollTo({
              x: 0,
              y: contentHeight + YOUR_VIEW_HEIGHT - SCREEN_HEIGTH,
              animated: true,
            });
          }}
        >
    

    ...

    YOUR_VIEW_HEIGHT 将从 View 的 onLayout 方法中获取。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-09
      • 1970-01-01
      • 2012-06-20
      • 2022-10-01
      • 2016-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多