HarmonyOS SegmentButton能否实现类似滑动条的效果

SegmentButton(分段式按钮)能否实现类似滑动条的效果,即 selectedIndex 在滑动结束后改变以及点击后立即改变的效果

在某些场景下 使用了SegmentButton(分段式按钮),并通过@Watch来监控Tab的切换,但是我需要实现在SegmentButton中滑动,并在最后一次停留时,抬起手指时才进行状态的响应,即selectedIndexes的切换

HarmonyOS
2024-12-24 14:54:33
805浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

SegmentButton滑动过程中selectedIndexes值修改控制不了,目前处理方案是添加一个条件显示高亮,demo如下:

import {
  ItemRestriction,
  SegmentButton,
  SegmentButtonItemTuple,
  SegmentButtonOptions,
  SegmentButtonTextItem
} from '@ohos.arkui.advanced.SegmentButton'

@Entry
@Component
struct SegmentButtonPage {
  @State @Watch('WatchEventType') eventType: string = '';

  WatchEventType(){
    console.log(this.eventType)
    if(this.eventType == 'Up' || this.eventType == ''){
      this.tabOptions = SegmentButtonOptions.tab({
        buttons: [{ text: '页签按钮1' }, { text: '页签按钮2' }, {
          text: '页签按钮3'
        }] as ItemRestriction<SegmentButtonTextItem>,
        backgroundColor: Color.Green,
        selectedBackgroundColor: Color.Orange,
        fontColor:Color.Black,
        selectedFontColor:Color.Black,
        fontWeight:FontWeight.Regular,
        selectedFontWeight:FontWeight.Regular,
        backgroundBlurStyle: BlurStyle.NONE
      })
    }else{
      this.tabOptions = SegmentButtonOptions.tab({
        buttons: [{ text: '页签按钮1' }, { text: '页签按钮2' }, {
          text: '页签按钮3'
        }] as ItemRestriction<SegmentButtonTextItem>,
        backgroundColor: Color.Green,
        selectedBackgroundColor: Color.Green,
        fontColor:Color.Black,
        selectedFontColor:Color.Black,
        fontWeight:FontWeight.Regular,
        selectedFontWeight:FontWeight.Regular,
        backgroundBlurStyle: BlurStyle.NONE
      })
    }
  }

  @State text: string = '';

  @State tabOptions: SegmentButtonOptions = SegmentButtonOptions.tab({
    buttons: [{ text: '页签按钮1' }, { text: '页签按钮2' }, {
      text: '页签按钮3'
    }] as ItemRestriction<SegmentButtonTextItem>,
    backgroundColor: Color.Green,
    selectedBackgroundColor: Color.Orange,
    fontColor:Color.Black,
    selectedFontColor:Color.Black,
    fontWeight:FontWeight.Regular,
    selectedFontWeight:FontWeight.Regular,
    backgroundBlurStyle: BlurStyle.NONE
  })
  @State tabSelectedIndexes: number[] = [0]
  build() {
    Row() {
      Column() {
        Column({ space: 25 }) {
          // 一
          Text(`${JSON.stringify(this.tabSelectedIndexes)}`)
          Text(this.text)
            .margin(20)
          SegmentButton({
            options: this.tabOptions,
            selectedIndexes: this.tabSelectedIndexes
          })
            .onTouch((event?: TouchEvent) => {
              if(event){
                if (event.type === TouchType.Down) {
                  this.eventType = 'Down';
                }
                if (event.type === TouchType.Up) {
                  this.eventType = 'Up';
                }
                if (event.type === TouchType.Move) {
                  this.eventType = 'Move';
                }
                this.text = 'TouchType:' + this.eventType
              }
            })
        }.width('90%')
      }.width('100%')
    }.height('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.
分享
微博
QQ
微信
回复
2024-12-24 17:37:52


相关问题
如何实现类似keyframes效果
2355浏览 • 1回复 待解决
HarmonyOS 怎么实现类似SnackBar效果
626浏览 • 1回复 待解决
实现层叠广告滑动效果
1585浏览 • 1回复 待解决
HarmonyOS 类似翻页效果实现
930浏览 • 1回复 待解决
视频进度滑动三种实现方式
2839浏览 • 1回复 待解决
HarmonyOS 如何实现类似match_parent效果
1093浏览 • 1回复 待解决
滑动组件如何实现单边spring效果
1590浏览 • 1回复 待解决
ArkTS卡片能否实现全透明效果
1131浏览 • 1回复 待解决
HarmonyOS 自定义滑动
588浏览 • 1回复 待解决