滚动视图联动:实现多List联动滚动,横竖向滚动

左右List数据联动,可上下滚动,右侧List可左右滚动,左侧List悬停左侧效果。

场景:股票列表,汽车类属性比较

HarmonyOS
2024-05-26 15:48:55
浏览
收藏 1
回答 1
回答 1
按赞同
/
按时间
vclearner

使用的核心API

组件List

 .onScrollFrameBegin() 
 .nestedScroll()
  • 1.
  • 2.

组件Scroll

 .onScroll() 
 .nestedScroll()
  • 1.
  • 2.

滚动视图控制器Scroller

 .currentOffset() 
 .scrollTo()
  • 1.
  • 2.

核心代码解释

1. 确定总体布局

contentBuilder() { 
    Column() { 
        // 顶部固定 
        Row(){ 
            // 与左侧固定列表(l2)保持一致 
            Column() 
            // 可左右滚动列表(l1) 
            List()  
        } 
        // 内容滚动区域 
        Row() { 
            // 左侧固定列表(l2) 
            List()  
            // 右侧可左右滚动Scroll 
            Scroll() { 
                // 可上下滚动列表(l3) 
                List() 
            }  
        } 
    } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.

1. 顶部固定

顶部固定位置左侧

Column() { 
    Text('共11款车') 
        .fontSize(15) 
    Toggle({ type: ToggleType.Switch, isOn: false }) 
        .height(30) 
        .width(50) 
    Text('只看差异') 
        .fontSize(15) 
        .fontWeight(FontWeight.Bolder) 
} 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

顶部固定位置右侧

List({ scroller: this.topRightScroller }) { 
        ForEach(this.topRightArr, (item: string, index: number) => { 
          ListItem() { 
            Text(item) 
              .height(100) 
              .width(RightItemWidth) 
              .fontSize(16) 
              .textAlign(TextAlign.Start) 
              .borderRadius(0) 
              .padding(10) 
              .backgroundColor(0xFFFFFF) 
          } 
        }, (item: string) => item) 
      } 
 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

1. 内容区域

左侧固定

List({scroller: this.tempScroller }) { 
      ForEach(this.leftGroupArr, (item: string, index: number) => { 
        ListItemGroup({ header: this.leftStickyHeader(item) }) { 
          ForEach(this.arr, (item: string, index: number) => { 
            ListItem() { 
              Column(){ 
                Text(item) 
                  .width(LeftItemWidth) 
                  .height('100%') 
                  .fontSize(16) 
                  .textAlign(TextAlign.Start) 
                  .backgroundColor(0xFFFFFF) 
                  .padding(10) 
                  .layoutWeight(1) 
                Line().width("100%").height(0.5).backgroundColor(0xeeeeee) 
              } 
            } 
            .height(100) 
          }, (item: string) => item) 
        } 
      }) 
    }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.

右侧上下滚动

 Scroll(this.hScroller) { 
      List({ initialIndex: 0, scroller: this.fullScroller }) { 
        ForEach(this.leftGroupArr, (item: string, index: number) => { 
          ListItemGroup({ header: this.rightStickyHeader(index) }) { 
            ForEach(this.arr, (item: string, index: number) => { 
              // 嵌套可左右滚动list作为上下滚动list的item 
              // 自定义组件 
  
            }, (item: string) => item) 
          } 
        }) 
      } 
    .listDirection(Axis.Vertical) 
  
    ...... 
    } 
  
    ...... 
  } 
 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.

右侧上下滚动嵌套左右滚动

 List({ scroller: this.scroller }) { 
      ForEach(this.arr, (item: string, index: number) => { 
        ListItem() { 
          Text('' + index) 
            .height(100) 
            .width(RightItemWidth) 
            .fontSize(16) 
            .textAlign(TextAlign.Start) 
            .borderRadius(0) 
            .padding(10) 
            .backgroundColor(0xFFFFFF) 
        } 
      }, (item: string) => item) 
    } 
    .listDirection(Axis.Horizontal) 
  
    ...... 
 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.

1. 滚动联动

绑定滚动视图控制器

