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关于yum

    yum仓库设置:1.cd /etc/yum.repos.d yum仓库 2.CentOS-Base.repo 网络源 CentOS-Media.repo 光盘源 设置 vi CentOS-Media. ...

  2. Vue源码探究-全局API

    Vue源码探究-全局API 本篇代码位于vue/src/core/global-api/ Vue暴露了一些全局API来强化功能开发,API的使用示例官网上都有说明,无需多言.这里主要来看一下全局API ...

  3. 21.Yii2.0框架多表关联一对多查询之性能优化--模型的使用

    控制器里 功能: 通过分类,查分类下的所有文章 //关联查询 public function actionRelatesearch(){ //关联查询 //查询方法一(查一行) 一维数组下的值是obj ...

  4. 第6章 AOP与全局异常处理6.5-6.11 慕课网微信小程序开发学习笔记

    https://coding.imooc.com/learn/list/97.html 目录: 第6章 AOP与全局异常处理6-1 正确理解异常处理流程 13:236-2 固有的处理异常的思维模式与流 ...

  5. Python简单试题2

    1,给定一组数据,将奇数进行从小到大排序,偶数在原位置不变 例: 原数据:[5, 3, 2, 8, 1, 4]   排序后:[5, 3, 2, 8, 1, 4] 若数组为空,则返回空数组. 代码如下: ...

  6. Mysql 安装及MySQL-python 问题

    今天遇到了个低级问题: EnvironmentError:mysql_config not found 网上谷歌了一圈发现没用,静下来想的时候才发现新电脑没安装Mysql,吐血 后面再去官网上下载My ...

  7. js适配根字体大小

    方法一:<script>                (function (doc, win) {                var docEl = doc.documentElem ...

  8. [python xml 学习篇][0]

    tree = ET.parse("Result.xml")root = tree.getroot()print type(root)print root.tag # 得到root ...

  9. javascript学习笔记 - 引用类型 Object

    引用类型是一种数据结构,也称作对象定义,类似于类的概念. 对象是引用类型的实例. javascript引用类型有:Object, Array, Date, RegExp, Function 使用new ...

  10. IO Streams:字符流

    简介 Java平台使用Unicode约定存储字符值.字符流I / O自动将此内部格式转换为本地字符集.在西方,本地字符集通常是ASCII的8位超集. 对于大多数应用,具有字符流的I / O并不比具有字 ...