如何实现列表既可以左右滑、又可以上下滑动

列表既可以左右滑动,又可以上下滑动

HarmonyOS
2024-06-11 23:32:56
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
liqi399

List+Scroll+gesture,List实现竖向滚动及吸顶,Scroll+gesture实现横向滚动。

参考代码:

@Entry 
@Component 
struct ScrollExample { 
  private panOption: PanGestureOptions = new PanGestureOptions({ direction: PanDirection.Left | PanDirection.Right }) 
  @Provide offsetX: number | undefined = undefined; 
 
  @Builder 
  topArea() { 
    tableRow().margin({right: 100}) 
  } 
 
  build() { 
    Column() { 
      // 顶部 
      this.topArea() 
      // 主体 
      MainArea().margin({bottom:100}) 
    } 
    .backgroundColor(Color.Orange) 
    .gesture( 
      // 用于触发拖动手势事件,滑动的最小距离为5vp时拖动手势识别成功。 
      PanGesture(this.panOption).onActionStart((event?: GestureEvent) => { 
        this.offsetX = event?.offsetX 
        console.info('gesture11: PanGesture start' + event?.offsetX) 
      }) 
    ) 
  } 
} 
@Component 
struct MainArea { 
  private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 
 
  // table header 
  @Builder 
  tabHeader() { 
    Stack() { 
      Row() { 
        Text('1111111').fontSize(20).width(100).height(100) 
        Blank() 
        Text('1111110').fontSize(20).width(100).height(100) 
      }.width('100%').backgroundColor(Color.Gray) 
    } 
  } 
 
  build() { 
    List() { 
      ListItemGroup({ header: this.tabHeader() }) { 
        ForEach(this.arr, (item: number) => { 
          ListItem() { 
            tableRow() 
          }.margin({right: 100}) 
        }, (item: number) => item.toString()) 
      } 
    }.sticky(StickyStyle.Header) 
  } 
} 
// table row 
@Component 
struct tableRow { 
  build() { 
    Row() { 
      Text('111111').fontSize(20).width(100).height(100) 
      ScrollComponent() 
    }.width('100%') 
  } 
} 
 
@Component 
struct ScrollComponent { 
  scroller: Scroller = new Scroller() 
  @Consume @Watch('onCountUpdated') offsetX: number; 
 
  //监听滑动距离 
  onCountUpdated() { 
    if (this.offsetX) { 
      this.scroller.scrollTo({ 
        xOffset: this.scroller.currentOffset()?.xOffset - this.offsetX * 50, //滑动距离可根据实际情况设置 
        yOffset: 0, 
        animation: { duration: 1000 } 
      }) 
    } 
  } 
 
  build() { 
    Scroll(this.scroller) { 
      Row() { 
        Text('111111').fontSize(20).width(100).height(100) 
        Text('111113').fontSize(20).width(100).height(100) 
        Text('111114').fontSize(20).width(100).height(100) 
        Text('111115').fontSize(20).width(100).height(100) 
        Text('111116').fontSize(20).width(100).height(100) 
        Text('111117').fontSize(20).width(100).height(100) 
        Text('111118').fontSize(20).width(100).height(100) 
        Text('111119').fontSize(20).width(100).height(100) 
        Text('1111110').fontSize(20).width(100).height(100) 
        Text('1111111').fontSize(20).width(100).height(100) 
      } 
    } 
    .scrollable(ScrollDirection.Horizontal) // 滚动方向纵向 
    .scrollBar(BarState.Off) // 滚动条常驻显示 
    .edgeEffect(EdgeEffect.None) 
    // 禁止scroll滚动 
    .enabled(false) 
  } 
}
  • 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.
分享
微博
QQ
微信
回复
2024-06-12 23:44:21


相关问题
服务卡片可以响应上下滑动操作吗?
7393浏览 • 2回复 待解决
键盘拉起时列表无法上下滑动
2497浏览 • 1回复 待解决