HarmonyOS list嵌套list,双重list都使用LazyForEach的时候,内层的list的item出现空白问题

操作步骤:

list{
  list{//内层list是横向滚动
    innerItem(). //当上下滚动的时候,这里的图片数据会丢失,不管是本地资源文件数据还是网络加载的图片,但文字不会丢失
  }
}
HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
shlp

完整示例代码如下:

import { AddressDataSource, UserDataSource, AddressItem, User } from './DataSource'
const picture =
  'xxxxx'
@Entry
@Component
struct Index {
  private mDataResource = new UserDataSource()
  aboutToAppear(): void {
    for (let index = 0; index < 50; index++) {
      const user = {
        name: `用户名称${index}`,
        age: `用户年龄${index}`
      } as User
      if (index === 4 || index === 8 || index === 24 || index === 37) {
        user.address = []
        const test = new AddressDataSource()
        for (let position = 0; position < 3; position++) {
          const addressItem: AddressItem = {
            addressName: `地址${position}`,
            addressCity: `城市${position}`,
            homePicture: picture
          }
          user.address.push(addressItem)
          test.momentList.push(addressItem)
        }
        user.addressDataSource = test
      }
      this.mDataResource.momentList.push(user)
    }
  }
  handleItemType(user: User): string {
    return user.address && user.address.length > 0 ? 'address' : 'user'
  }
  build() {
    Column() {
      Image(picture)
        .height(30)
        .width(30)
      List() {
        LazyForEach(this.mDataResource, (user: User, index: number) => {
          ListItem() {
            if (this.handleItemType(user) === 'address') {
              AddressModule({ user })
            } else {
              UserModule({ user })
            }
          }.width('100%')
          .reuseId(this.handleItemType(user))
        }, (item: User) => item.name)
      }
      .height('100%')
      .width('100%')
    }
  }
}
@Reusable
@Component
struct UserModule {
  user?: User
  @State userName: string = ''
  @State userAge: string = ''
  aboutToAppear(): void {
    this.handleParams(this.user)
  }
  aboutToReuse(params: Record<string, Object>): void {
    this.handleParams(params.user as User)
  }
  handleParams(user?: User) {
    this.userName = user?.name ?? ''
    this.userAge = user?.age ?? '0'
  }
  build() {
    Row() {
      Text(this.userName)
      Blank()
      Text(this.userAge)
    }
    .backgroundColor(Color.Gray)
    .margin({
      top: 18
    })
    .padding(16)
  }
}
@Reusable
@Component
struct AddressModule {
  user?: User
  private addressDataSource = new AddressDataSource()
  aboutToAppear(): void {
    console.info("AddressModule create")
    this.handleParams(this.user)
  }
  aboutToReuse(params: Record<string, Object>): void {
    this.handleParams(params.user as User)
  }
  handleParams(user?: User) {
    this.addressDataSource.momentList = []
    this.addressDataSource.momentList.push(...user?.address ?? [])
    this.addressDataSource.notifyDataReload()
  }
  @Builder
  AddressChild(address: AddressItem) {
    Row() {
      Image($r('app.media.startIcon'))
        .height(30)
        .width(30)
      Column() {
        Text(address.addressCity)
        Text(address.addressName)
      }
      .justifyContent(FlexAlign.SpaceBetween)
      Image(picture)
        .height(30)
        .width(30)
    }
    .width(100)
  }
  build() {
    List() {
      LazyForEach(this.user?.addressDataSource, (address: AddressItem, index: number) => {
        ListItem() {
          this.AddressChild(address)
        }
      }, (item: User) => item.name)
    }
    .listDirection(Axis.Horizontal)
    .width(180).height(120)
  }
}
分享
微博
QQ
微信
回复
21h前
相关问题
HarmonyOS List嵌套list布局
37浏览 • 1回复 待解决
list 如何使用 lazyforeach
303浏览 • 1回复 待解决
HarmonyOS Web组件和List嵌套使用问题
287浏览 • 1回复 待解决
HarmonyOS List item 刷新问题
815浏览 • 1回复 待解决
HarmonyOS listitem如何保存状态
321浏览 • 2回复 待解决
ArkTS UIList和集合List冲突问题
453浏览 • 1回复 待解决
scroll和list嵌套滑动
1546浏览 • 1回复 待解决
HarmonyOSList是如何加载item
244浏览 • 2回复 待解决
HarmonyOS list 嵌套web滑动切换问题
509浏览 • 1回复 待解决
HarmonyOS listitem交互效果处理
427浏览 • 1回复 待解决
HarmonyOS Listspace问题
69浏览 • 1回复 待解决
HarmonyOS List和Web嵌套问题
16浏览 • 0回复 待解决
使用List lazyForeach时,reuseId未生效
329浏览 • 1回复 待解决
HarmonyOS 使用List警告
281浏览 • 1回复 待解决
怎么获取List里面每个itemposition?
297浏览 • 1回复 待解决