ScrollView 简单使用

简单介绍一下ScrollView返回到顶部和需要定位到某个模块到顶部展示。

 类似于这个页面,点击西亚需要把西亚的页面展示到顶部,或者点击撸撸展示撸撸到顶部,点击右下角图标返回到德玛这一部分,简单实现一下。

import React, { Component } from 'react';
import { StyleSheet, Text, View, ScrollView, Image, TextInput, TouchableOpacity, StatusBar } from 'react-native';

export default class lianxi1 extends Component {
    constructor(props) {
        super(props);
        this.state = {
            tabArr: ["德玛", "西亚", "撸撸"],// tab数组
            tabIndex:0,
            viewY1:0,
            viewY2:0
        }
    }
    componentWillMount() {

    }
    render() {
        const { goBack } = this.props.navigation;
        return (
            <View style={styles.container}>
                <View style={styles.detailsTab}>
                <TouchableOpacity
                    activeOpacity={0.8}
                    style={styles.dsTaberror}
                    onPress={() => { goBack() }}
                >
                    <Image source={require("../images/back.png")} />
                </TouchableOpacity>
                <View style={styles.dsTabcenter}>
                {this.state.tabArr && this.state.tabArr.map((item, index) => {
                    return (
                        <TouchableOpacity
                            key={index}
                            activeOpacity={0.8}
                            //设置点击的标题颜色
                            style={[styles.tabItemClick, this.state.tabIndex == index ? { borderBottomColor: Colors.red } : null]}
                            key={index}
                            onPress={() => {
                                switch (index) {
                                    //点击展示对应的模块
                                    case 0:
                                        this.setState({
                                            tabIndex: index,
                                        })
                                        //设置展示对应模块
                                        this._scroll.scrollTo({ x: 0, y: 0, animated: true })
                                        break;
                                    case 1:
                                        this.setState({
                                            tabIndex: index,
                                        })
                                        this._scroll.scrollTo({ x: 0, y: this.state.viewY1, animated: true })
                                        break;
                                    case 2:
                                        this.setState({
                                            tabIndex: index,
                                        })
                                        this._scroll.scrollTo({ x: 0, y: this.state.viewY2, animated: true })
                                        break;
                                    default:
                                        this._scroll.scrollTo({ x: 0, y: 0, animated: true })
                                        break;
                                }
                            }}
                        >
                            <Text style={[styles.tabItemText, this.state.tabIndex == index ? { color: Colors.red } : null]}>
                                {item}
                            </Text>
                        </TouchableOpacity>
                    )
                })}
            </View></View>
                <ScrollView
                    ref={(ref) => this._scroll = ref}
                >

                    <View style={{height:Px2dp(400),width:SCREEN_WIDTH,backgroundColor:'#bbcc00'}}/>

                    <View
                        //设置对应模块的高度位置
                        onLayout={(e) => {
                            this.setState({
                                viewY1: e.nativeEvent.layout.y,
                            })
                        }}
                        style={{height:Px2dp(400),width:SCREEN_WIDTH,backgroundColor:'#aabbee'}}/>


                    <View
                        onLayout={(e) => {
                            //设置对应模块的高度位置
                            this.setState({
                                viewY2: e.nativeEvent.layout.y,
                            })
                        }}
                        style={{height:Px2dp(400),width:SCREEN_WIDTH,backgroundColor:'#ccbbaa'}}/>

                    <View style={{height:Px2dp(1000),width:SCREEN_WIDTH,backgroundColor:'#ffddaa'}}/>
                </ScrollView>
                {/*返回顶部的点击按钮*/}
                <TouchableOpacity
                    onPress={()=>  this.setState({
                        tabIndex: 0,
                    },()=>this._scroll.scrollTo({ x: 0, y: 0, animated: true }))}
                    activeOpacity={.8}
                    style={[{ position: 'absolute', bottom: Px2dp(15), right: Px2dp(15) }, this.props.style]}
                >
                    <Image source={require('../newImages/to_top_arrow.png')} />
                </TouchableOpacity >
            </View>
        );
    }

}
const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: Colors.mainBg,
    },
    // 头部布局
    detailsTab: {
        backgroundColor: Colors.white,
        paddingTop: StatusBarHeight,
        height: NavHeight,
        flexDirection: 'row',
        alignItems: 'center',
        justifyContent: 'space-between',
    },
    dsTaberror: {
        justifyContent: 'center',
        alignItems: 'center',
        paddingLeft: Px2dp(16),
        paddingRight: Px2dp(16),
        height: NavHeight - StatusBarHeight,
    },
    dsTabcenter: {
        flex: 1,
        flexDirection: 'row',
        height: NavHeight - StatusBarHeight,
        justifyContent: 'space-around',
        alignItems: 'center',
    },
    tabItemText: {
        color: Colors.text666,
        fontSize: Px2dp(14),
        lineHeight: NavHeight - StatusBarHeight,
    },
    tabItemClick: {
        height: NavHeight - StatusBarHeight,
        justifyContent: 'center',
        alignItems: 'center',
        borderBottomWidth: Px2dp(2),
        borderBottomColor: 'transparent',
    },
});

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值