javascript - 在 React Native 中使用 ScrollView 自定义分页

标签 javascript react-native

在我的应用程序中,我使用 ScrollView 来显示图像。在我的 ScrollView 组件下方,我有两个按钮,例如 prevnext。我想用按钮添加自定义分页。我的图像也来自循环中的数组。我想一次显示 4 张图像。因此,next 按钮应该显示接下来的 4 张图片(如果有的话),prev 按钮应该显示前 4 张图片(如果有的话)。我的 ScrollView 目前是这样的:

    <View>
      <View style={{width:width,paddingHorizontal:10,paddingVertical:20}}>
        <ScrollView 
          horizontal={true}
          showsHorizontalScrollIndicator={false} >
 {list.map((item, i)=>( 
    <Image key={i} source={{uri: item}} 
           style={{width: 80, height: 80,marginLeft:5}} />
    )
 )}

      </ScrollView>
    </View>
  </View>

在此下方我有两个按钮。我为它们创建了两个不同的功能。这是现在的样子:

  <View>
    <Button
      title="Prev"
      color="#841584"
      onPress={handleClickBackward}
    />
</View>
<View>
  <Button
    title="Next"
    color="#841584"
    onPress={handleClickForward}
  />
</View>

我如何操作我的 list 数组,以便自定义分页有效。我已经在 next 上尝试了数组切片方法,但它删除了第一张图像,并且在按下 prev 按钮后我没有找到它们。

最佳答案

你可以使用 onNextPress 函数并在 scrolview 中添加 ref

   const scrollRef = useRef<ScrollView>();
        
            const onNextPress = () => {
                scrollRef.current?.scrollTo({
                    y : 0,//you must  increase x or y 
                    animated : true
                });
            }
        
            return (
               <View style={{width:width,paddingHorizontal:10,paddingVertical:20}}>
                <ScrollView 
                  ref={scrollRef}//ref
                  horizontal={true}
                  showsHorizontalScrollIndicator={false} >
         {list.map((item, i)=>( 
            <Image key={i} source={{uri: item}} 
                   style={{width: 80, height: 80,marginLeft:5}} />
            )
         )}
        
              </ScrollView>
            </View>
            );

关于javascript - 在 React Native 中使用 ScrollView 自定义分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65522007/

相关文章:

javascript - 解决方法 : javascript dictionary which takes objects as keys

javascript - 在 JavaScript 中创建下拉菜单/div

javascript - 如何将 QUnit 测试集成到 Yii 中

android - react-native gradle config 和 react-native-firebase 最新版本不匹配

performance - React 导航与 React Native 导航

reactjs - 在 react-navigation 5.x 中放置 createSwitchNavigator 以从 react-navigation 4 迁移到 5.x

javascript - 千位输入掩码 : Make Caret Position Move

Javascript - 检查方法原型(prototype)是否已更改?

java - 如何使用手机的加速度传感器计算振动频率(Hz或G或等效)?

react-native - 是否真的可以通过与 Expo 一起运行的 React Native 应用程序通过 Detox/Jest 测试?