HarmonyOS Tabs组件嵌套滑动

存在两个Tabs,希望A和B两个Tabs有联动效果,当B滑动到最右侧,继续左滑时,A往右滑动。

HarmonyOS
2024-10-12 09:26:57
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
FengTianYa

核心功能参考以下demo:方法是在下面的tabs为最后一个时,增加拖动手势,再向右滑动上面的tabs index++,向前滑动同理,下面的tabs为第一个时,增加拖动手势,再向左滑动上面的tabs index--。

//xxx.index  
import { CommonConstants } from '../common/constants/CommonConstants';  
@Entry  
@Component  
struct NestedCeiling {  
  private listArr: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];  
  private listArr1: number[] = [1, 2, 3, 4, 5, 6, 7, 8];  
  private scrollerForScroll: Scroller = new Scroller();  
  private scrollerForList: Scroller = new Scroller();  
  @State currentIndexTop:number = 0;//上层tabs索引  
  @State currentIndexUnder:number = 0;//下层tabs索引  
  @Builder  
  tabBuilder(index: number, name: string) {  
    Column() {  
      Text(name)  
        .fontSize($r('app.float.middle_font_size'))  
        .lineHeight($r('app.float.title_line_height'))  
        .margin({  
          top: $r('app.float.title_margin_top'),  
          bottom: $r('app.float.title_margin_bottom')  
        })  
    }  
  }  
  @Builder  
  listBuilder(listName: string, tabName: string, index: number) {  
    TabContent() {  
      Scroll(){  
        Flex({direction:FlexDirection.Column,justifyContent:FlexAlign.Start}){  
          List({ space: CommonConstants.LIST_SPACE, scroller: this.scrollerForList }) {  
            ForEach(this.listArr1, (item: number) => {  
              ListItem() {  
                Text(listName + item)  
                  .width(CommonConstants.FULL_WIDTH)  
                  .height(CommonConstants.FULL_HEIGHT)  
                  .borderRadius($r('app.float.list_item_radius'))  
                  .fontSize($r('app.float.middle_font_size'))  
                  .fontWeight(CommonConstants.FONT_WEIGHT_FIVE)  
                  .padding({ left: $r('app.float.list_item_padding') })  
                  .backgroundColor(Color.White)  
              }  
              .width(CommonConstants.FULL_WIDTH)  
              .height($r('app.float.list_item_height'))  
            }, (item: string) => JSON.stringify(item))  
          }  
          .padding({  
            left: $r('app.float.list_padding'),  
            right: $r('app.float.list_padding')  
          })  
          .width(CommonConstants.FULL_WIDTH)  
          .edgeEffect(EdgeEffect.None)  
          .scrollBar(BarState.Off)  
          .nestedScroll({  
            scrollForward: NestedScrollMode.SELF_FIRST,  
            scrollBackward: NestedScrollMode.SELF_FIRST  
          })  
          //todo 1、设置子tabs,并增加参数判断是否为最后一个TabContent  
          Tabs({index:$$this.currentIndexUnder}) {  
            this.listBuilder2(listName+'b', tabName, index, false)  
            this.listBuilder2(listName+'b', tabName, index, true)  
          }  
        }  
      }  
    }  
    .tabBar(SubTabBarStyle.of(listName+""))  
  }  
  //第四个参数,自主标记是否为最后一个  
  @Builder  
  listBuilder2(listName: string, tabName: string, index: number, boo:boolean){  
    //todo 2、判断为最后一个TabContent时增加拖拽事件  
    if(boo){  
      TabContent() {  
        List({ space: CommonConstants.LIST_SPACE, scroller: this.scrollerForList }) {  
          ForEach(this.listArr, (item: number) => {  
            ListItem() {  
              Text(listName + item)  
                .width(CommonConstants.FULL_WIDTH)  
                .height(CommonConstants.FULL_HEIGHT)  
                .borderRadius($r('app.float.list_item_radius'))  
                .fontSize($r('app.float.middle_font_size'))  
                .fontWeight(CommonConstants.FONT_WEIGHT_FIVE)  
                .padding({ left: $r('app.float.list_item_padding') })  
                .backgroundColor(Color.White)  
            }  
            .width(CommonConstants.FULL_WIDTH)  
            .height($r('app.float.list_item_height'))  
          }, (item: string) => JSON.stringify(item))  
        }  
        .padding({  
          left: $r('app.float.list_padding'),  
          right: $r('app.float.list_padding')  
        })  
        .width(CommonConstants.FULL_WIDTH)  
        .height(CommonConstants.FULL_HEIGHT)  
        .edgeEffect(EdgeEffect.None)  
        .scrollBar(BarState.Off)  
        .nestedScroll({  
          scrollForward: NestedScrollMode.PARENT_FIRST,  
          scrollBackward: NestedScrollMode.SELF_FIRST  
        })  
      }  
      //todo 3、绑定手势动作  
      .gesture(  
        // 绑定拖动手势,向右,移动距离为5vp(默认)时触发  
     PanGesture({direction:PanDirection.Left})  
          .onActionStart(() => {  
            this.currentIndexTop++  
            this.currentIndexUnder=0  
          })  
      )  
      .tabBar(SubTabBarStyle.of(listName+""))  
    }else{  
      TabContent() {  
        List({ space: CommonConstants.LIST_SPACE, scroller: this.scrollerForList }) {  
          ForEach(this.listArr, (item: number) => {  
            ListItem() {  
              Text(listName + item)  
                .width(CommonConstants.FULL_WIDTH)  
                .height(CommonConstants.FULL_HEIGHT)  
                .borderRadius($r('app.float.list_item_radius'))  
                .fontSize($r('app.float.middle_font_size'))  
                .fontWeight(CommonConstants.FONT_WEIGHT_FIVE)  
                .padding({ left: $r('app.float.list_item_padding') })  
                .backgroundColor(Color.White)  
            }  
            .width(CommonConstants.FULL_WIDTH)  
            .height($r('app.float.list_item_height'))  
          }, (item: string) => JSON.stringify(item))  
        }  
        .padding({  
          left: $r('app.float.list_padding'),  
          right: $r('app.float.list_padding')  
        })  
        .width(CommonConstants.FULL_WIDTH)  
        .height(CommonConstants.FULL_HEIGHT)  
        .edgeEffect(EdgeEffect.None)  
        .scrollBar(BarState.Off)  
        .nestedScroll({  
          scrollForward: NestedScrollMode.PARENT_FIRST,  
          scrollBackward: NestedScrollMode.SELF_FIRST  
        })  
      }  
      .tabBar(SubTabBarStyle.of(listName+""))  
    }  
  }  
  build() {  
    Column() {  
      Text($r('app.string.title'))  
        .fontWeight(FontWeight.Bold)  
        .fontSize($r('app.float.big_font_size'))  
        .height($r('app.float.list_item_height'))  
        .textAlign(TextAlign.Start)  
        .width(CommonConstants.FULL_WIDTH)  
        .padding({  
          left: $r('app.float.discover_left_padding'),  
          bottom: $r('app.float.discover_bottom_padding'),  
          top: $r('app.float.discover_top_padding')  
        })  
        .onClick(()=>{  
          this.currentIndexTop++  
        })  
      Scroll(this.scrollerForScroll) {  
        Column() {  
          //外层父tabs  
          Tabs({index:$$this.currentIndexTop}) {  
            this.listBuilder(CommonConstants.LIST_NAME_1, CommonConstants.TAB_NAME_1, 0)  
            this.listBuilder(CommonConstants.LIST_NAME_2, CommonConstants.TAB_NAME_2, 1)  
          }  
          .barWidth($r('app.float.bar_width'))  
        }  
      }  
      .width(CommonConstants.FULL_WIDTH)  
      .height(CommonConstants.STACK_HEIGHT)  
    }  
    .height(CommonConstants.FULL_HEIGHT)  
    .backgroundColor($r('app.color.start_window_background'))  
  }  
}
//float.json  
{  
  "float": [  
    {  
      "name": "middle_font_size",  
      "value": "16fp"  
    },  
    {  
      "name": "title_line_height",  
      "value": "22vp"  
    },  
    {  
      "name": "title_margin_top",  
      "value": "10vp"  
    },  
    {  
      "name": "title_margin_bottom",  
      "value": "5vp"  
    },  
    {  
      "name": "divider_width",  
      "value": "64vp"  
    },  
    {  
      "name": "list_item_radius",  
      "value": "24vp"  
    },  
    {  
      "name": "list_item_padding",  
      "value": "12vp"  
    },  
    {  
      "name": "list_padding",  
      "value": "10vp"  
    },  
    {  
      "name": "list_item_height",  
      "value": "56vp"  
    },  
    {  
      "name": "big_font_size",  
      "value": "30fp"  
    }
分享
微博
QQ
微信
回复
2024-10-12 15:40:01
相关问题
Tabs组件嵌套滑动组件
1427浏览 • 1回复 待解决
HarmonyOS Tabs组件嵌套Tabs组件问题
799浏览 • 1回复 待解决
HarmonyOS Tabs和Web嵌套左右滑动问题
336浏览 • 1回复 待解决
如何处理tabs嵌套web滑动场景
486浏览 • 1回复 待解决
HarmonyOS Refresh组件嵌套滑动冲突问题
963浏览 • 1回复 待解决
tabs组件 左右滑动延迟较高
1069浏览 • 1回复 待解决
HarmonyOS 嵌套滑动问题
342浏览 • 1回复 待解决
HarmonyOS 嵌套滑动NestedScroll 指定offset
398浏览 • 1回复 待解决
HarmonyOS scroll嵌套List不能整体滑动
454浏览 • 1回复 待解决
Scroll与WaterFlow滑动嵌套
1028浏览 • 1回复 待解决
HarmonyOS List嵌套waterflow滑动卡顿
279浏览 • 1回复 待解决
HarmonyOS list 嵌套web滑动切换问题
475浏览 • 1回复 待解决
滑动嵌套事件冲突处理
268浏览 • 0回复 待解决
scroll和list的嵌套滑动
1497浏览 • 1回复 待解决