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. 如何用纯 CSS 创作一个过山车 loader

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/KBxYZg/ 可交互视频 此视频是 ...

  2. 不使用脚手架的 vue 应用

    工作中的项目不止有页面繁多的模块化项目,还会只有一两个页面的类似于填写信息参与活动的活动页.这个时候,就可以回归以前的三剑客模式,在 index.html 里引用 vue.js 进行开发. 关键点: ...

  3. 序列内置方法详解(string/list/tuple)

    一.常用方法集合 1.1.string,字符串常用方法 以下举例是python2.7测试: 函数名称 作用 举例 str.capitalize() 字符串第一个字符如果是字母,则把字母替换为大写字母. ...

  4. sql 单表/多表查询去除重复记录

    单表distinct 多表group by group by 必须放在 order by 和 limit之前,不然会报错 *************************************** ...

  5. 这一千个Python库,总有你想要的!

    环境管理 管理 Python 版本和环境的工具 p – 非常简单的交互式 python 版本管理工具. pyenv – 简单的 Python 版本管理工具. Vex – 可以在虚拟环境中执行命令. v ...

  6. hdu 6318

    Long long ago, there was an integer sequence a.Tonyfang think this sequence is messy, so he will cou ...

  7. Linux学习-以最新核心版本编译 CentOS 7.x 的核心

    为了某些缘故需要最新的 4.x.y 的核心版本来实作某些特定的功能时,那该 如何是好?没办法,只好使用最新的核心版本来编译你可以依照上面的程序来一个一个处理, 没有问题~不过,你也可以根据 ELRep ...

  8. Elasticsearch安装---安装jdk

    1.在Linux 上检查Java版本是否满足要求: java -version 如果运行上面命令时报错:-bash: java: command not found,首先检查是否有jdk ,要是有安装 ...

  9. 我的PC必装软件

    我的PC必装软件 TeXLive LaTeX套装 WinEdt LaTeX编辑器 PyCharm Python IDE Inkscape 矢量图制作,我根本不会用,但是常用来把SVG转成*.tex文件 ...

  10. Leetcode with Python

    1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...