HarmonyOS 怎么实现下拉分类列表?

页面,下拉列表以矩阵排列各区,选择切换地区分类 需要demo。

HarmonyOS
2024-10-21 11:44:19
603浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Heiang

请参考下下方demo

@Entry  
@Component  
struct GridHighLight {  
  @State selectIndex_: number = 0  
  private scrollerForScroll: Scroller = new Scroller()  
  private scrollerForList: Scroller = new Scroller()  
  @State showDialog: boolean = false  
  private hoursList: Array<string> = [  
    "全城区", "城关区", "七里河区", "西固区", "安宁区", "红古区", "永登区", "皋兰区", "榆中区", "兰州新区"  
  ]  
  @State recordTitleList: string = ''  
  @State businessHours: string = ''  
  
  onPageShow() {  
    this.businessHours = this.hoursList[0];  
  }  
  
  @Builder  
  //初始下拉按钮  
  tabBuilder() {  
    Row() {  
      Row() {  
        Text(this.businessHours)  
          .fontColor(this.showDialog ? Color.Orange : Color.Black)  
          .fontSize(16)  
          .maxLines(1)  
          .textOverflow({  
            overflow: TextOverflow.Ellipsis  
          })  
          .constraintSize({ maxWidth: 70 })  
  
        Image($r('app.media.ic_public_spinner_small'))  
          .width(20)  
          .height(20)  
          .margin({ left: 8 })  
          .fillColor(this.showDialog ? Color.Orange : Color.Black)  
          .rotate({  
            x: 1,  
            y: 0,  
            z: 0,  
            centerX: '50%',  
            centerY: '50%',  
            angle: this.showDialog ? 180 : 0  
          })  
      }  
      .onClick(() => {  
        this.showDialog = true  
        this.scrollerForScroll.scrollEdge(Edge.Bottom)  
      })  
    }  
    .width('100%')  
    .padding({ left: 16 })  
    .justifyContent(FlexAlign.Start)  
    .alignItems(VerticalAlign.Center)  
  }  
  
  @Builder  
  //下拉的内容  
  tabContentData() {  
    List({ scroller: this.scrollerForList }) {  
      ListItem() {  
        Stack() {  
          // 内容区  
          Column()  
            .width('100%')  
            .height('100%')  
            .opacity(this.showDialog ? .3 : 1)  
            .backgroundColor(this.showDialog ? Color.Black : Color.White)  
  
          // 选择弹窗区域  
          if (this.showDialog) {  
            Column() {  
              Column() {  
                this.ChooseContent()  
              }  
              .width('100%')  
              .backgroundColor($r('sys.color.ohos_id_color_dialog_bg'))  
  
              Column()  
                .layoutWeight(1)  
                .width('100%')  
                .onClick(() => {  
                  this.showDialog = false  
                })  
            }  
            .height('100%')  
          }  
        }  
        .alignContent(Alignment.TopStart)  
      }  
    }  
    .height('100%')  
    .scrollBar(BarState.Off)  
    .edgeEffect(EdgeEffect.None)  
    .nestedScroll({  
      scrollForward: NestedScrollMode.PARENT_FIRST,  
      scrollBackward: NestedScrollMode.SELF_FIRST  
    })  
  }  
  
  @Builder  
  ChooseContent() {  
    Column() {  
      Grid() {  
        ForEach(this.hoursList, (item: string, index: number) => {  
          GridItem() {  
            Text(item)  
              .fontColor(this.selectIndex_ == index ? Color.White : Color.Black)  
          }  
          .backgroundColor(this.selectIndex_ == index ? '#ff5700' : '#efedee')  
          .onClick(() => {  
            this.selectIndex_ = index  
            this.businessHours = item;  
          })  
        }, (item: string): string => item)  
      }  
      .columnsGap(10)  
      .rowsGap(10)  
      .padding({ left: 16, right: 16 })  
      .width('100%')  
      .height(220)  
      .columnsTemplate('1fr 1fr 1fr')  
      .rowsTemplate('1fr 1fr 1fr 1fr')  
  
      Divider()  
        .height('1px')  
        .margin(10)  
        .color($r('sys.color.ohos_id_color_list_separator'))  
  
      Row() {  
        Button('重置')  
          .backgroundColor('#ffefeded')  
          .fontSize(20)  
          .fontColor(Color.Grey)  
          .height(48)  
          .width('50%')  
          .margin({ right: 4 })  
          .onClick(() => {  
            this.selectIndex_ = 0  
            this.businessHours = this.hoursList[0];  
          })  
  
        Button('确认')  
          .fontSize(20)  
          .backgroundColor("#ff5700")  
          .fontColor(Color.White)  
          .height(48)  
          .width('50%')  
          .margin({ right: 4 })  
          .onClick(() => {  
            this.showDialog = false  
          })  
      }  
      .padding({ left: 20, right: 20, bottom: 20 })  
      .justifyContent(FlexAlign.SpaceBetween)  
      .width('100%')  
    }  
    .onTouch((e) => {  
      e.stopPropagation()  
    })  
  }  
  build() {  
    Stack() {  
      Scroll(this.scrollerForScroll) {  
        Column() {  
          if (!this.showDialog) {  
            Row().height(100).width('100%')  
              .backgroundColor('#F1F3F5')  
          }  
          Tabs({ barPosition: BarPosition.Start }) {  
            TabContent() {  
              this.tabContentData()  
            }  
            .tabBar(this.tabBuilder())  
          }  
          .vertical(false)  
          .barWidth('100%')  
        }  
      }  
      .scrollBar(BarState.Off)  
  
      if (this.showDialog) {  
        Column()  
          .height('100%')  
          .width('100%')  
          .hitTestBehavior(HitTestMode.Transparent)  
      }  
    }  
    .alignContent(Alignment.TopStart)  
    .width('100%')  
    .height('100%')  
    .backgroundColor('#F1F3F5')  
  }  
}
  • 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.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
分享
微博
QQ
微信
回复
2024-10-21 17:23:10
相关问题
HarmonyOS Webview如何实现下拉刷新效果
712浏览 • 1回复 待解决
Refresh结合lottie实现下拉刷新动画
1913浏览 • 1回复 待解决
HarmonyOS ArkWeb如何实现下拉刷新功能
1495浏览 • 1回复 待解决
如何使用Swiper组件实现下拉刷新
1725浏览 • 1回复 待解决
HarmonyOS 实现一个自定义分类列表
1182浏览 • 1回复 待解决
HarmonyOS 按钮下拉列表弹窗
776浏览 • 1回复 待解决
应用分类应该怎么选择?
5754浏览 • 2回复 待解决
如何实现下载断点续传
2632浏览 • 0回复 待解决
HarmonyOS 如何实现下列功能,请提供demo
1540浏览 • 1回复 待解决
HarmonyOS 下拉刷新如何实现
509浏览 • 1回复 待解决
HarmonyOS 实现下载功能中的断点续传
873浏览 • 1回复 待解决