【React Native】自定义列表下拉刷新

        关于React Native List的下拉刷新,虽然官方出了一个控件RefreshControl,但可定制性太差,基本上样式固定了。为了满足项目需求,我在GitHub上搜到了这个组件,使用起来非常不错。

        同时支持android和ios,并且拥有相同的Api,可以自定义下拉刷新样式;它里面已经实现了View,Scrollview,Listview和Flatlist的下拉刷新,可以支持绝大多数的React Native中的组件实现下拉刷新功能。

安装

        yarn add react-native-rk-pull-to-refresh

如果link失败就需要手动link

        react-native link react-native-rk-pull-to-refresh

使用提醒

        它内部包含了PullView、PullScrollView,、PullListView和PullFlatList,如果你想使用PullFlatList的话,那么你要保持你的React Native版本在0.43及以上。并且你要添加如下的代码到FlatList(node_modules/react-native/Libraries/Lists/FlatList.js)中:

...
getScrollMetrics = () => {
    return this._listRef.getScrollMetrics()
}

...

        同时在VirtualizedList(node_modules/react-native/Libraries/Lists/VirtualizedList.js)中添加如下代码:

...
 getScrollMetrics = () => {
    return this._scrollMetrics
 }

 ...

属性

PorpTypeOptionalDefaultDescription
refreshableboolyestrue是否需要下拉刷新功能
isContentScrollboolyesfalse在下拉的时候内容时候要一起跟着滚动
onPullReleasefuncyes刷新的回调
topIndicatorRenderfuncyes下拉刷新头部的样式,当它为空的时候就使用默认的
topIndicatorHeightnumberyes下拉刷新头部的高度,当topIndicatorRender不为空的时候要设置正确的topIndicatorHeight
onPullStateChangeHeightfuncyes下拉时候的回调,主要是刷新的状态的下拉的距离
onPushingfuncyes下拉时候的回调,告诉外界此时是否在下拉刷新

        startRefresh():手动调用下拉刷新功能 

        finishRefresh():结束下拉刷新

使用样例

PullListView默认样式

import React, {PureComponent} from 'react';
import {ListView, View, Text, Dimensions} from 'react-native';
import {PullListView} from 'react-native-rk-pull-to-refresh'
const width = Dimensions.get('window').width
export default class PullListViewDemo extends PureComponent {
    constructor(props) {
        super(props);
        this.dataSource =
            new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2}).cloneWithRows(this.getDataSource())
    }

    getDataSource = () => {
        let array = new Array();
        for (let i = 0; i < 50; i++) {
            array.push(`ListViewItem:${i + 1}`);
        }
        return array;
    }

    render() {
        return (
            <PullListView
                ref={(c) => this.pull = c}
                isContentScroll={true}
                style={{flex: 1, width: width}}
                onPushing={this.props.onPushing}
                onPullRelease={this._onPullRelease}
                dataSource={this.dataSource}
                renderRow={this._renderRow}/>
        )
    }

    _onPullRelease = () => {
        setTimeout(() => {
            this.pull && this.pull.finishRefresh()
        }, 2000)
    }

    _renderRow = (rowData) => {
        return (
            <View style={{width: width, height: 50, justifyContent: 'center', alignItems: 'center'}}>
                <Text>{rowData}</Text>
            </View>);
    }

    componentDidMount() {
        this.pull && this.pull.startRefresh()
    }

}

PullView自定义样式

import React, {PureComponent} from 'react';

import {View, Text, Dimensions, StyleSheet, ActivityIndicator} from 'react-native';

import {PullView} from 'react-native-rk-pull-to-refresh'

const width = Dimensions.get('window').width
const topIndicatorHeight = 50

export default class PullViewDemo extends PureComponent {
    render() {
        return (
            <PullView
                ref={(c) => this.pull = c}
                style={{flex: 1, width: width}}
                topIndicatorRender={this.topIndicatorRender}
                topIndicatorHeight={topIndicatorHeight}
                onPullStateChangeHeight={this.onPullStateChangeHeight}
                onPushing={this.props.onPushing}
                onPullRelease={this._onPullRelease}>
                <Text style={{flex: 1, width: width, paddingTop: 200, textAlign: 'center'}}>这是内容</Text>
            </PullView>
        )
    }

    onPullStateChangeHeight = (pullState, moveHeight) => {
        if (pullState == 'pulling') {
            this.txtPulling && this.txtPulling.setNativeProps({style: styles.show});
            this.txtPullok && this.txtPullok.setNativeProps({style: styles.hide});
            this.txtPullrelease && this.txtPullrelease.setNativeProps({style: styles.hide});
        } else if (pullState == 'pullok') {
            this.txtPulling && this.txtPulling.setNativeProps({style: styles.hide});
            this.txtPullok && this.txtPullok.setNativeProps({style: styles.show});
            this.txtPullrelease && this.txtPullrelease.setNativeProps({style: styles.hide});
        } else if (pullState == 'pullrelease') {
            this.txtPulling && this.txtPulling.setNativeProps({style: styles.hide});
            this.txtPullok && this.txtPullok.setNativeProps({style: styles.hide});
            this.txtPullrelease && this.txtPullrelease.setNativeProps({style: styles.show});
        }
    }

    topIndicatorRender = () => {
        return (
            <View style={{flexDirection: 'row', justifyContent: 'center', alignItems: 'center', height: topIndicatorHeight}}>
                <ActivityIndicator size="small" color="gray" style={{marginRight: 5}}/>
                <Text ref={(c) => this.txtPulling = c} style={styles.hide}>pulling...</Text>
                <Text ref={(c) => this.txtPullok = c} style={styles.hide}>pullok...</Text>
                <Text ref={(c) => this.txtPullrelease = c} style={styles.hide}>pullrelease...</Text>
            </View>
        );
    }

    _onPullRelease = () => {
        setTimeout(() => {
            this.pull && this.pull.finishRefresh()
        }, 2000)
    }

    componentDidMount() {
        this.pull && this.pull.startRefresh()
    }
}

const styles = StyleSheet.create({
    hide: {
        position: 'absolute',
        left: 10000,
        backgroundColor: 'transparent'
    },
    show: {
        position: 'relative',
        left: 0,
        backgroundColor: 'transparent'
    }

});

参考链接:GitHub - hzl123456/react-native-rk-pull-to-refresh: a pull to refresh component for react-native, same api on both android and ios

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
React Native中的自定义控件使用与React相同的组件方式进行实现。以下是自定义控件的基本步骤: 1. 创建一个自定义控件组件:在你的项目中创建一个新的组件,该组件包含你自定义的UI元素。 2. 定义控件属性:你可以在组件的props中定义一些属性,这些属性可以用来设置控件的外观和行为。 3. 实现控件逻辑:在组件的render方法中实现控件的逻辑,包括控件的事件处理、状态管理等。 4. 导出控件:将你的自定义控件组件导出,以便其他组件可以使用它。 以下是一个简单的例子,演示如何创建自定义控件: ``` import React, { Component } from 'react'; import { Text, TouchableOpacity } from 'react-native'; class CustomButton extends Component { constructor(props) { super(props); this.state = { pressed: false, }; } handlePress = () => { this.setState({ pressed: true }); }; handleRelease = () => { this.setState({ pressed: false }); }; render() { const { title, disabled } = this.props; const { pressed } = this.state; const buttonStyle = [ styles.button, disabled && styles.disabled, pressed && styles.pressed, ]; return ( <TouchableOpacity style={buttonStyle} onPress={this.handlePress} onPressOut={this.handleRelease} activeOpacity={0.6} disabled={disabled} > <Text style={styles.text}>{title}</Text> </TouchableOpacity> ); } } const styles = { button: { backgroundColor: '#007aff', paddingVertical: 10, paddingHorizontal: 20, borderRadius: 5, }, disabled: { opacity: 0.5, }, pressed: { backgroundColor: '#0051a8', }, text: { color: '#fff', fontSize: 16, fontWeight: 'bold', textAlign: 'center', }, }; export default CustomButton; ``` 在上面的例子中,我们创建了一个CustomButton组件,它包含一个TouchableOpacity,以及一些属性和状态来控制按钮的外观和行为。在render方法中,我们使用了一些简单的样式来设置按钮的外观,以及一些事件处理来处理按钮的行为。最后,我们将CustomButton组件导出,以便其他组件可以使用它。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZhangKui_c

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值