react-native 之九宫格列表

先上效果图:

这里写图片描述

就是类似这种的列表. 在 iOS 中有 UICollectionView 可以直接写这种类型的列表, 但在React-native 中直接就可以用 FlatList 完成, 也就是说这种类型的列表和普通的列表没什么区别, 只是在布局的时候要些许的差异. Let’s do it!

1.渲染一个列表


 render() {
        return (
            <View style={styles.container}>
                <FlatList
                    data={this.state.data}
                    renderItem={this.renderItem}
                    keyExtractor={(item, index) => index}
                    contentContainerStyle={styles.list_container}
                />
            </View>
        )
    };

2. 给列表一个样式


list_container: {
        // 主轴方向
        flexDirection:'row',
        justifyContent: 'space-between',
        // 一行显示不下,换一行
        flexWrap:'wrap',
        // 侧轴方向
        alignItems:'center', // 必须设置,否则换行不起作用
        paddingHorizontal: 20,
    },

这里注意: flexDirection flexWrap alignItems 这三个属性是必须设置的.

其实写九宫格列表最重要的就是这一块.

完整代码:


const {width, height} = Dimensions.get('window')
const cols = 2;
const vMargin = 20;
const cellWH = (width-2*vMargin-15)/cols;
const hMargin = 25;

export default class AllRecommend extends Component {


    constructor(props) {
        super(props)
        this.state = {
            data: [],
        }
    };

    componentDidMount() {
        this.fetchData();
    }

    render() {
        return (
            <View style={styles.container}>
                <FlatList
                    data={this.state.data}
                    renderItem={this.renderItem}
                    keyExtractor={(item, index) => index}
                    contentContainerStyle={styles.list_container}
                />
            </View>
        )
    };

    renderItem({item, index})  {

        return (
            <TouchableOpacity activeOpacity={0.5}>
                <View style={styles.item}>
                    <Image source={{uri: item['image']}} style={{width: cellWH,height:cellWH, borderRadius: 5}}/>
                    <Text style={{marginTop: 5, textAlign: 'center'}} numberOfLines={1}>{item.title}</Text>
                </View>
            </TouchableOpacity>
        )
    }

    fetchData() {
        NetRequest.get('https://api.douban.com/v2/music/search?q=李志',null,null).then((response) => {
            this.setState({
                data: response['musics']
            })
        }, (error) => {

        })
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: color.white,
        paddingVertical: 15,
    },
    list_container: {
        // 主轴方向
        flexDirection:'row',
        justifyContent: 'space-between',
        // 一行显示不下,换一行
        flexWrap:'wrap',
        // 侧轴方向
        alignItems:'center', // 必须设置,否则换行不起作用
        paddingHorizontal: 20,
    },
    item: {
        width:cellWH,
        marginTop:hMargin,
        alignItems: 'center',
    }
})

每一个 item 的大小尺寸可以根据自己的实际需求去设置.

enjoy it!

小白开发交流群: 860196537

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值