React Native之九宫格布局

九宫格的布局,其实大家都耳熟能详了,那么如何用react native来开发九宫格布局呢?

首先,贴上UI需求图:




对于以上的布局,虽然目前图片还是很少,还是希望大家可以用最优雅的方式开发代码,简洁好看,复用性也高。

开发的思想:

将图片的所有信息(包括URL地址,图片文字,是否是大图还是小图等等)都整合在一个对象里面,可以在当前模块中数据传递,也可以支持从上级传递数据。

以下就是本次九宫格开发的核心代码块:

generateDetailItems(){
        var arr=[];
        var badge = this.state.imageList;
        if(badge){
            for (var i = 0;i<badge.length;i++){
                if(badge[i].bigOr==0){
                    arr.push(
                        <View style={styles.viewStyle}>
                            <Image source={badge[i].imgUrl} style={styles.faceWidth} resizeMode={Image.resizeMode.contain} />
                            <Text style={styles.textStyle}>{badge[i].textDesc}</Text>
                        </View>
                    );
                }else{
                    arr.push(
                        <View style={styles.outerViewStyle}>
                            <View style={styles.viewStyle}>
                                <Image source={badge[badge.length-1].imgUrl} style={styles.bigImage} resizeMode={Image.resizeMode.contain} />
                                <Text style={styles.textStyle}>{badge[badge.length-1].textDesc}</Text>
                            </View>
                        </View>
                    );
                }

            }
            return arr;
        }

}


初始化imageList如下:

constructor(props) {
        super(props);
        this.props = props;
        this.state = {
            imageList:[
                {imgUrl: require('./image/far.png'), textDesc:'太远',bigOr:0},
                {imgUrl: require('./image/close.png'), textDesc:'太近',bigOr:0},
                {imgUrl: require('./image/dark.png'), textDesc:'太暗',bigOr:0},
                {imgUrl: require('./image/badBg.png'), textDesc:'背景复杂凌乱',bigOr:0},
                {imgUrl: require('./image/dim.png'), textDesc:'对焦模糊',bigOr:0},
                {imgUrl: require('./image/reflect.png'), textDesc:'反光',bigOr:0},
                {imgUrl: require('./image/right.png'), textDesc:'正确姿势',bigOr:1},
            ] || this.props.imageList,
        }
}


具体详情和使用,请去我的Github下载源码:

https://github.com/spicyboiledfish/GridView



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值