HarmonyOS 鸿蒙 ArkTs(5.0.1 13)实现Scroll下拉到顶刷新/上拉触底加载,Scroll滚动到顶部

server/2025/1/16 21:25:35/

HarmonyOS 鸿蒙 ArkTs(5.0.1 13)实现Scroll下拉到顶刷新/上拉触底加载

效果展示

在这里插入图片描述
在这里插入图片描述

使用方法

import LoadingText from "../components/LoadingText"
import PageToRefresh from "../components/PageToRefresh"
import FooterBar from "../components/FooterBar"
class PageToRefreshController {onScrollTop = () => {}
}
@Entry
@Component
struct Index {//滚动回顶部方法startprivate PageToRefreshRef = new PageToRefreshController();onScrollTop(){this.PageToRefreshRef.onScrollTop()}//滚动回顶部方法endonSearch() {// 刷新数据this.loading = true;setTimeout(()=>{this.loading = false;this.simpleList = [1, 2, 3, 4, 5,6,7,8,9,10,11,12,13,14,15]},1000)}onReachBottom(){// 触底加载if(!this.finish){this.loading = true;setTimeout(()=>{this.loading = false;this.finish = true;this.simpleList = this.simpleList.concat([16,17,15,19,20,21,22,23,24,25,26,27,28,29,30])},1000)}}// 判断是否需要显示滚动到顶部的按钮(scroll滚动的位置)@State scrollY:number = 0;// 数据@State simpleList: Array<number> = [1, 2, 3, 4, 5,6,7,8,9,10,11,12,13,14,15];@State loading:boolean = false;@State finish:boolean = false;build() {Stack({ }) {Flex({ direction: FlexDirection.Column }) {// 顶部Column(){// 自定义顶部的组件Text('顶部标题').fontColor(0xffffff).fontSize(14)}.width('100%').padding(10).flexShrink(0).backgroundColor(0xFC5531).expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP])// 内容Stack(){//调用组件PageToRefresh({controller: this.PageToRefreshRef, scrollY: this.scrollY, refreshPull: () => {this.onSearch()}, reachBottom: () =>{this.onReachBottom()}}){Column(){Row(){// 自定义内容Text('这里可以定义滚动后固定在顶部的内容').fontColor(0xffffff).fontSize(14)}.width('100%').height(50).backgroundColor(0x232020).justifyContent(FlexAlign.Center)List() {ForEach(this.simpleList, (item: number, index: number) => {ListItem(){Row(){Text(item.toString()).fontColor(0xffffff).fontSize(24)}.width('100%').height(80).backgroundColor(0x1BA035).margin({bottom: 10}).justifyContent(FlexAlign.Center)}}, (item: number) => item.toString())ListItem(){LoadingText({loading: this.loading, finish: this.finish, onPullData: () => {//点击直接加载数据this.onReachBottom()}})}}}.alignItems(HorizontalAlign.Start).justifyContent(FlexAlign.Start).constraintSize({minHeight: '100%'})}if(this.scrollY >= 50){Row(){Text('我已经固定在顶部啦').fontColor(0xffffff).fontSize(14)}.position({x:0,y:0}).width('100%').height(50).backgroundColor(0x232020).justifyContent(FlexAlign.Center)}}.flexGrow(1).flexShrink(1)//你自定义的底部tabbar组件(仅供示例)FooterBar({scrollTop: this.scrollY >= 500, //判断是否显示滚动到顶部onGoTop: () => {//点击滚动到顶部的方法this.onScrollTop();}}).flexShrink(0)}.width('100%').height('100%')}}
}

新建PageToRefresh 组件

