React native 横滑效果
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 横滑效果的更多相关文章
- 【React Native开发】React Native For Android环境配置以及第一个实例(1)
年9月15日也公布了ReactNative for Android,尽管Android版本号的项目公布比較迟,可是也没有阻挡了广大开发人员的热情.能够这样讲在2015年移动平台市场上有两个方向技术研究 ...
- [RN] React Native 中使用 stickyHeaderIndices 实现 ScrollView 的吸顶效果
React Native中,ScrollView组件可以使用 stickyHeaderIndices 轻松实现 sticky 效果. 例如下面代码中: <ScrollView showsVert ...
- React Native学习(七)—— FlatList实现横向滑动列表效果
本文基于React Native 0.52 Demo上传到Git了,有需要可以看看,写了新内容会上传的.Git地址 https://github.com/gingerJY/React-Native-D ...
- React Native之ListView实现九宫格效果
概述 在安卓原生开发中,ListView是很常用的一个列表控件,那么React Native(RN)如何实现该功能呢?我们来看一下ListView的源码 ListView是基于ScrollView扩展 ...
- 【React Native开发】React Native控件之Image组件解说与美团首页顶部效果实例(10)
),React Native技术交流4群(458982758),欢迎各位大牛,React Native技术爱好者增加交流!同一时候博客左側欢迎微信扫描关注订阅号,移动技术干货,精彩文章技术推送! Im ...
- [RN] React Native 头部 滑动吸顶效果的实现
React Native 头部 滑动吸顶效果的实现 效果如下图所示: 实现方法: 一.吸顶组件封装 StickyHeader .js import * as React from 'react'; i ...
- [RN] React Native 使用 React-native-scrollable-tab-view 实现 类头条 新闻页头部 效果
React Native 使用 React-native-scrollable-tab-view 实现 类头条 新闻页效果 效果如下: 一.安装依赖 npm install react-native- ...
- [RN] React Native 幻灯片效果 Banner
[RN] React Native 幻灯片效果 Banner 1.定义Banner import React, {Component} from 'react'; import {Image, Scr ...
- react native 入门实践
上周末开始接触react native,版本为0.37,边学边看写了个demo,语法使用es6/7和jsx.准备分享一下这个过程.之前没有native开发和react的使用经验,不对之处烦请指出.希望 ...
随机推荐
- DC 课程内容
- php 常用函数集合(持续更新中...)
php 常用函数集合 在php的开发中,巧妙的运用php自带的一些函数,会起到事半功倍的效果,在此,主要记录一些常用的函数 1.time(),microtime()函数 time():获取当前时间戳 ...
- python-函数的对象、函数嵌套、名称空间和作用域
目录 函数的对象 函数对象的四大功能 引用 当做参数传给一个函数 可以当做函数的返回值 可以当做容器类型的元素 函数的嵌套 函数的嵌套定义 函数的嵌套调用 名称空间与作用域 名称空间 内置名称空间 全 ...
- python3中urllib库的request模块详解
刚刚接触爬虫,基础的东西得时时回顾才行,这么全面的帖子无论如何也得厚着脸皮转过来啊! 原帖地址:https://www.2cto.com/kf/201801/714859.html 什么是 Urlli ...
- 蓝桥--2n皇后问题(递归)--搬运+整理+注释
N皇后问题: #include <iostream> #include <cmath> using namespace std; int N; ];//用来存放算好的皇后位置. ...
- Linux学习-函式库管理
动态与静态函式库 首先我们要知道的是,函式库的类型有哪些?依据函式库被使用的类型而分为两大类,分别是静态 (Static) 与动态 (Dynamic) 函式库两类. 静态函式库的特色: 扩展名:(扩展 ...
- poj2195 bfs+最小权匹配
题意:给个矩阵,矩阵里有一些人和房子(人数和房子数相等),一个人只进一个房子(可以路过房子而不进),每走一步花费1美金,求所有人都进入房子的最小花费,这是典型的二分图带权匹配问题. 这题就是建图有点麻 ...
- python基础学习笔记——类空间问题以及类之间的关系
一. 类的空间问题 1.1 何处可以添加对象属性 class A: def __init__(self,name): self.name = name def func(self,sex): self ...
- day06 面向对象编程
面向对象: 面向对象: 世界万物,皆可分类 世界万物,皆为对象 只要是对象,就肯定属于某种品类 只要是对象,就肯定有属性 特性: 多态: 一个统一的接口,多种实现 例如: 一个 ...
- loj2001 「SDOI2017」树点涂色
there #include <iostream> #include <cstdio> using namespace std; int n, m, dfn[100005], ...