HarmonyOS 拖动页面listview上下几条ite收缩的动效实现

现在需求是两个listview向上慢滑动整体view时,第一个listview向指定的item收缩,比如第一个listview有6条,整体向上滑动的同时,第四条置顶,上三条和下两条滑动消失,想请教一下有什么api能实现这样的效果,或者这种需要怎么去实现?

HarmonyOS
2024-10-16 11:35:03
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

1、经分析,如果需要实现滑动折叠的效果,需要拆分第一个listview,使用双滚轮实现目的。

2、如果可以接受动画折叠,可以使用显式动画实现。

参考代码如下:

//1、index.ets  
@Entry  
@Component  
struct Index {  
  private scrollerForScroll: Scroller = new Scroller();  
  private scrollerForList: Scroller = new Scroller();  
  @State numbers1: string[] = ['1', '2', '3', '4', '5']  
  @State numbers2: string[] = ['1', '2', '3', '4', '5', '6', '7']  
  @State curSelectedRow: number = 2;  
  @State curSelectedColumn: number = 3;  
  @State textHeight: number = 50;  
  
  getTopNums() {  
    return this.numbers1.filter((v: string, index1: number) => {  
      return index1 <= this.curSelectedRow;  
    })  
  }  
  getBottomNums() {  
    return this.numbers1.filter((v: string, index1: number) => {  
      return index1 > this.curSelectedRow;  
    })  
  }  
  @Builder  
  myBuilder(number1: string[]) {  
    Column() {  
      ForEach(number1, (v: string) => {  
        Flex({ justifyContent: FlexAlign.SpaceBetween, wrap: FlexWrap.NoWrap }) {  
          ForEach(this.numbers2, (item: string, index2: number) => {  
            Text(v + item)  
              .width(this.textHeight)  
              .height(this.textHeight)  
              .textAlign(TextAlign.Center)  
              .borderRadius(40)  
              .borderWidth(this.curSelectedRow === Number(v) - 1 && this.curSelectedColumn === index2 ? 1 : 0)  
              .onClick(() => {  
                this.curSelectedRow = Number(v) - 1;  
                this.curSelectedColumn = index2;  
              })  
          })  
        }  
        .borderWidth({ bottom: 1 })  
        .width('100%')  
        .backgroundColor(Color.Pink)  
      })  
    }  
  }  
  build() {  
    Stack({ alignContent: Alignment.Top }) {  
      Scroll(this.scrollerForScroll) {  
        Column() {  
          // list view1  
          this.myBuilder(this.getTopNums());  
          List({ space: 10, scroller: this.scrollerForList }) {  
            ListItem() {  
              // list view2  
              this.myBuilder(this.getBottomNums());  
            }.width("100%")  
            ListItem() {  
              Column() {  
                Text('content top')  
                Text('content bottom')  
              }  
              .justifyContent(FlexAlign.SpaceBetween)  
              .height('100%')  
              .width('100%')  
              .backgroundColor(Color.White)  
              .borderRadius(10)  
            }.width("100%").height('100%')  
          }  
          .edgeEffect(EdgeEffect.None)  
          .scrollBar(BarState.Off)  
          .nestedScroll({ scrollForward: NestedScrollMode.PARENT_FIRST, scrollBackward: NestedScrollMode.SELF_FIRST })  
          .width("100%")  
          // 吸顶效果  
          .height(`calc(100% - ${this.textHeight}vp)`)  
        }  
        .padding({ left: 10, right: 10, bottom: 20 })  
      }  
      .scrollBar(BarState.Off)  
      .width("100%")  
      .height("100%")  
      .onScrollFrameBegin(offset => {  
        if (this.scrollerForList.currentOffset().yOffset > 0) {  
          this.scrollerForList.scrollBy(0, offset);  
          return { offsetRemain: 0 };  
        }  
        return { offsetRemain: offset };  
      })  
    }  
    .width('100%')  
    .height('100%')  
    .backgroundColor(Color.Gray)  
  }  
}
//2、index.ets  
@Entry  
@Component  
struct Index {  
  private numRows: string[] = ['1', '2', '3', '4', '5'];  
  private numColumns: string[] = ['1', '2', '3', '4', '5', '6', '7'];  
  @State btnText: string = '收缩';  
  @State isExpand: boolean = true;  
  @State curSelectedRow: number = 2;  
  @State curSelectedColumn: number = 3;  
  @State textHeight: number = 40;  
  build() {  
    Column({ space: 10 }) {  
      ForEach(this.numRows, (v: string, index1: number) => {  
        Flex({ justifyContent: FlexAlign.SpaceBetween, wrap: FlexWrap.NoWrap }) {  
          ForEach(this.numColumns, (item: string, index2: number) => {  
            Text(v + item)  
              .width(40)  
              .height(this.curSelectedRow === index1 ? 40 : this.textHeight)  
              .textAlign(TextAlign.Center)  
              .borderRadius(40)  
              .borderWidth(this.curSelectedRow === index1 && this.curSelectedColumn === index2 ? 1 : 0)  
              .onClick(() => {  
                this.curSelectedRow = index1;  
                this.curSelectedColumn = index2;  
              })  
          })  
        }  
        .width('100%')  
        .backgroundColor(Color.Pink)  
      })  
      Button(this.btnText).onClick(() => {  
        this.isExpand = !this.isExpand;  
        this.btnText = this.isExpand ? '收缩' : '展开';  
        animateTo({  
          duration: 300,  
          curve: Curve.Linear  
        }, () => {  
          this.textHeight = this.textHeight == 0 ? 40 : 0  
        })  
      })  
    }.width('100%')  
  }  
}
分享
微博
QQ
微信
回复
2024-10-16 16:56:58
相关问题
页面上下拖动时怎么隐藏键盘
829浏览 • 1回复 待解决
如何设置全屏返回
1908浏览 • 1回复 待解决
鸿蒙jsUi如何制作按钮按下
8495浏览 • 3回复 待解决
如何实现上下切换页面间跳转动画
1871浏览 • 0回复 待解决
HarmonyOS 如何实现拖动改变ListItem顺序
250浏览 • 1回复 待解决
拖动实现列表重新排序
919浏览 • 1回复 待解决
HarmonyOS 加载.json格式图资源
349浏览 • 1回复 待解决
Text文本过长时如何实现上下滚动?
473浏览 • 1回复 待解决
HarmonyOS Grid拖动+删除?
20浏览 • 0回复 待解决