topRightScroller: Scroller = new Scroller() // 顶部区域左右滚动List 
fullScroller: Scroller = new Scroller() // 内容区域右侧上下滚动List 
tempScroller: Scroller = new Scroller() // 内容区域左侧上下滚动List 
hScroller: Scroller = new Scroller() // 内容区域右侧左右滚动Scroll 
@State scrollOffset: number = 0  // 监听hScroller 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

内容区域左右联动

内容区域左侧List属性回调,联动右侧List上下滚动同步

.onScrollFrameBegin((offset: number, state: ScrollState) => { 
      this.fullScroller.scrollTo({ 
        xOffset: 0, 
        yOffset: this.tempScroller.currentOffset().yOffset + offset, 
        animation: false 
      }) 
      return { offsetRemain: offset } 
    }) 
 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

内容区域右侧List属性回调,联动左侧List上下滚动同步

.onScrollFrameBegin((offset: number, state: ScrollState) => { 
        this.tempScroller.scrollTo({ 
          xOffset: 0, 
          yOffset: this.fullScroller.currentOffset().yOffset + offset, 
          animation: false 
        }) 
        return { offsetRemain: offset } 
      }) 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

内容区域左右滚动Scroll联动顶部固定位置右侧List滚动

.onScroll((scrollOffset: number, scrollState: ScrollState) => { 
      console.log('fxm' + scrollOffset) 
      this.rightHeaderOffset = this.hScroller.currentOffset().xOffset 
      this.topRightScroller.scrollTo({ xOffset: this.hScroller.currentOffset().xOffset, yOffset: 0 }) 
    }) 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

核心代码

// xxx.ets 
import { LightWeightMap } from '@kit.ArkTS' 
const LeftItemWidth = 100 
const RightItemWidth = 100 
  
@Component 
struct ItemComponent { 
  private title: string = '' 
  private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 
  private scroller: Scroller = new Scroller() 
  private static instance: ItemComponent; 
  
  public static getContext(): ItemComponent { 
    if (!ItemComponent.instance) { 
      ItemComponent.instance = new ItemComponent(); 
    } 
    return ItemComponent.instance; 
  } 
  @Builder 
  RightSingleLineList() { 
    List({ scroller: this.scroller }) { 
      ForEach(this.arr, (item: string, index: number) => { 
        ListItem() { 
          Text('' + index) 
            .height(100) 
            .width(RightItemWidth) 
            .fontSize(16) 
            .textAlign(TextAlign.Start) 
            .borderRadius(0) 
            .padding(10) 
            .backgroundColor(0xFFFFFF) 
        } 
      }, (item: string) => item) 
    } 
    .height('100%') 
    .width('100%') 
    .layoutWeight(1) 
    .listDirection(Axis.Horizontal) 
    .scrollBar(BarState.Off) 
    .friction(0.6) 
    .nestedScroll({ 
      scrollForward: NestedScrollMode.PARENT_FIRST, 
      scrollBackward: NestedScrollMode.PARENT_FIRST 
    }) 
    .divider({ strokeWidth: 0.5, color: 0xeeeeee }) // 每行之间的分界线 
    .edgeEffect(EdgeEffect.None) // 边缘效果设置为Spring 
    .onScroll((scrollOffset: number, scrollState: ScrollState) => { 
      console.log('list scroll' + this.scroller.currentOffset().yOffset.toString()) 
    }) 
    .onScrollStop(() => { 
    }) 
  } 
  build() { 
    Column(){ 
      this.RightSingleLineList() 
      Line().width("100%").height(0.5).backgroundColor(0xeeeeee) 
    }.height(100) 
  } 
} 
  
  
@Entry 
@Component 
struct ListExample { 
  private scrollerMap: LightWeightMap<Object, Scroller> = new LightWeightMap(); 
  private leftGroupArr: string[] = ['基本信息', '车身', '发动机'] 
  private topRightArr: string[] = ['车型1', '车型2', '车型3', '车型4', '车型5', '车型6', '车型7', '车型8', '车型9', '车型10'] 
  @State arr: string[] = ['车长', '车高', '内饰', '轮毂', '系统', '电机', '马力', '音响', '悬架', '座椅通风', '自动泊车', '后备箱容积', '座椅记忆'] 
  @State scrollOffset: number = 0 
  fullScroller: Scroller = new Scroller() 
  hScroller: Scroller = new Scroller() 
  tempScroller: Scroller = new Scroller() 
  topRightScroller: Scroller = new Scroller() 
  @State rightHeaderOffset: number = 0 
  
