HarmonyOS 鸿蒙Next Grid列表使用Refresh加载更多时footer未能展示在底部

HarmonyOS 鸿蒙Next Grid列表使用Refresh加载更多时footer未能展示在底部

Grid列表使用Refresh加载更多时.footer试图未能展示在底部

2 回复

写了一个demo,当footer放到grid里面,如果数组是奇数个,就会只占一半,是否考虑拿出来,这样可以展示在底部,参考demo:

// Index.ets
import { audio } from '[@kit](/user/kit).AudioKit';

[@Entry](/user/Entry)
[@Component](/user/Component)
struct Index {
 [@State](/user/State) arr: Array<number> = [ 0,1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
 [@State](/user/State) refreshing: boolean = false;
 [@State](/user/State) refreshOffset: number = 0;
 [@State](/user/State) refreshState: RefreshStatus = RefreshStatus.Inactive;
 [@State](/user/State) canLoad: boolean = false;
 [@State](/user/State) isLoading: boolean = false;
 [@State](/user/State) num: number =700;

 [@Builder](/user/Builder)
 refreshBuilder() {
   Stack({ alignContent: Alignment.Bottom }) {
     // can use the refresh state to decide whether the progress component is exist or not.
     // in this case, the component is not exist otherwise in the pull down or refresh state
     if (this.refreshState != RefreshStatus.Inactive && this.refreshState != RefreshStatus.Done) {
       Progress({ value: this.refreshOffset, total: 64, type: ProgressType.Ring })
         .width(32).height(32)
         .style({ status: this.refreshing ? ProgressStatus.LOADING : ProgressStatus.PROGRESSING })
         .margin(10)
     }
   }.height("100%").width("100%")
 }

 [@Builder](/user/Builder)
 footer() {
   Row() {
     LoadingProgress().height(32).width(48)
     Text("加载中1")
   }.width("100%")
   .height(64)
   .justifyContent(FlexAlign.Center)
   // hidden this component when don't need to load
   .visibility(this.isLoading ? Visibility.Visible : Visibility.Hidden)
   .position({bottom:3})

 }

 build() {
   Refresh({ refreshing: $$this.refreshing, builder: this.refreshBuilder() }) {
     if (this.isLoading){
       Column(){
         Grid() {
           ForEach(this.arr, (item: number) => {
             GridItem() {
               Text('' + item)
                 .width('100%')
                 .height(80)
                 .fontSize(16)
                 .textAlign(TextAlign.Center)
                 .backgroundColor(0xFFFFFF)
             }
           }, (item: string) => item)

           // GridItem(){
           // this.footer()
           // }

         }
         // .height(this.num)
         .columnsTemplate('1fr 1fr')
         .onScrollIndex((start: number, end: number) => {
           // when reach the end of list, trigger data load
           if (this.canLoad && end >= this.arr.length - 1) {
             this.canLoad = false;
             this.isLoading = true;
             // simulate trigger data load
             setTimeout(() => {
               for (let i = 0; i < 10; i++) {
                 this.arr.push(this.arr.length);
                 this.isLoading = false;
               }
             }, 700)
           }
         })
         .onScrollFrameBegin((offset: number, state: ScrollState) => {
           // loading can be triggered only when swipe up
           if (offset > 5 && !this.isLoading) {
             this.canLoad = true;
           }
           return { offsetRemain: offset };
         })
         .scrollBar(BarState.Off)
         // open the spring back of edge
         .edgeEffect(EdgeEffect.Spring, { alwaysEnabled: true })

       }.height(700)
     }else {
       Column(){
         Grid() {
           ForEach(this.arr, (item: number) => {
             GridItem() {
               Text('' + item)
                 .width('100%')
                 .height(80)
                 .fontSize(16)
                 .textAlign(TextAlign.Center)
                 .backgroundColor(0xFFFFFF)
             }
           }, (item: string) => item)

           // GridItem(){
           // this.footer()
           // }

         }
         // .height(this.num)
         .columnsTemplate('1fr 1fr')
         .onScrollIndex((start: number, end: number) => {
           // when reach the end of list, trigger data load
           if (this.canLoad && end >= this.arr.length - 1) {
             this.canLoad = false;
             this.isLoading = true;
             // simulate trigger data load
             setTimeout(() => {
               for (let i = 0; i < 10; i++) {
                 this.arr.push(this.arr.length);
                 this.isLoading = false;
               }
             }, 700)
           }
         })
         .onScrollFrameBegin((offset: number, state: ScrollState) => {
           // loading can be triggered only when swipe up
           if (offset > 5 && !this.isLoading) {
             this.canLoad = true;
           }
           return { offsetRemain: offset };
         })
         .scrollBar(BarState.Off)
         // open the spring back of edge
         .edgeEffect(EdgeEffect.Spring, { alwaysEnabled: true })

       }.height('100%')
     }

     this.footer();
   }
   .width('100%')
   .height('100%')
   .backgroundColor(0xDCDCDC)
   .onOffsetChange((offset: number) => {
     this.refreshOffset = offset;
   })
   .onStateChange((state: RefreshStatus) => {
     this.refreshState = state;
   })
   .onRefreshing(() => {
     // simulate refresh the data
     setTimeout(() => {
       this.refreshing = false;
     }, 2000)
   })
 }
}

更多关于HarmonyOS 鸿蒙Next Grid列表使用Refresh加载更多时footer未能展示在底部的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙系统中,Next Grid列表组件在使用Refresh加载更多数据时,若遇到footer未能正确展示在底部的问题,这通常与布局管理、滚动视图或Refresh组件的配置有关。

首先,请确保你的Next Grid列表被正确嵌套在支持滚动的容器中,如Scroll组件,并确保该容器的布局属性(如flex-directionjustify-content等)不会干扰footer的定位。检查footer的样式设置,特别是position属性,确保它不是被设置为absoluterelative而导致位置不正确。

其次,验证Refresh组件的配置。确保Refresh的触发区域没有覆盖到footer的位置,同时检查Refresh的加载完成回调中是否有逻辑错误导致footer未能正确更新位置。

此外,考虑到可能是版本或特定环境下的兼容性问题,建议检查你所使用的HarmonyOS SDK版本是否支持当前的开发需求,并查看是否有相关的已知问题或补丁。

如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html。在那里,你可以获得更专业的技术支持和解决方案。

回到顶部