简体   繁体   English

水平 ScrollView 在 React Native 中不滚动

[英]Horizontal ScrollView Not Scrolling in React Native

I'm trying to render a horizontal scroll view of clickable photos.我正在尝试渲染可点击照片的水平滚动视图。 The vertical scroll works fine, but once I set horizontal={true}, I'm limited to how far I can scroll horizontally.垂直滚动效果很好,但是一旦我设置了 horizontal={true},我就只能水平滚动多远。 It prevents me from scrolling more than what is shown in this image .它阻止我滚动超过此图像中显示的内容。

<Container>
       <ScrollView
      horizontal={true}
      contentContainerStyle={{
        flex: 1,
        paddingTop: "5%"
      }}
    >
      {posts.map(post => {
        return (
          <TouchableOpacity
            key={post.id}
            style={{
              width: width,
              flex: 1,
              height: height / 3,
              borderRadius: "8px",
              overflow: "hidden",
              marginRight: "2%"
            }}
          >
            <Thumbnail
              source={{
                uri: uri`
              }}
            />
            <PostTitle>{post.name}</PostTitle>
          </TouchableOpacity>
        );
      })}
    </ScrollView>
</Container>
const Container = styled.View`
  flex: 1;
  padding-top: 18%;
  background: #fff;
`;

const Title = styled.Text`
  color: #333;
  text-transform: uppercase;
  font-weight: 600;
  padding-left: 8%;
  padding-bottom: 5%;
`;


const Thumbnail = styled.Image`
  flex: 1;
  justify-content: center;
  align-items: center;
  resize-mode: cover;
`;

const PostTitle = styled.Text`
  color: #fdfdfd;
  font-size: 16px;
  font-weight: bold;
  position: absolute;
  top: 10%;
  left: 8%;
`;

Fixed.固定的。 Removed "marginRight" and it worked.删除了“marginRight”并且它起作用了。 Horizontal ScrollView doesn't like styling its children's margins. Horizontal ScrollView 不喜欢为其子级的边距设置样式。

The problem certainly is from the margin.问题当然出在边缘。 However, I've found that specifically, setting the margin in percentage values or adding flex: 1 to contentContainerStyle is what causes this problem.但是,我发现具体来说,以百分比值设置边距或将flex: 1添加到contentContainerStyle是导致此问题的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM