已开启
[问题咨询]: onRefresh回调失败问题 #6
zhoukc42创建于  3月13日
zhoukc42
zhoukc42
3月13日 创建

感谢提出问题,我们将安排人答复!

问题描述

  1. npm i react-native-mjrefresh
  2. 操作,下拉内层ScrollView列表置顶部后继续下拉,触发刷新,查看列表颜色是否改变。
  3. demo:
import React from 'react';
import { SafeAreaView, View, Text, StyleSheet, ScrollView } from 'react-native';
import MJRefresh from 'react-native-mjrefresh';

const PullRefreshDemo = () => {
    const [isRefreshing, setIsRefreshing] = React.useState(false);

    const handleRefresh = () => {
        console.log('onRefresh');
        setIsRefreshing(true);
        setTimeout(() => {
            setIsRefreshing(false);
        }, 2000);
    };

    return (
        <SafeAreaView style={styles.container} >
            <View style={styles.header}>
                <Text style={styles.headerText}>Pull to Refresh Demo</Text>
            </View>
            <ScrollView>
                <View style={styles.testContainer}>
                    <MJRefresh
                        refreshing={isRefreshing}
                        onRefresh={handleRefresh}
                        indicator={{
                            finish: (
                                <View style={styles.customIndicator}>
                                    <Text style={styles.customIndicatorText}>刷新完成</Text>
                                </View>
                            )
                        }}
                    >
                        <ScrollView>
                            {[1, 2, 3, 4, 5].map((item) => (
                                <View key={item} style={[styles.box, { backgroundColor: getRandomColor() }]}>
                                    <Text style={styles.boxText}>{item}</Text>
                                </View>
                            ))}
                            <Text style={{ padding: 10, textAlign: 'center' }}>
                                下拉刷新
                            </Text>
                        </ScrollView>
                    </MJRefresh>
                </View>
            </ScrollView>
        </SafeAreaView>
    );
};

const getRandomColor = () => {
    const colors = ['#f44336', '#2196F3', '#4CAF50', '#FFC107', '#9C27B0', '#FF9800'];
    return colors[Math.floor(Math.random() * colors.length)];
};

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#fff',
    },
    header: {
        padding: 8,
        backgroundColor: '#f5f5f5',
        borderBottomWidth: 1,
        borderBottomColor: '#e0e0e0',
    },
    headerText: {
        fontSize: 16,
        fontWeight: 'bold',
        textAlign: 'center',
    },
    testContainer: {
        padding: 8,
        alignItems: 'center',
        height: 400,
        borderWidth: 1,
        borderColor: '#ddd',
        marginBottom: 8,
    },
    customIndicator: {
        padding: 10,
        backgroundColor: '#e8f5e9',
        borderRadius: 4,
    },
    customIndicatorText: {
        color: '#388e3c',
        fontSize: 14,
    },
    box: {
        width: 100,
        height: 100,
        margin: 10,
        justifyContent: 'center',
        alignItems: 'center',
    },
    boxText: {
        color: 'white',
        fontSize: 24,
        fontWeight: 'bold',
    },
});

export default PullRefreshDemo;
likedislike
zhoukc42zhoukc42
3月13日 添加了label:question
zhoukc42zhoukc42
3月13日 修改了issue 的描述
dingchengjie
dingchengjie成员
5月20日 评论:

你好开发者。你的这个下载命令是下载不到鸿蒙的版本的,正确的下载方式应该是 npm i @react-native-ohos/react-native-mjrefresh
使用该命令来进行验证,如有问题可以再进行沟通

likedislike
Sett
Sett
7月1日 评论:

本Issue主要用于开发协作,欢迎在此反馈问题或提出建议

likedislike