HarmonyOS 首页下拉刷新异常

首页需要使用下拉刷新,使用了第三方框架https://ohpm.openharmony.cn/#/cn/detail/@abner%2Frefresh

此框架支持任意view的下拉刷新,在刷新框架内部嵌套了Scroll。当Scroll滑动到顶部的时候触发下拉刷新。但实际下滑时就触发了帮忙看看是什么问题。

HarmonyOS
2024-10-17 11:12:07
783浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

可以参考如下代码:

// TabViewDemo.ets  
import { RefreshController, RefreshLayout, WindowFullScreenUtil } from '@abner/refresh';  
import { headerRefreshLayout } from './headerRefreshLayout'  
@Entry  
@Component  
struct TabViewDemo {  
  @State currentIndex2: number = 0;//顶部的下标  
  @State isTop:boolean = false;  
  @State isRefreshing: boolean = false  
  @State selectedindex:number = 0;  
  @State controller1: RefreshController = new RefreshController() //刷新控制器  
  @State currentIndex: number = 0;  
  @State subCurrentIndex: number = 0;  
  private controller: TabsController = new TabsController();  
  private subController: TabsController = new TabsController();  
  private panOptionLeft: PanGestureOptions = new PanGestureOptions({ direction: PanDirection.Left });  
  private panOptionRight: PanGestureOptions = new PanGestureOptions({ direction: PanDirection.Right });  
  private scroller: Scroller = new Scroller()  
  private title: string[] =  
    ['aaaa', 'bbbb', 'cccc', 'dddd', 'eeee', 'ffff', 'gggg', 'hhhh', 'iiii', 'jjjj']  
  
