HarmonyOS UI不刷新问题

import PreferencesUtil from '../utils/PreferencesUtil';
import { CommonConstants } from '@ohos/common';

@Entry
@Component
struct SearchPage {
  statusBarHeight: number = AppStorage.get<number>('statusBarHeight') as number
  @State searchValue: string = '洗发'
  @State searchList: string[] = []

  requestData() {

  }

  async onPageShow() {
    this.requestData()
    this.searchList =
      await PreferencesUtil.getPreferenceValue(CommonConstants.SEARCH_LIST,
        CommonConstants.NULL_SEARCH_LIST) as string[]
    console.log(this.searchList.toString())
  }

  build() {
    Column() {
      SearchNav({
        searchValue: $searchValue, onSearchChange: () => {
          console.log(this.searchList.toString())
        }
      })
      SearchComp({
        sectionContent: () => {
          this.SectionBuilder('最近搜索')
        }, listContent: () => {
          // this.ListContentBuilder()
        }
      })
      SearchComp({
        sectionContent: () => {
          this.SectionBuilder('热门搜索')
        }, listContent: () => {
          if (this.searchList) {
            this.ListContentBuilder({ searchList: this.searchList })
          }
        }
      })
    }
    .padding({
      top: this.statusBarHeight,
      left: 10,
      right: 10,
      bottom: 20
    })
    .height('100%')
    .width('100%')
  }

  @Builder
  SectionBuilder(title: string) {
    Row() {
      Text(title)
        .fontColor('#4d4d4d')
        .fontSize(15)
      if (title == '最近搜索') {
        Image($r('app.media.icon_delete'))
          .width(24)
          .height(24)
      }
    }
    .justifyContent(FlexAlign.SpaceBetween)
    .width('100%')
  }

  @Builder
  ListContentBuilder($$: ListParams) {
    GridRow() {
      ForEach($$.searchList, (item: string, index) => {
        GridCol() {
          Text(item)
        }
      })
    }
  }
}

class ListParams {
  searchList: string[] = []
}

@Component
struct SearchComp {
  @Builder
  DefaultContentBuilder() {
  }

  @BuilderParam sectionContent: () => void = this.DefaultContentBuilder
  @BuilderParam listContent: () => void = this.DefaultContentBuilder

  build() {
    Column() {
      this.sectionContent()
      this.listContent()
    }
  }
}

@Component
struct SearchNav {
  searchController: SearchController = new SearchController()
  @Link searchValue: string
  onSearchChange: () => void = () => {
  }

  build() {
    Row() {
      Image($r('app.media.ic_back_black'))
        .width(26)
        .height(26)
        .margin({
          right: 5,
          top: 10,
          bottom: 10
        })
      Search({ value: this.searchValue, placeholder: '搜索你想要的商品', controller: this.searchController })
        .searchButton('搜索', { fontColor: '#999', fontSize: 14 })
        .placeholderFont({ size: 14 })
        .height(34)
        .minFontSize(14)
        .maxFontSize(14)
        .onSubmit((searchValue) => {
          this.searchValue = searchValue
          this.onSearchChange()
        })
        .onChange((searchValue) => {
          this.searchValue = searchValue
          this.onSearchChange()
        })
        .layoutWeight(1)
    }
  }
}
HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

当状态变量被改变时,查询依赖该状态变量的组件。searchList重新赋值时是可以观察到的,但是并没有依赖该状态变量的组件,除非在forEach中把searchList传入,这样便能够查询到依赖该状态变量的组件了。跨组件的可以用@Provide、@Consume传递searchList,参考示例:

//SearchPage
import PreferencesUtil from '../utils/PreferencesUtil';
import { CommonConstants } from '@ohos/common';

@Entry
@Component
struct SearchPage {
  statusBarHeight: number = AppStorage.get<number>('statusBarHeight') as number
  @State searchValue: string = '洗发'
  @State searchList: string[] = []

  requestData() {

  }

  async onPageShow() {
    this.requestData()
    this.searchList =
      await PreferencesUtil.getPreferenceValue(CommonConstants.SEARCH_LIST,
        CommonConstants.NULL_SEARCH_LIST) as string[]
    console.log(this.searchList.toString())
  }
  
