import { Component } from 'react';
import {
StyleSheet,
View,
Text,
TouchableOpacity,
ScrollView,
Dimensions
} from 'react-native'; const dimen = Dimensions.get('window');
const deviceWidth = dimen.width; /**
* tab组件头部
* props data tab列表
* props style 样式
* props index 默认选中的序号
* props onChange 选中
*/
export default class TabBar extends Component { static defaultProps = {
data: [],
index: -1,
onChange: () => { },
}
constructor(props) {
super(props);
this.state = {
index: -1,
}
this.scroll = null;
this.laout_list = []
this.scrollW = 0;
}
render() {
return <View style={[tabBarStyle.tab, this.props.style]}>
<ScrollView ref={e => this.scroll = e}
horizontal directionalLockEnabled
showsHorizontalScrollIndicator={false}
snapToAlignment="center">
{this.props.data.map((item, index) =>
<TouchableOpacity onPress={() => this.setIndex(index)} onLayout={e => this.setLaout(e.nativeEvent.layout, index)} key={item.id} style={tabBarStyle.itemBtn}>
<Text style={[tabBarStyle.item, this.state.index === index ? tabBarStyle.active : null]} > {item.name}</Text>
<View style={[tabBarStyle.line, this.state.index === index ? tabBarStyle.active2 : null]}></View>
</TouchableOpacity>
)}
</ScrollView>
</View>
}
scroll = null;
laout_list = []
scrollW = 0; shouldUpdate = true;
shouldComponentUpdate() {
if (!this.shouldUpdate) return false;
return !(this.shouldUpdate = false);
}
componentWillReceiveProps(pp) {
if (pp.index != this.props.index) {
this.setState({ index: pp.index })
setTimeout(() => {
this.setIndex(pp.index, false);
}, 200);
}
}
setLaout(layout, index) {
this.laout_list[index] = layout;
this.scrollW += layout.width;
} setIndex(index, bl = true) {
this.setState({ index })
if (!this.scroll) return;
let layout = this.laout_list[index];
let rx = deviceWidth / 2;
let sx = layout.x - rx + layout.width / 2;
if (sx < 0) sx = 0;
sx < this.scrollW - deviceWidth && this.scroll.scrollTo({ x: sx, animated: bl });
sx >= this.scrollW - deviceWidth && this.scroll.scrollToEnd({ animated: bl });
this.props.onChange && this.props.onChange(index);
this.shouldUpdate = true;
}
}
const tabBarStyle = StyleSheet.create({
tab: {
backgroundColor: '#fbfafc',
flexDirection: 'row',
alignItems: "center",
justifyContent: "center",
borderBottomColor: '#efefef',
borderBottomWidth: px(1),
height: 40
},
itemBtn: {
paddingHorizontal: 12,
paddingTop: 2,
flexDirection: 'column',
justifyContent: "center",
alignItems: "center"
},
item: {
fontSize: px(28),
color: "#858385",
},
active: {
color: "#d0648f"
},
line: {
width: 20,
height: 2,
backgroundColor: "#fbfafc",
marginTop: 5,
marginBottom: 2,
},
active2: {
backgroundColor: "#d0648f"
},
sortimg: {
width: 55,
height: 40,
}
});

React native 横滑效果的更多相关文章

  1. 【React Native开发】React Native For Android环境配置以及第一个实例(1)

    年9月15日也公布了ReactNative for Android,尽管Android版本号的项目公布比較迟,可是也没有阻挡了广大开发人员的热情.能够这样讲在2015年移动平台市场上有两个方向技术研究 ...

  2. [RN] React Native 中使用 stickyHeaderIndices 实现 ScrollView 的吸顶效果

    React Native中,ScrollView组件可以使用 stickyHeaderIndices 轻松实现 sticky 效果. 例如下面代码中: <ScrollView showsVert ...

  3. React Native学习(七)—— FlatList实现横向滑动列表效果

    本文基于React Native 0.52 Demo上传到Git了,有需要可以看看,写了新内容会上传的.Git地址 https://github.com/gingerJY/React-Native-D ...

  4. React Native之ListView实现九宫格效果

    概述 在安卓原生开发中,ListView是很常用的一个列表控件,那么React Native(RN)如何实现该功能呢?我们来看一下ListView的源码 ListView是基于ScrollView扩展 ...

  5. 【React Native开发】React Native控件之Image组件解说与美团首页顶部效果实例(10)

    ),React Native技术交流4群(458982758),欢迎各位大牛,React Native技术爱好者增加交流!同一时候博客左側欢迎微信扫描关注订阅号,移动技术干货,精彩文章技术推送! Im ...

  6. [RN] React Native 头部 滑动吸顶效果的实现

    React Native 头部 滑动吸顶效果的实现 效果如下图所示: 实现方法: 一.吸顶组件封装 StickyHeader .js import * as React from 'react'; i ...

  7. [RN] React Native 使用 React-native-scrollable-tab-view 实现 类头条 新闻页头部 效果

    React Native 使用 React-native-scrollable-tab-view 实现 类头条 新闻页效果 效果如下: 一.安装依赖 npm install react-native- ...

  8. [RN] React Native 幻灯片效果 Banner

    [RN] React Native 幻灯片效果 Banner 1.定义Banner import React, {Component} from 'react'; import {Image, Scr ...

  9. react native 入门实践

    上周末开始接触react native,版本为0.37,边学边看写了个demo,语法使用es6/7和jsx.准备分享一下这个过程.之前没有native开发和react的使用经验,不对之处烦请指出.希望 ...

随机推荐

  1. 【linux】【指令集】查看是否打开selinux

    > getenforce selinux相关原理资料参考 <鸟哥的linux私房菜>  http://cn.linux.vbird.org/linux_server/0210netw ...

  2. 大意了,这几道Python面试题没有答对,Python面试题No13

    第1题: Python如何爬取 HTTPS 网站? 这类问题属于简单类问题 在使用 requests 前加入:requests.packages.urllib3.disable_warnings(). ...

  3. 面向对象之元类(metaclass)

    一.前言: 要搞懂元类必须要搞清楚下面几件事: 类创建的时候,内部过程是什么样的,也就是我们定义类class 类名()的过程底层都干了些啥 类的调用即类的实例化过程的了解与分析 我们已经知道元类存在的 ...

  4. 初级练手项目——用Python一步一步实现“智能”贪吃蛇!

    贪吃蛇作为一款经典的小游戏,想必大家一定并不陌生,今天我们就来用Python来设计与实现贪吃蛇游戏,回味一下童年的快乐.快跟着小编来看看吧! ​ 基本环境配置 ●版本:Python3 ●系统:Wind ...

  5. Django与多个数据库交互

    定义数据库 在Django中使用多个数据库的第一步是告诉Django您将要使用的数据库服务器. 数据库可以有您选择的任何别名.但是,别名 default 有着特殊的意义.Django使用别名为 def ...

  6. jquery中arrt()和prop()的区别

    在jQuery中,attr()函数和prop()函数都用于设置或获取指定的属性,它们的参数和用法也几乎完全相同. 但不得不说的是,这两个函数的用处却并不相同.下面我们来详细介绍这两个函数之间的区别. ...

  7. 数字pid笔记(1)

    针对stm32中可以如下实现: p->IncrementVal = (p->Kp * (p->err - p->err_next)) + (p->Ki * p->e ...

  8. 自动设置IP地址bat脚本

    自动获取IP及DNS: netsh interface ip set address name="本地连接" source=dhcpnetsh interface ip set d ...

  9. 重新造轮子之静态链接1(Static linking)

    最近学习计算机病毒学的过程中,又讲到了静态链接的问题,联想到了之前保健哥在信息安全的课堂上向我们展示了一个没有main()函数的C程序到底应该如何编写.个人觉得这个小实验对于加深静态链接的过程的理解也 ...

  10. HDU 3394 双连通分量 桥 Railway

    第一个答案是统计图中桥的个数 如果一个点-双连通分量中边的个数大于点的个数那么这个块中所有的边都是冲突的,累加到第二个答案中去. #include <iostream> #include ...