HarmonyOS List联动滑动

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

HarmonyOS
2天前
浏览
收藏 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%')
  }
}
分享
微博
QQ
微信
回复
2天前
相关问题
HarmonyOS list滑动问题
815浏览 • 1回复 待解决
HarmonyOS list 嵌套web滑动切换问题
490浏览 • 1回复 待解决
HarmonyOS List嵌套waterflow滑动卡顿
285浏览 • 1回复 待解决
HarmonyOS 动态修改List不让它滑动
55浏览 • 1回复 待解决
HarmonyOS如何拦截list滑动事件?
401浏览 • 1回复 待解决
HarmonyOS scroll嵌套List不能整体滑动
485浏览 • 1回复 待解决
HarmonyOS List+Swipe+web滑动冲突
189浏览 • 1回复 待解决
HarmonyOS如何去掉List组件的滑动线
910浏览 • 1回复 待解决
HarmonyOS list组件如何设置匀速滑动
40浏览 • 1回复 待解决
HarmonyOS List怎么适配鼠标水平滑动
37浏览 • 1回复 待解决
HarmonyOS List停止滑动回调不准确
307浏览 • 1回复 待解决
HarmonyOS List滑动速度是否能控制?
297浏览 • 1回复 待解决
如何屏蔽List滑动事件
2426浏览 • 1回复 待解决
scroll和list的嵌套滑动
1518浏览 • 1回复 待解决