  build() {
    Column() {
      SearchNav({
        searchValue: $searchValue, onSearchChange: () => {
          console.log(this.searchList.toString())
        }
      })
      SearchComp({
        sectionContent: () => {
          this.SectionBuilder('最近搜索')
        }, listContent: () => {
          // this.ListContentBuilder()
        }
      })
      SearchComp({
        sectionContent: () => {
          this.SectionBuilder('热门搜索')
        }, listContent: () => {
          if (this.searchList) {
            this.ListContentBuilder({ searchList: this.searchList })
          }
        }
      })
    }
    .padding({
      top: this.statusBarHeight,
      left: 10,
      right: 10,
      bottom: 20
    })
    .height('100%')
    .width('100%')
  }

  @Builder
  SectionBuilder(title: string) {
    Row() {
      Text(title)
        .fontColor('#4d4d4d')
        .fontSize(15)
      if (title == '最近搜索') {
        Image($r('app.media.icon_delete'))
          .width(24)
          .height(24)
      }
    }
    .justifyContent(FlexAlign.SpaceBetween)
    .width('100%')
  }

  @Builder
  ListContentBuilder($$: ListParams) {
    GridRow() {
      ForEach($$.searchList, (item: string, index) => {
        GridCol() {
          Text(item)
        }
      })
    }
  }
}

class ListParams {
  searchList: string[] = []
}

@Component
struct SearchComp {
  @Builder
  DefaultContentBuilder() {
  }

  @BuilderParam sectionContent: () => void = this.DefaultContentBuilder
  @BuilderParam listContent: () => void = this.DefaultContentBuilder

  build() {
    Column() {
      this.sectionContent()
      this.listContent()
    }
  }
}

@Component
struct SearchNav {
  searchController: SearchController = new SearchController()
  @Link searchValue: string
  onSearchChange: () => void = () => {
  }

  build() {
    Row() {
      Image($r('app.media.ic_back_black'))
        .width(26)
        .height(26)
        .margin({
          right: 5,
          top: 10,
          bottom: 10
        })
      Search({ value: this.searchValue, placeholder: '搜索你想要的商品', controller: this.searchController })
        .searchButton('搜索', { fontColor: '#999', fontSize: 14 })
        .placeholderFont({ size: 14 })
        .height(34)
        .minFontSize(14)
        .maxFontSize(14)
        .onSubmit((searchValue) => {
          this.searchValue = searchValue
          this.onSearchChange()
        })
        .onChange((searchValue) => {
          this.searchValue = searchValue
          this.onSearchChange()
        })
        .layoutWeight(1)
    }
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS @Builder UI刷新问题
36浏览 • 1回复 待解决
HarmonyOS UI刷新问题
580浏览 • 0回复 待解决
HarmonyOS 装饰器刷新问题
235浏览 • 1回复 待解决
HarmonyOS 状态变量刷新问题
518浏览 • 1回复 待解决
HarmonyOS UI刷新
30浏览 • 1回复 待解决
HarmonyOS 数据刷新问题
103浏览 • 1回复 待解决
HarmonyOS 页面刷新问题
339浏览 • 1回复 待解决
HarmonyOS 列表刷新问题
659浏览 • 1回复 待解决
HarmonyOS 组件刷新问题
85浏览 • 1回复 待解决
HarmonyOS webview刷新问题
52浏览 • 2回复 待解决
状态装饰器 ui刷新问题
2476浏览 • 1回复 待解决
NativeWindowFlushBuffer接口刷新问题
2020浏览 • 1回复 待解决
HarmonyOS swiper数据刷新问题
42浏览 • 1回复 待解决
HarmonyOS 页面跳转刷新问题
951浏览 • 1回复 待解决
HarmonyOS ForEach列表刷新问题
83浏览 • 1回复 待解决
HarmonyOS List item 刷新问题
817浏览 • 1回复 待解决
HarmonyOS 页面数据刷新问题
42浏览 • 1回复 待解决
HarmonyOS 组件的下拉刷新问题
512浏览 • 1回复 待解决
HarmonyOS使用Refresh下拉刷新问题
943浏览 • 1回复 待解决
IF条件变化后UI刷新
783浏览 • 1回复 待解决
HarmonyOS @builder方法的ui刷新
58浏览 • 1回复 待解决
HarmonyOS 自定义弹窗刷新问题
73浏览 • 1回复 待解决