设为首页 加入收藏

TOP

React Native 开发豆瓣评分(七)首页组件开发(二)
2019-09-17 18:03:33 】 浏览:36
Tags:React Native 开发 评分 首页 组件
t; <View style={[styles.stars, styles.active, { width: activeStarsWidth }]}> {activeStars.map(item => item)} </View> <View style={styles.stars}> {defaultStars.map(item => item)} </View> </View> ) } } const styles = StyleSheet.create({ rates: { flexDirection: 'row', position: 'relative' }, stars: { flexDirection: 'row', alignItems: 'center', overflow: 'hidden', flexGrow: 0 }, active: { position: 'absolute', zIndex: 200, left: 0, top: 0 } });

开发电影海报组件

海报组件开发需要注意的是:

  1. 点击电影海报,跳转详情页面,跳转逻辑都是一样的,所以可以不用自定义事件的方式跳转,直接在组件里面调用 this.props.navigation.push 进行跳转。页面在 router 里注册后可以直接使用 this.props.navigation.push,但是组件不行。在组件中,想要使用 navigation 进行跳转,要么是使用自定义属性,将 navigation 传入组件,要么使用 react-navigation 提供的 withNavigation翻翻,withNavigation(component) 返回一个 render 函数,默认将 navigation 作出自定义属性传入组件。

  2. 有些海报图片背景纯白,和页面背景融合了,看不到边界,所以需要给他设置 border,由于 Image 组件不能设置 border,所以这里需要使用 ImageBackground 组件。

  3. title 只能为一行,产出部分省略,需要加一个 numberOfLines={1} 的属性。

import React, { Component } from 'react';
import { Text, View, StyleSheet, ImageBackground, TouchableWithoutFeedback } from 'react-native';
import PropTypes from 'prop-types';
import { withNavigation } from 'react-navigation';
import { px } from '../utils/device';
import Rate from './rate';

class MoviesItem extends Component {
    constructor(props) {
        super(props);
    }
    static propTypes = {
        data: PropTypes.object
    }
    render() {
        const { data, navigation } = this.props;
        const { id, title, cover, rating, null_rating_reason } = data;
        return (
            <TouchableWithoutFeedback onPress={() => navigation.push('Detail', { id })}>
                <View style={styles.page}>
                    <ImageBackground source={{ uri: cover.url }} style={styles.img}></ImageBackground>
                    <Text style={styles.title} numberOfLines={1}>{title}</Text>
                    {rating ? (
                        <View style={styles.rate}>
                            <Rate value={rating.value / 2} size={px(20)} margin={px(4)} />
                            <Text style={styles.rateText}>{rating.value.toFixed(1)}</Text>
                        </View>
                    ) : (
                        <Text style={styles.rate}>{null_rating_reason}</Text>
                    )}
                </View>
            </TouchableWithoutFeedback>
        )
    }
}

export default withNavigation(MoviesItem);

const styles = StyleSheet.create({
    page: {
        width: px(160)
    },
    img: {
        width: px(160),
        height: px(224),
        overflow: 'hidden',
        borderRadius: px(8),
        borderWidth: 1,
        borderStyle: 'solid',
        borderColor: '#f8f8f8'
    },
    title: {
        fontSize: px(28),
        fontWeight: '600',
        color: '#333',
        marginTop: px(12),
        lineHeight: px(40)
    },
    rate: {
        flexDirection: 'row',
        alignItems: 'center'
    },
    rateText: {
        fontSize: px(24),
        color: '#999',
        marginLeft: px(6)
    }
});

使用

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇vue微信分享链接添加动态参数 下一篇前端笔记之React(三)使用动态样..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目