HarmonyOS List联动滑动

有个页面需要两个List联动,且第一个List需要吸顶,但使用ListItemGroup做吸顶功能后,由于Index变成Apis文档中说的:ListItemGroup作为一个整体计算一个索引值,ListItemGroup内部的ListItem不计算索引值。导致两个List不再能根据Index联动.

HarmonyOS
2024-12-20 15:46:39
1150浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

demo示例如下:

// 商品顶部分类
interface CategoriesType {
  current: string[],
  hot: string[],
  Categories: Map<string, Category[]>
}

interface Category {
  code: string;
  category: string;
}

@Entry
@Component
export default  struct CityList{
  private currentCategory: string = ''
  private hotCategories: string[] = []
  private groupCategories: Map<string, Category[]> = new Map
  private groupNameList: string[] = ['A区', 'B区', 'C区', 'D区', 'F区', 'G区', 'H区', 'J区', 'K区', 'L区']
  @State private selectGroupIndex: number = -1
  private categoryScroller: ListScroller = new ListScroller()
  private categoryScroller1: ListScroller = new ListScroller()
  private isClickScroll:boolean = false
  aboutToAppear() {
    let jsonString: string = '{"current":["保健品专区"],"hot":["险种转换","保单挂失","保单补发"],"Categories":{"A区":[{"code":"001","category":"新增附加险"},{"code":"002","category":"保险附效1"},{"code":"003","category":"保险附效2"},{"code":"004","category":"保险附效3"},{"code":"005","category":"保险附效4"},{"code":"006","category":"保险附效5"},{"code":"007","category":"保险附效6"}],"B区":[{"code":"008","category":"保险附效1"},{"code":"009","category":"保险附效2"},{"code":"012","category":"保险附效3"}],"C区":[{"code":"008","category":"保险附效1"},{"code":"009","category":"保险附效2"},{"code":"010","category":"保险附效3"},{"code":"011","category":"保险附效4"},{"code":"012","category":"保险附效5"}],"D区":[{"code":"008","category":"保险附效1"},{"code":"009","category":"保险附效2"},{"code":"010","category":"保险附效3"},{"code":"011","category":"保险附效4"},{"code":"012","category":"保险附效5"}],"E区":[{"code":"008","category":"保险附效1"},{"code":"009","category":"保险附效2"},{"code":"010","category":"保险附效3"},{"code":"011","category":"保险附效4"},{"code":"012","category":"保险附效5"}],"F区":[{"code":"008","category":"保险附效1"},{"code":"009","category":"保险附效2"},{"code":"010","category":"保险附效3"},{"code":"011","category":"保险附效4"},{"code":"012","category":"保险附效5"}],"G区":[{"code":"008","category":"保险附效1"},{"code":"009","category":"保险附效2"},{"code":"010","category":"保险附效3"},{"code":"011","category":"保险附效4"},{"code":"012","category":"保险附效5"}],"H区":[{"code":"008","category":"保险附效1"},{"code":"009","category":"保险附效2"},{"code":"010","category":"保险附效3"},{"code":"011","category":"保险附效4"},{"code":"012","category":"保险附效5"}],"J区":[{"code":"008","category":"保险附效1"},{"code":"009","category":"保险附效2"},{"code":"010","category":"保险附效3"},{"code":"011","category":"保险附效4"},{"code":"012","category":"保险附效5"}],"K区":[{"code":"008","category":"保险附效1"},{"code":"009","category":"保险附效2"},{"code":"010","category":"保险附效3"},{"code":"011","category":"保险附效4"},{"code":"012","category":"保险附效5"}],"L区":[{"code":"008","category":"保险附效1"},{"code":"009","category":"保险附效2"},{"code":"010","category":"保险附效3"},{"code":"011","category":"保险附效4"},{"code":"012","category":"保险附效5"}]}}'
    let data: CategoriesType = JSON.parse(jsonString) as CategoriesType
    this.currentCategory = data.current[0]
    this.hotCategories = data.hot
    this.groupCategories = data.Categories
    console.log('fxm = ', JSON.stringify(this.groupCategories['A区']))
  }

  build() {
    Column() {
      Row() {
        this.navigationList()
      }
      .width('100%')
      .height(42)
      Column() {
        this.categoryList()
      }
      .height('100%')
    }
  }

  getCitiesWithGroupName(name: string): Category[] {
    console.log('fxm ', this.groupCategories[name])
    return this.groupCategories[name]
  }

