Skip to content

Commit

Permalink
Add ViewPager
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanguozheng committed Jan 28, 2016
1 parent 7164b2b commit 2d1741b
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 9 deletions.
16 changes: 8 additions & 8 deletions MainScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import React, {
} from 'react-native';

import Header from './Header';
import HomePage from './home/HomePage';
import TabNavigator from 'react-native-tab-navigator';

const HOME = 'home';
Expand Down Expand Up @@ -49,7 +50,7 @@ export default class MainScreen extends Component {
);
}

_createChildView(tag) {
static _createChildView(tag) {
return (
<View style={{flex:1,backgroundColor:'#00baff',alignItems:'center',justifyContent:'center'}}>
<Text style={{fontSize:22}}>{tag}</Text>
Expand All @@ -62,11 +63,11 @@ export default class MainScreen extends Component {
<View style={{flex: 1}}>
<Header />
<TabNavigator hidesTabTouch={true} tabBarStyle={styles.tab}>
{this._renderTabItem(HOME_NORMAL, HOME_FOCUS, HOME, this._createChildView(HOME))}
{this._renderTabItem(CATEGORY_NORMAL, CATEGORY_FOCUS, CATEGORY, this._createChildView(CATEGORY))}
{this._renderTabItem(FAXIAN_NORMAL, FAXIAN_FOCUS, FAXIAN, this._createChildView(FAXIAN))}
{this._renderTabItem(CART_NORMAL, CART_FOCUS, CART, this._createChildView(CART))}
{this._renderTabItem(PERSONAL_NORMAL, PERSONAL_FOCUS, PERSONAL, this._createChildView(PERSONAL))}
{this._renderTabItem(HOME_NORMAL, HOME_FOCUS, HOME, <HomePage/>)}
{this._renderTabItem(CATEGORY_NORMAL, CATEGORY_FOCUS, CATEGORY, MainScreen._createChildView(CATEGORY))}
{this._renderTabItem(FAXIAN_NORMAL, FAXIAN_FOCUS, FAXIAN, MainScreen._createChildView(FAXIAN))}
{this._renderTabItem(CART_NORMAL, CART_FOCUS, CART, MainScreen._createChildView(CART))}
{this._renderTabItem(PERSONAL_NORMAL, PERSONAL_FOCUS, PERSONAL, MainScreen._createChildView(PERSONAL))}
</TabNavigator>
</View >
);
Expand All @@ -84,6 +85,5 @@ const styles = StyleSheet.create({
height: 35,
resizeMode: 'stretch',
marginTop: 12.5
},

}
});
66 changes: 66 additions & 0 deletions home/HomePage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Created by yuanguozheng on 16/1/22.
*/
'use strict';
import React, {
Component,
View,
Text,
Image,
StyleSheet,
ScrollView,
RefreshControl
} from 'react-native';
import MenuButton from './MenuButton';
import ViewPager from 'react-native-viewpager';

const BANNER_IMGS = [
require('../images/banner/1.jpg'),
require('../images/banner/2.jpg'),
require('../images/banner/3.jpg'),
require('../images/banner/4.jpg')
];

export default class HomePage extends Component {

constructor(props) {
super(props);
// 用于构建DataSource对象
var dataSource = new ViewPager.DataSource({
pageHasChanged: (p1, p2) => p1 !== p2,
});
// 实际的DataSources存放在state中
this.state = {
dataSource: dataSource.cloneWithPages(BANNER_IMGS)
}
}

_renderPage(data, pageID) {
return (
<Image
source={data}
style={styles.page}/>
);
}

render() {
return (
<View>
<ViewPager
style={{height:130}}
dataSource={this.state.dataSource}
renderPage={this._renderPage}
isLoop={true}
autoPlay={true}/>
</View>
)
}
}

const styles = StyleSheet.create({
page: {
flex: 1,
height: 130,
resizeMode: 'stretch'
},
});
Binary file added images/banner/1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/banner/2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/banner/3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/banner/4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"dependencies": {
"i": "^0.3.4",
"react-native": "^0.18.0",
"react-native-tab-navigator": "^0.2.15"
"react-native-tab-navigator": "^0.2.15",
"react-native-viewpager": "^0.2.1"
}
}

26 comments on commit 2d1741b

@rogueRabbit
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你好,轮播图代码会报错啊呀,会报can not find variable: animated

@yuanguozheng
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rogueRabbit ViewPager库和RN的版本对吗?

@ahzar
Copy link

@ahzar ahzar commented on 2d1741b Jul 26, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

循环轮播时最后一个图片条转到第一个图片时 有白屏一晃而过 晃眼 怎么解决?

@yuanguozheng
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ahzar 取消自动播放

@jinleli
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image
我的出来,图片不是完整的一张,有一小半是下张图片的,怎么回事啊?

