当React Native

时间:2018-01-18 01:42:09

标签: react-native

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

我希望ScrollView在内容大小增加时自动滚动Y,并在内容大小减小时滚动-Y。我该怎么做到这一点?

1 个答案:

答案 0 :(得分:0)

您可以使用“ 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方法获取。