react native 滑动到页面的指定位置 scrollTo的使用

一、ScrollView滚动视图

其中有个方法是ScrollTo 滚动到指定的x, y偏移处。第三个参数为是否启用平滑滚动动画。

scrollTo(y: number | { x?: number, y?: number, animated?: boolean }, x: number, animated: boolean) 

使用示例:

scrollTo({x: 0, y: 0, animated: true})

具体使用方法举例:

第一种:

 使用measure测量出位置

       <Container>
            <Content>
            <ScrollView
                horizontal={true}
                ref={(view) => { this.myScrollView = view; }}
            >
                <View></View>
                <View ref={view => this.totop = view}>
                    想跳转的位置
                </View>
            </ScrollView>
            </Content>
            <Footer>
                <Button onPress={this.clickToScroll}>点击滑动到指定位置</Button>
            </Footer>
        </Container>
 clickToScroll = () => {
    //先用measure测量出位置
    this.refs.totop.measure((fx, fy, width, height, px, py) => {
        console.log('Component width is: ' + width)
        console.log('Component height is: ' + height)
        console.log('X offset to frame: ' + fx)
        console.log('Y offset to frame: ' + fy)
        console.log('X offset to page: ' + px)
        console.log('Y offset to page: ' + py)
        //然后用scrollTo跳转到对应位置
        //x是水平方向
        //y是垂直方向
        this.myScrollView.scrollTo({ x: px, y: py, animated: true });
    });
  }

第二种:

使用onLayout方法计算出位置

onLayout 当组件挂载或者布局变化的时候调用,参数为:

{nativeEvent: { layout: {x, y, width, height}}}

这个事件会在布局计算完成后立即调用一次,不过收到此事件时新的布局可能还没有在屏幕上呈现,尤其是一个布局动画正在进行中的时候。

        <Container>
            <Content>
            <ScrollView
                ref={(view) => { this.myScrollView = view; }}
            >
                <View></View>
                <View onLayout={event=>{this.layout = event.nativeEvent.layout}}>
                    想跳转的位置
                </View>
            </ScrollView>
            </Content>
            <Footer>
                <Button onPress={this.clickToScroll}>点击滑动到指定位置</Button>
            </Footer>
        </Container>
  clickToScroll = () => {
    // 其中this.layouot.y就是距离现在的高度位置 
    this.myScrollView.scrollTo({ x: 0, y: this.layout.y, animated: true});
  }

ScrollView中的 horizontal={true} 水平排列,水平滚动,默认没有,是垂直方向。

注意:当ScrollView在组件Content中滑动ios有时候会有一些问题,解决办法不放在Content或者用以下方法滑动

第二种:

添加插件react-native-keyboard-aware-scroll-view

在代码头部引入import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view';

以上的代码中用KeyboardAwareScrollView替换Content

  clickToScroll = () => {
    //测量的宽度和高度还是用以上的方法
    //直接用scrollToPosition跳转到相应的位置
    this.myContent.scrollToPosition(0,this.layoutY.y)
  }

 

以上。。。

 

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React Native是一个用于构建跨平台移动应用的开源框架,它允许开发者使用JavaScript编写应用程序,并在iOS和Android等多个平台上运行。滑动删除是React Native中常见的交互效果之一,可以通过一些组件和手势操作来实现。 在React Native中,可以使用FlatList或SwipeableListView等组件来实现滑动删除功能。其中,FlatList是一个高性能的可滚动列表组件,而SwipeableListView则是一个支持左右滑动操作的列表组件。 具体实现滑动删除的步骤如下: 1. 导入所需的组件和样式。 2. 创建一个数据源,用于渲染列表项。 3. 在renderItem函数中,定义每个列表项的布局和样式。 4. 在renderItem函数中,使用Swipeable组件包裹列表项,并设置滑动操作的效果。 5. 在Swipeable组件中,定义左滑和右滑的操作按钮,并设置相应的样式和功能。 6. 在onSwipeableLeftOpen和onSwipeableRightOpen回调函数中,处理左滑和右滑操作的逻辑,例如删除列表项或执行其他操作。 下面是一个简单的示例代码: ```javascript import React, { useState } from 'react'; import { View, Text, FlatList, TouchableOpacity, StyleSheet } from 'react-native'; import { Swipeable } from 'react-native-gesture-handler'; const SwipeableListItem = ({ item, onDelete }) => { const [swipeableRef, setSwipeableRef] = useState(null); const closeSwipeable = () => { swipeableRef && swipeableRef.close(); }; const renderRightActions = () => ( <TouchableOpacity style={styles.deleteButton} onPress={onDelete}> <Text style={styles.deleteButtonText}>Delete</Text> </TouchableOpacity> ); return ( <Swipeable ref={setSwipeableRef} friction={2} rightThreshold={40} renderRightActions={renderRightActions} onSwipeableRightOpen={closeSwipeable} > <View style={styles.listItem}> <Text>{item.title}</Text> </View> </Swipeable> ); }; const SwipeToDeleteScreen = () => { const [data, setData] = useState([ { id: '1', title: 'Item 1' }, { id: '2', title: 'Item 2' }, { id: '3', title: 'Item 3' }, ]); const handleDeleteItem = (itemId) => { setData(data.filter(item => item.id !== itemId)); }; return ( <View style={styles.container}> <FlatList data={data} keyExtractor={item => item.id} renderItem={({ item }) => ( <SwipeableListItem item={item} onDelete={() => handleDeleteItem(item.id)} /> )} /> </View> ); }; const styles = StyleSheet.create({ container: { flex: 1, padding: 16, }, listItem: { height: 60, justifyContent: 'center', borderBottomWidth: 1, borderBottomColor: '#ccc', }, deleteButton: { flex: 1, backgroundColor: 'red', justifyContent: 'center', alignItems: 'flex-end', paddingRight: 16, }, deleteButtonText: { color: 'white', }, }); export default SwipeToDeleteScreen; ``` 这是一个简单的滑动删除示例,通过FlatList和Swipeable组件实现了一个可滑动删除的列表。在这个示例中,每个列表项都可以向右滑动,显示一个红色的删除按钮,点击删除按钮后,对应的列表项会被删除。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值