  build() {  
    Tabs({ barPosition: BarPosition.End, index: this.currentIndex, controller: this.controller }) {  
      TabContent() {  
        this.HealthTab()  
      }  
      .tabBar('健康')  
  
      TabContent() {  
      }  
      .tabBar('商城')  
  
      TabContent() {  
      }  
      .tabBar('消息')  
  
      TabContent() {  
      }  
      .tabBar('我的')  
    }  
    .animationDuration(400)  
    .onChange((index: number) => {  
      this.currentIndex = index  
    })  
  }  
  @Builder  
  itemLayout() {  
    Scroll() {  
      Column(){  
        Row(){  
          Text("我是页面 的内容")  
            .fontColor(Color.White)  
            .fontSize(30)  
        }  
        .alignItems(VerticalAlign.Center)  
        .justifyContent(FlexAlign.Center)  
        .backgroundColor(Color.Red)  
        .height(200)  
        .width('100%')  
  
        Row(){  
          Text("我是页面 的内容")  
            .fontColor(Color.White)  
            .fontSize(30)  
        }  
        .alignItems(VerticalAlign.Center)  
        .justifyContent(FlexAlign.Center)  
        .backgroundColor(Color.Blue)  
        .height(200)  
        .width('100%')  
        Row(){  
          Text("我是页面 的内容")  
            .fontColor(Color.White)  
            .fontSize(30)  
        }  
        .alignItems(VerticalAlign.Center)  
        .justifyContent(FlexAlign.Center)  
        .backgroundColor(Color.Yellow)  
        .height(200)  
        .width('100%')  
        Row(){  
          Text("我是页面 的内容")  
            .fontColor(Color.White)  
            .fontSize(30)  
        }  
        .alignItems(VerticalAlign.Center)  
        .justifyContent(FlexAlign.Center)  
        .backgroundColor(Color.Gray)  
        .height(200)  
        .width('100%')  
        Row(){  
          Text("我是页面 的内容")  
            .fontColor(Color.Red)  
            .fontSize(30)  
        }  
        .alignItems(VerticalAlign.Center)  
        .justifyContent(FlexAlign.Center)  
        .backgroundColor(Color.White)  
        .height(200)  
        .width('100%')  
        Row(){  
          Text("我是页面 的内容")  
            .fontColor(Color.Red)  
            .fontSize(30)  
        }  
        .alignItems(VerticalAlign.Center)  
        .justifyContent(FlexAlign.Center)  
        .backgroundColor(Color.Black)  
        .height(200)  
        .width('100%')  
      }  
    }  
    .onScrollStop(()=>{  
      console.info('Safe', '滑动停止: '+this.isTop);  
    })  
    .onScroll((xOffset: number, yOffset: number) => {  
      console.info('Safe', '滑动过程: '+xOffset +" yOffset:"+yOffset);  
      this.isTop= false  
    })  
    .onScrollEdge((side: Edge) => {  
      console.info('Safe', ' 滑动到了边缘: ');  
      switch (side){  
        case Edge.Top:  
          console.info('Safe', ' 滑动到了顶部: ');  
          this.isTop = true  
          break  
        case Edge.Bottom:  
          console.info('Safe', ' 滑动到了底部: ');  
          break  
      }  
    })  
    // }  
    .backgroundColor(Color.Yellow)  
    .width("100%")  
    .height("100%")  
  }  
  @Builder  
  HealthTab() {  
    Column() {  
      Row() {  
        List({ space: 5, scroller: this.scroller }) {  
          ForEach(this.title, (item: string, index: number) => {  
            ListItem() {  
              Column(){  
                Text(item)  
                  .fontWeight(this.subCurrentIndex === index ? FontWeight.Bold : FontWeight.Normal)  
                Column()  
                  .height(4)  
                  .width(40)  
                  .backgroundColor(Color.Black)  
                  .visibility(this.subCurrentIndex === index ? Visibility.Visible : Visibility.None)  
              }  
              .justifyContent(FlexAlign.SpaceEvenly)  
              .width('100%')  
              .height('100%')  
            }  
            .onClick(() => {  
              this.subCurrentIndex = index  
              this.subController.changeIndex(index)  
              this.scroller.scrollToIndex(index, true, ScrollAlign.CENTER)  
            })  
            .height('100%')  
            .width('100')  
          })  
        }  
        .height('100%')  
        .scrollBar(BarState.Off)  
        .listDirection(Axis.Horizontal)  
      }  
      .alignItems(VerticalAlign.Center)  
      .height('10%')  
      Tabs({ barPosition: BarPosition.Start, controller: this.subController }) {  
        ForEach(this.title, (item: string, index: number)=>{  
          TabContent() {  
            Refresh({ refreshing: $$this.isRefreshing,builder:headerRefreshLayout()}){  
              this.itemLayout()  
            }  
            .onStateChange((refreshStatus: RefreshStatus) => {  
              console.info('Refresh onStatueChange state is ' + refreshStatus)  
            })  
            .onRefreshing(() => {  
              setTimeout(() => {  
                this.isRefreshing = false  
              }, 1000)  
              console.log('onRefreshing test')  
            })  
          }  
          .backgroundColor(0x123456 + index*50)  
        })  
      }  
      .barHeight(0)  
      .animationDuration(400)  
      .onChange((index: number) => {  
        this.subCurrentIndex = index;  
        this.scroller.scrollToIndex(index, true, ScrollAlign.CENTER)  
      })  
      .barMode(BarMode.Scrollable)  
      .width('100%')  
    }  
    .width('100%')  
  }  
}
// headerRefreshLayout.ets  
@Builder  
export function headerRefreshLayout() {  
  Column(){  
    Text("这是下拉刷新头部")  
      .width("100%")  
      .height(80)  
      .fontColor(Color.White)  
      .textAlign(TextAlign.Center)  
  }  
  .backgroundColor(Color.Green)  
  .width("100%")  
  .height(80)  
}  
@Entry  
@Component  
struct Index {  
  @State message: string = 'Hello World'  
  build() {  
    Row() {  
      Column() {  
        headerRefreshLayout()  
      }  
      .width('100%')  
    }  
    .height('100%')  
  }  
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
  • 195.
  • 196.
  • 197.
  • 198.
  • 199.
  • 200.
  • 201.
  • 202.
  • 203.
  • 204.
  • 205.
  • 206.
  • 207.
  • 208.
  • 209.
  • 210.
  • 211.
  • 212.
  • 213.
  • 214.
  • 215.
  • 216.
  • 217.
  • 218.
  • 219.
  • 220.
  • 221.
  • 222.
  • 223.
  • 224.
  • 225.
  • 226.
  • 227.
  • 228.
  • 229.
  • 230.

可以使用官方提供的组件,参考文档如下:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-container-refresh-V5#示例2

分享
微博
QQ
微信
回复
2024-10-17 17:42:09
相关问题
HarmonyOS rn组件FlatList下拉刷新异常
844浏览 • 1回复 待解决
HarmonyOS 下拉刷新功能
703浏览 • 1回复 待解决
HarmonyOS 下拉刷新如何实现
494浏览 • 1回复 待解决
HarmonyOS 组件的下拉刷新问题
1243浏览 • 1回复 待解决
HarmonyOS 下拉刷新,上拉加载示例
664浏览 • 1回复 待解决
HarmonyOS 下拉刷新,上拉加载功能
581浏览 • 1回复 待解决
HarmonyOS使用Refresh下拉刷新问题
1526浏览 • 1回复 待解决
上拉加载,下拉刷新组件
1128浏览 • 1回复 待解决
HarmonyOS Web组件怎么添加下拉刷新
688浏览 • 1回复 待解决
HarmonyOS 下拉刷新的时候没有更新数据
1306浏览 • 1回复 待解决
HarmonyOS Webview如何实现下拉刷新效果
684浏览 • 1回复 待解决
HarmonyOS ArkWeb如何实现下拉刷新功能
1465浏览 • 1回复 待解决
Refresh结合lottie实现下拉刷新动画
1877浏览 • 1回复 待解决
如何使用Swiper组件实现下拉刷新
1711浏览 • 1回复 待解决