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

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

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

HarmonyOS
2天前
浏览
收藏 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%')
  }
}
分享
微博
QQ
微信
回复
2天前
相关问题
HarmonyOS 怎么实现类似SnackBar效果
60浏览 • 1回复 待解决
如何实现类似keyframes效果
1921浏览 • 1回复 待解决
HarmonyOS 类似翻页效果实现
82浏览 • 1回复 待解决
实现层叠广告滑动效果
890浏览 • 1回复 待解决
滑动组件如何实现单边spring效果
930浏览 • 1回复 待解决
视频进度滑动三种实现方式
1599浏览 • 1回复 待解决
ArkTS卡片能否实现全透明效果
263浏览 • 1回复 待解决
HarmonyOS SegmentButton控件修改高度
27浏览 • 1回复 待解决