import LoadingText from "./LoadingText"
class PageToRefreshController {onScrollTop = () => {}
}
@Component
export default struct PageToRefresh {refreshPull?: () => voidreachBottom?: () => voidscroller: Scroller = new Scroller()private controller: PageToRefreshController = new PageToRefreshController();aboutToAppear() {if (this.controller) {this.controller.onScrollTop = this.onScrollTop;}}private onScrollTop = () => {//滚动回顶部this.scroller.scrollTo({ xOffset: 0, yOffset: 0, animation: { duration: 1000, curve: Curve.Ease } })}@Link scrollY: numberprivate currentOffsetY: number = 0;@State refreshStatus: boolean = false;@State refreshText: string = '正在刷新';@State pullUpText: string = '正在加载';@State isRefreshing: boolean = false;@State isCanLoadMore: boolean = false;@State ArrData: string[] = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];@State newArr: string [] = ['10', '11']putDownPullUpRefresh(event?: TouchEvent): void {if (event === undefined) {return;}switch (event.type) {case TouchType.Down:this.currentOffsetY = event.touches[0].y;break;case TouchType.Move:let isDownPull = event.touches[0].y - this.currentOffsetY > 50;if (isDownPull && this.isCanLoadMore === false && this.scrollY <= 20) {this.refreshStatus = true;}if(this.scroller.isAtEnd()){console.log('滚动到底部')this.isCanLoadMore = true;}break;case TouchType.Cancel:break;case TouchType.Up:if (this.refreshStatus) {console.log('下拉刷新')this.refreshStatus = false;if(this.refreshPull) this.refreshPull();}if (this.isCanLoadMore) {console.log('上拉加载')this.isCanLoadMore = false;if(this.reachBottom) this.reachBottom();}break;default:break;}}@BuilderputDown() {Row() {LoadingText({loading: true, finish: false, loadingText: this.refreshText})}.justifyContent(FlexAlign.Center).width('100%')}@BuilderPullUp() {Row() {LoadingText({loading: true, finish: false, loadingText: this.pullUpText})}.justifyContent(FlexAlign.Center).width('94%').height('5%')}@Builderslot() {Stack(){}.flexShrink(0)};@BuilderParam component: () => void = this.slot;build() {Column() {Scroll(this.scroller) {Column() {if (this.refreshStatus) {this.putDown()}this.component();}}.width('100%').onWillScroll(() => {this.scrollY = this.scroller.currentOffset().yOffset;}).onTouch((event?: TouchEvent) => {this.putDownPullUpRefresh(event);})}.width('100%').height('100%').backgroundColor(0xf4f4f4)}
}

加载文字

@Component
export default struct LoadingText {onPullData?: () => void@Prop loadingText: string = "加载中...";@Prop loading: boolean = false;@Prop finishText: string = "- 我是有底线的 -";@Prop finish: boolean = false;build() {Row() {if(this.loading){LoadingProgress().width(20).height(20).margin({ right: 10 }).color(0x999999)Text(this.loadingText).fontSize(12).fontColor(0x999999).margin({left: 4})}else if(this.finish){Text(this.finishText).fontSize(12).fontColor(0x999999)}else{Text("轻轻上拉加载更多").fontSize(12).fontColor(0x999999).onClick(()=>{if (this.onPullData) {this.onPullData()}})}}.alignItems(VerticalAlign.Center).justifyContent(FlexAlign.Center).width('100%').height(24)}
}

http://www.ppmy.cn/server/158920.html

相关文章

改进萤火虫算法之八:量子萤火虫算法(Quantum-behaved Firfly Algorithm,QFA)

量子萤火虫算法(Quantum-behaved Firfly Algorithm,QFA)是对萤火虫算法的一种改进,旨在提升萤火虫个体的搜索能力。以下是对量子萤火虫算法的详细介绍: 一、萤火虫算法基础 萤火虫算法(Firefly Algorithm,FA)是一种基于群体智能的优化算法,由剑桥大学的Xin-She Yang在…

Soildworks的学习【2025/1/12】

右键空白处&#xff0c;点击选项卡&#xff0c;即可看到所有已调用的选项卡&#xff1a; 点击机械小齿轮选项卡&#xff0c;选择文档属性&#xff0c;选择GB国标&#xff1a; 之后点击单位&#xff0c;选择MMGS毫米单位&#xff1a; 窗口右下角有MMGS&#xff0c;这里也可以选择…

Flask-SQLAlchemy 基础用法

1 安装 Flask-SQLAlchemy 首先&#xff0c;确保您已安装 Flask 和 Flask-SQLAlchemy&#xff1a; pip install Flask Flask-SQLAlchemy2 创建 Flask 应用 创建一个基本的 Flask 应用结构&#xff1a; from flask import Flask from flask_sqlalchemy import SQLAlchemyapp …

VB.NET 正则表达式完全指南

VB.NET 正则表达式完全指南 VB.NET通过 System.Text.RegularExpressions 命名空间提供正则表达式支持。本指南将详细介绍VB.NET中正则表达式的使用方法、性能优化和最佳实践。 1. 基础知识 1.1 导入命名空间 Imports System.Text.RegularExpressions1.2 基本使用 Public C…

XML 解析器:深入解析与高效应用

XML 解析器&#xff1a;深入解析与高效应用 引言 XML&#xff08;可扩展标记语言&#xff09;作为一种重要的数据交换格式&#xff0c;被广泛应用于各种系统和平台中。为了有效地处理和解析XML数据&#xff0c;XML解析器发挥着至关重要的作用。本文将深入探讨XML解析器的原理…

【C盘清理】C盘清理工具、Unity缓存文件转移

链接: https://pan.baidu.com/s/1yE_7qF741o4NmBIsrd3XzA?pwdbwnn CCleaner 用于清理磁盘垃圾 勾选你要分析的选项&#xff0c;点击分析&#xff0c;分析完毕后&#xff0c;点击清理。 主要别清错东西了。&#xff08;可以不要勾选网络缓存、网络记录相关的选项&#xff0…

java 迪米特法则,原理、思想、工作流程、实现细节、稳定性、优缺点、应用场景等

迪米特法则&#xff08;Law of Demeter&#xff0c;LoD&#xff09;&#xff0c;也被称为“最少知识原则”&#xff0c;是一种指导面向对象设计的原则&#xff0c;旨在减少对象之间的耦合度。以下是对迪米特法则的详细解析。 1. 定义 迪米特法则指出&#xff1a;一个对象应该…

错误修改系列---基于RNN模型的心脏病预测(pytorch实现)

前言 前几天发布了pytorch实现&#xff0c;TensorFlow实现为&#xff1a;基于RNN模型的心脏病预测(tensorflow实现)&#xff0c;但是一处繁琐地方 一处错误&#xff0c;这篇文章进行修改&#xff0c;修改效果还是好了不少&#xff1b;源文章为&#xff1a;基于RNN模型的心脏病…