@yuanguozheng
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jinleli 高度、宽度,或者Image的缩放类型不对

@jinleli
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我写的和您的代码一样,高度130,宽度没写,然后缩放类型stretch,我用的模拟器不是iphone跟这个有没有关系呢?

@yuanguozheng
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jinleli 有可能,Android上屏幕不太一样可能会有变形,你计算一下然后改改就行

@jinleli
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

非常感谢,通过您的教程学到了很多

@yuanguozheng
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jinleli 不客气,我也是新手,希望能共同学习

@RuntaiChen
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么我的滚动原点位置不对呢

@yuanguozheng
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RuntaiChen 应该是布局有点问题

@lixiaobin0303
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

会报错 null is not an object(evaluating 'this.state.dataSource')

@Duovic
Copy link

@Duovic Duovic commented on 2d1741b May 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我的出现了设置ViewPage的高度为130,但是出现的效果像是有两个ViewPage
c01fae9e222a66c1706d26ed92836398
的高度

@yuanguozheng
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lixiaobin0303 检查一下前后写的数据源是否正确

@yuanguozheng
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wdhonor 看一下里面的Item的高度

@Duovic
Copy link

@Duovic Duovic commented on 2d1741b May 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yuanguozheng 这个ViewPage里面没有Item啊 只是有被填充的图片资源 这个我也设置了高度是130

@yuanguozheng
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wdhonor 看看外层容器的高度,还有ViewPager是否是Flex:1

@Duovic
Copy link

@Duovic Duovic commented on 2d1741b May 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yuanguozheng 问题已经解决了 使用flex来进行解决的 就像Android中的weight进行设置权重比例一样

@yuanguozheng
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wdhonor 解决了就好

@Lcn29
Copy link

@Lcn29 Lcn29 commented on 2d1741b Jul 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yuanguozheng 问一下几个问题
(1)ViewPager 被其他控件包住的时候就无法显示了
(2)如果要保留自动滑动,怎么解决从最后一张跳到第一张闪了一下的问题呢、
(3)设置了高度和flex ,ViewPager的导航点自己在页面的低端了,没法移到上面,,,
default

最后附上代码
import React, { Component } from 'react';
import {
StyleSheet,
Image,
} from 'react-native';

import ViewPager from 'react-native-viewpager';

const BANNER_IMGS = [
require('../image/banner/1.jpg'),
require('../image/banner/2.jpg'),
require('../image/banner/3.jpg'),
require('../image/banner/4.jpg')
];

export default class HomePage extends Component{

constructor(props) {
    super(props);
    // 用于构建DataSource对象
        var dataSource = new ViewPager.DataSource({
            pageHasChanged: (p1, p2) => p1 !== p2,
        });
    // 实际的DataSources存放在state中
        this.state = {
            dataSource: dataSource.cloneWithPages(BANNER_IMGS)
    }
}

_renderPage(data, pageID) {
    return (
            <Image
                source={data}
                style={styles.page}/>
    );
}

render() {

   /*
    像这样 ViewPager 被一个 View 包起来 viewPager 无法显示
   return (
        <View>
            <ViewPager
                style={{height:130}}
                dataSource={this.state.dataSource}
                renderPage={this._renderPage}
                isLoop={true}
                autoPlay={true}/>
            </View>
    )*/


    return (
            <ViewPager
                style={{height:130}}
                dataSource={this.state.dataSource}
                renderPage={this._renderPage}
                isLoop={true}
                autoPlay={true}/>
        )
    }

};

const styles = StyleSheet.create({
page: {
flex: 1,
height: 130,
resizeMode: 'stretch'
},
});

"dependencies": {
"react": "16.0.0-alpha.12",
"react-native": "0.45.1",
"react-native-tab-navigator": "^0.3.3",
"react-native-viewpager": "^0.2.13"
},

@yuanguozheng
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最好给View一个大小 @Lcn29

@Lcn29
Copy link

@Lcn29 Lcn29 commented on 2d1741b Jul 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yuanguozhen
default
g 给ViewPager 外层的View 设置一个和ViewPager一样的高度,可以解决1,3问题

如果需要保留自动轮播的效果,怎么解决最后一张跳到第一张闪一下的问题呢,还有怎么设置他轮播的时间,

@yuanguozheng
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Lcn29 闪的问题应该是这个第三方控件的bug,你可以看看这个ViewPager控件的最新版本是否解决了,如果没有解决,看下源码自己改下好了,其实也不难

@liulee1514
Copy link

@liulee1514 liulee1514 commented on 2d1741b Oct 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我写上 报这个错
undefined is not an object(evaluating 'React.PropTypes.fun ')

@tangyongjing
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@liulee1514 你的RN版本是不是0.5?

Please sign in to comment.