  build() { 
    Column() { 
      this.contentBuilder() 
    } 
  } 
  
  @Builder 
  leftStickyHeader(title: string) { 
    Column() { 
      Text(title) 
        .height('100%') 
        .textAlign(TextAlign.Start) 
        .padding(10) 
        .width('100%') 
        .fontSize(15) 
        .fontWeight(FontWeight.Bolder) 
    }.height(60).width('100%').backgroundColor(0xEEEEEE) 
  } 
  
  @Builder 
  rightStickyHeader(index: number) { 
    Column() { 
      Text(index == 0 ? "官方配置>" : '●标配 ○选配 —无').height('100%') 
        .position({ x: 100 + this.rightHeaderOffset, y: 0 }) 
        .textAlign(TextAlign.End) 
        .width(150) 
    }.height(60).width('100%').backgroundColor(0xEEEEEE) 
  } 
  
  // 顶部固定 
  @Builder 
  topFixed() { 
    Row() { 
      Column() { 
        Text('共11款车').fontSize(15) 
        Toggle({ type: ToggleType.Switch, isOn: false }).height(30).width(50) 
        Text('只看差异').fontSize(15).fontWeight(FontWeight.Bolder) 
      } 
      .width(LeftItemWidth) 
      .height(100) 
      .backgroundColor(Color.White) 
      .justifyContent(FlexAlign.Center) 
      .alignItems(HorizontalAlign.Start) 
      .padding(10) 
  
      // 分割线 
      Line().height(100).width(0.5).backgroundColor(0xeeeeee) 
      // 对比列表 
      List({ scroller: this.topRightScroller }) { 
        ForEach(this.topRightArr, (item: string, index: number) => { 
          ListItem() { 
            Text(item) 
              .height(100) 
              .width(RightItemWidth) 
              .fontSize(16) 
              .textAlign(TextAlign.Start) 
              .borderRadius(0) 
              .padding(10) 
              .backgroundColor(0xFFFFFF) 
          } 
        }, (item: string) => item) 
      }.listDirection(Axis.Horizontal) 
      .divider({ strokeWidth: 0.5, color: 0xeeeeee }) 
      .scrollBar(BarState.Off) 
    }.height(100).width('100%').backgroundColor(Color.Pink) 
  
  } 
  
  @Builder 
  leftList(){ 
    List({scroller: this.tempScroller }) { 
      ForEach(this.leftGroupArr, (item: string, index: number) => { 
        ListItemGroup({ header: this.leftStickyHeader(item) }) { 
          ForEach(this.arr, (item: string, index: number) => { 
            ListItem() { 
              Column(){ 
                Text(item) 
                  .width(LeftItemWidth) 
                  .height('100%') 
                  .fontSize(16) 
                  .textAlign(TextAlign.Start) 
                  .backgroundColor(0xFFFFFF) 
                  .padding(10) 
                  .layoutWeight(1) 
                Line().width("100%").height(0.5).backgroundColor(0xeeeeee) 
              } 
            } 
            .height(100) 
          }, (item: string) => item) 
        } 
      }) 
    } 
    .sticky(StickyStyle.Header) 
    .listDirection(Axis.Vertical) // 排列方向 
    .scrollBar(BarState.Off) 
    .friction(0.6) 
    .divider({ strokeWidth: 0, color: 0xdddddd }) 
    .edgeEffect(EdgeEffect.None) // 边缘效果设置为Spring 
    .nestedScroll({ scrollForward: NestedScrollMode.PARENT_FIRST, scrollBackward: NestedScrollMode.PARENT_FIRST }) 
    .width(LeftItemWidth) 
    .visibility(Visibility.Visible) 
    .onScrollFrameBegin((offset: number, state: ScrollState) => { 
      this.fullScroller.scrollTo({ 
        xOffset: 0, 
        yOffset: this.tempScroller.currentOffset().yOffset + offset, 
        animation: false 
      }) 
      return { offsetRemain: offset } 
    }) 
  } 
  