  @Builder
  itemHead(text: string) {
    Text(text)
      .fontSize(16)
      .width("100%")
      .padding({ left: 10 })
      .height(45)
      .backgroundColor(0xEEEEEE)
  }

  @Builder
  categoryList() {
    List({ scroller: this.categoryScroller }) {
      ListItemGroup({ header: this.itemHead('当前专区') }) {
        ListItem() {
          Text(this.currentCategory)
            .width("100%")
            .height(45)
            .fontSize(16)
            .textAlign(TextAlign.Start)
            .backgroundColor(0xFFFFFF)
            .padding({ left: 10 })
        }
      }

      ListItemGroup({ header: this.itemHead('热门专区') }) {
        ForEach(this.hotCategories, (hotCategory: string) => {
          ListItem() {
            Text(hotCategory)
              .width("100%")
              .height(45)
              .fontSize(16)
              .textAlign(TextAlign.Start)
              .backgroundColor(0xFFFFFF)
              .padding({ left: 10 })
          }
        })
      }

      // A~L专区分组
      ForEach(this.groupNameList, (item: string) => {
        ListItemGroup({ header: this.itemHead(item) }) {
          ForEach(this.getCitiesWithGroupName(item), (item: Category) => {
            ListItem() {
              Text(item.category)
                .width("100%")
                .height(45)
                .fontSize(16)
                .textAlign(TextAlign.Start)
                .backgroundColor(0xFFFFFF)
                .padding({ left: 10 })
            }
          }, (item: Category) => item.category)
        }
      })
    }
    .width('100%')
    .height('100%')
    .scrollBar(BarState.Off)
    .sticky(StickyStyle.Header)
    .onTouch(()=>{
      // 分区列表触摸滚动,isClickScroll=false,防止滚动过程中与导航列表触发滚动冲突
      this.isClickScroll = false
    })
    .onScrollIndex((start: number, end: number, center: number)=>{
      // 通过selectGroupIndex状态变量与start联动控制导航列表选中状态
      if(!this.isClickScroll)
        this.selectGroupIndex = start - 2
    })
  }

  @Builder
  navigationList() {
    List({scroller:this.categoryScroller1}) {
      ForEach(this.groupNameList, (item: string, index: number) => {
        ListItem() {
          Text(item)
            .width(42)
            .height(30)
            .fontSize(16)
            .textAlign(TextAlign.Start)
            .backgroundColor(index == this.selectGroupIndex ? 0xCCCCCC : Color.Transparent)
            .padding({ left: 10 })
            .borderRadius(15)
            .onClick(() => {
              // 导航列表选中isClickScroll=true,防止与分区列表滚动过程中带动导航列表状态变化
              this.isClickScroll = true
              this.selectGroupIndex = index
              // 通过导航选中selectGroupIndex与Scroller控制分区列表滚动到对应位置
              this.categoryScroller.scrollToIndex(index + 2, true, ScrollAlign.START)
            })
        }
      }, (item: string) => item)
    }
    .listDirection(Axis.Horizontal)
    .backgroundColor(Color.Transparent)
    .width('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.
分享
微博
QQ
微信
回复
2024-12-20 18:05:40
相关问题
HarmonyOS list web scroll 联动
546浏览 • 1回复 待解决
怎么实现键盘和滑动联动
332浏览 • 0回复 待解决
HarmonyOS list无法滑动
539浏览 • 1回复 待解决
HarmonyOS 监听List组件滑动
1001浏览 • 1回复 待解决
HarmonyOS list滑动问题
1502浏览 • 1回复 待解决
HarmonyOS list嵌套MapComponent滑动冲突
609浏览 • 1回复 待解决
HarmonyOS 组件List如何禁止滑动
1556浏览 • 1回复 待解决
HarmonyOS List嵌套waterflow滑动卡顿
1040浏览 • 1回复 待解决
HarmonyOS 动态修改List不让它滑动
770浏览 • 1回复 待解决
HarmonyOS list 嵌套web滑动切换问题
1232浏览 • 1回复 待解决
HarmonyOS如何拦截list滑动事件?
1227浏览 • 1回复 待解决
HarmonyOS List+Swipe+web滑动冲突
677浏览 • 1回复 待解决
HarmonyOS scroll嵌套List不能整体滑动
1362浏览 • 1回复 待解决
如何屏蔽List滑动事件
3075浏览 • 1回复 待解决
scroll和list的嵌套滑动
2570浏览 • 1回复 待解决
HarmonyOS如何去掉List组件的滑动线
1722浏览 • 1回复 待解决
HarmonyOS List怎么适配鼠标水平滑动
474浏览 • 1回复 待解决