  @Builder 
  rightScroll(){ 
    Scroll(this.hScroller) { 
      List({ initialIndex: 0, scroller: this.fullScroller }) { 
        ForEach(this.leftGroupArr, (item: string, index: number) => { 
          ListItemGroup({ header: this.rightStickyHeader(index) }) { 
            ForEach(this.arr, (item: string, index: number) => { 
              ItemComponent({ title: this.arr[index] }) 
            }, (item: string) => item) 
          } 
        }) 
      } 
      .sticky(StickyStyle.Header) 
      .listDirection(Axis.Vertical) 
      .scrollBar(BarState.Off) 
      .friction(0.6) 
      .edgeEffect(EdgeEffect.None) 
      .width('100%') 
      .nestedScroll({ scrollForward: NestedScrollMode.PARENT_FIRST, scrollBackward: NestedScrollMode.PARENT_FIRST }) 
      .onScrollFrameBegin((offset: number, state: ScrollState) => { 
        this.tempScroller.scrollTo({ 
          xOffset: 0, 
          yOffset: this.fullScroller.currentOffset().yOffset + offset, 
          animation: false 
        }) 
        return { offsetRemain: offset } 
      }) 
    } 
    .position({ x: LeftItemWidth, y: 0 }) 
    .onScroll((scrollOffset: number, scrollState: ScrollState) => { 
      console.log('fxm' + scrollOffset) 
      this.rightHeaderOffset = this.hScroller.currentOffset().xOffset 
      this.topRightScroller.scrollTo({ xOffset: this.hScroller.currentOffset().xOffset, yOffset: 0 }) 
    }) 
    .edgeEffect(EdgeEffect.None) // 边缘效果设置为Spring 
    .nestedScroll({ scrollForward: NestedScrollMode.PARENT_FIRST, scrollBackward: NestedScrollMode.PARENT_FIRST }) 
    .width(RightItemWidth * 10 + 9 * 0.5) 
    .scrollable(ScrollDirection.Horizontal) 
    .backgroundColor(0xDCDCDC) 
    .padding({ top: 0 }) 
  
  } 
  
  @Builder 
  contentBuilder() { 
    Column() { 
      this.topFixed() 
      Row() { 
        this.leftList() 
        this.rightScroll() 
        Line().height('100%').width(0.5).backgroundColor('#EEEEEE').position({x:LeftItemWidth}) 
      }.justifyContent(FlexAlign.Start) 
      .alignItems(VerticalAlign.Top) 
    }.height('100%').justifyContent(FlexAlign.Start).alignItems(HorizontalAlign.Start) 
  } 
} 
 
  • 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.
  • 195.
  • 196.
  • 197.
  • 198.
  • 199.
  • 200.
  • 201.
  • 202.
  • 203.
  • 204.
  • 205.
  • 206.
  • 207.
  • 208.
  • 209.
  • 210.
  • 211.
  • 212.
  • 213.
  • 214.
  • 215.
  • 216.
  • 217.
  • 218.
  • 219.
  • 220.
  • 221.
  • 222.
  • 223.
  • 224.
  • 225.
  • 226.
  • 227.
  • 228.
  • 229.
  • 230.
  • 231.
  • 232.
  • 233.
  • 234.
  • 235.
  • 236.
  • 237.
  • 238.
  • 239.
  • 240.
  • 241.
  • 242.
  • 243.

实现效果

分享
微博
QQ
微信
回复1
2024-05-27 20:54:33
相关问题
HarmonyOS 图片放大后的滚动联动问题
650浏览 • 1回复 待解决
如何获取List组件滚动滚动的距离
3441浏览 • 1回复 待解决
list 支持循环滚动吗?
2847浏览 • 1回复 待解决
HarmonyOS 自动横向滚动List
728浏览 • 1回复 待解决
HarmonyOS中的设备联动如何实现?
1002浏览 • 0回复 待解决
HarmonyOS List组件的滚动监听
804浏览 • 1回复 待解决
HarmonyOS scroll和list滚动冲突
1106浏览 • 1回复 待解决
HarmonyOS List联动滑动
569浏览 • 1回复 待解决
HarmonyOS list组件点击后,滚动居中
719浏览 • 1回复 待解决
list组件无法滚动到底部
2223浏览 • 1回复 待解决