HarmonyOS 是否有新闻频道管理的案例

管理频道功能最上面是已选频道模块,下面的都是未选模块,下面各个模块的可以移动添加到已选模块,已选模块删除会移动回原来的所属模块。

HarmonyOS
2024-12-25 12:46:53
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Heiang

参考示例:

import curves from '@ohos.curves';

// import { ArrayList, List as A } from '@kit.ArkTS';
@Entry
@Component
struct TPage {
  @State numbers0: String[] = []
  @State numbers: String[] =
    ["娱乐", "关注", "热榜", "航天", "动漫", "宠物", "保险", "小说", "汽车", "财经", "体育", "女性", "搞笑", "军事",
      "有料", "北京", "深度", "电影"]
  scroller: Scroller = new Scroller()
  @State text: string = 'drag'
  @State isShowDelete: boolean = false
  @State isEdit: boolean = false
  @State rotateZ: number = 0;
  
  private stopJump() {
    animateTo({
      delay: 0,
      tempo: 5,
      duration: 0,
      curve: Curve.Smooth,
      playMode: PlayMode.Normal,
      iterations: 1

    },
      () => {
        this.rotateZ = 0;
      })
  }

  //抖动的动画
  private jumpWithSpeed(speed: number) {
    if (this.isEdit) {
      this.rotateZ = -1;
      animateTo({
        delay: 0, //延时播放
        tempo: speed, //动画的播放速度,值越大动画播放越快
        duration: 1000, //动画持续的时间
        curve: Curve.Smooth, //动画曲线
        playMode: PlayMode.Normal, //动画播放模式
        iterations: -1

      },
        () => {
          this.rotateZ = 1;

        })
    } else {
      this.stopJump()
    }
  }

  //拖拽过程中展示的样式
  @Builder
  pixelMapBuilder() {
    Column() {
      Text(this.text)
        .fontSize(16)
        .backgroundColor(0xF9CF93)
        .width(80)
        .height(40)
        .textAlign(TextAlign.Center)
        .borderRadius(20)
        .borderWidth(2)
        .borderColor(Color.Orange)
    }
  }

  //改变数组中元素位置
  changeIndex(index1: number, index2: number) {
    this.numbers0.splice(index2, 0, this.numbers0.splice(index1, 1)[0])

  }

  build() {
    Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.SpaceAround }) {

      Column() {
        Row({ space: 30 }) {
          Text("我的频道")
            .fontSize(20)
          Text(this.isEdit ? "长按拖动调整顺序" : "点击编辑按钮进入编辑")
            .fontColor(Color.Gray)
          Button() {
            Text(this.isEdit ? "完成" : "编辑")
              .fontColor(this.isEdit ? Color.Black : 0xF9CF93)
          }.width(80)
          .height(30)
          .backgroundColor(Color.White)
          .onClick(() => {
            this.isEdit = !this.isEdit
            this.jumpWithSpeed(5)
          })
        }.padding({ left: 15 })


        Grid(this.scroller) {
          ForEach(this.numbers0, (day: string, index: number) => {
            GridItem() {
              Stack({ alignContent: Alignment.TopEnd }) {
                Text(day)
                  .borderRadius(20)
                  .borderWidth(2)
                  .borderColor(Color.Orange)
                  .fontSize(16)
                  .backgroundColor(0xF9CF93)
                  .width(80)
                  .height(40)
                  .textAlign(TextAlign.Center)
                  .onTouch((event: TouchEvent) => {
                    if (event.type === TouchType.Down) {
                      this.text = day
                    }
                  })
                if (this.isEdit) {
                  Image($r('app.media.close'))
                    .width(20)
                    .height(20)
                    .onClick(() => {
                      animateTo({ duration: 300 }, () => {
                        //移除上部分频道,将移除上部加入下部分频道
                        this.numbers.splice(this.numbers.length, 0, this.numbers0[index])
                        this.numbers0.splice(index, 1)
                      })
                      this.stopJump()

                      this.jumpWithSpeed(5)
                    })
                }
              }
              .rotate({
                z: this.rotateZ,
                angle: 0.4,
                centerX: 0.5,
                centerY: 0.5
              })
            }
            .transition({ type: TransitionType.All, translate: { x: 100 }, scale: { x: 1, y: 1 } })
            .padding({ top: 15 })

          })
        }
        .columnsTemplate('1fr 1fr 1fr')
        .columnsGap(10)
        .rowsGap(10)
        .onScrollIndex((first: number) => {
          console.info(first.toString())
        })
        .margin({ top: 5 })
        .width('100%')
        .backgroundColor(0xFAEEE0)
        .height('100%')
        .supportAnimation(true) //是否支持动画
        //设置Grid是否进入编辑模式,进入编辑模式可以拖拽Grid组件内部GridItem
        .editMode(this.isEdit)
        //第一次拖拽此事件绑定的组件时,触发回调
        .onItemDragStart((event: ItemDragInfo, itemIndex: number) => {
          //设置拖拽过程中显示的图片
          return this.pixelMapBuilder()
        })
        //绑定此事件的组件可作为拖拽释放目标,当在本组件范围内停止拖拽行为时,触发回调
        //itemIndex为拖拽起始位置,insertIndex为拖拽插入位置
        .onItemDrop((event: ItemDragInfo, itemIndex: number, insertIndex: number, isSuccess: boolean) => {
          //不支持拖拽到已有内容以外的位置
          if (insertIndex < this.numbers.length) {
            this.changeIndex(itemIndex, insertIndex)
            this.stopJump()
            this.jumpWithSpeed(5)
          }
        })
      }
      .width('100%')
      .height('40%')

      Column() {
        Row() {
          Text("所有频道")
            .fontSize(20)
        }.padding({ left: 15 })
        .width("100%")
        .justifyContent(FlexAlign.Start)

        Grid(this.scroller) {
          ForEach(this.numbers, (day: string, index: number) => {
            GridItem() {
              Stack({ alignContent: Alignment.TopEnd }) {
                Text(day)
                  .borderRadius(20)
                  .borderWidth(2)
                  .borderColor(Color.Orange)
                  .fontSize(16)
                  .backgroundColor(0xF9CF93)
                  .width(80)
                  .height(40)
                  .textAlign(TextAlign.Center)
                  .onClick(() => {
                    animateTo({ duration: 300 }, () => {
                      //移除上部分频道,将移除上部加入下部分频道
                      this.numbers0.splice(this.numbers0.length, 0, this.numbers[index])
                      this.numbers.splice(index, 1)
                    })
                    this.stopJump()

                    this.jumpWithSpeed(5)
                  })
                // }
              }
              .rotate({
                z: this.rotateZ,
                angle: 0.4,
                centerX: 0.5,
                centerY: 0.5

              })

            }
            .transition({ type: TransitionType.All, translate: { x: 100 }, scale: { x: 1, y: 1 } })
            .padding({ top: 15 })

          })
        }
        .columnsTemplate('1fr 1fr 1fr')
        .columnsGap(10)
        .rowsGap(10)
        .onScrollIndex((first: number) => {
          console.info(first.toString())
        })
        .margin({ top: 5 })
        .width('100%')
        .backgroundColor(0xFAEEE0)
        .height('100%')
      }
      .width('100%')
      .height('40%')
    }.width('100%')
    .margin({ top: 5 })
  }
}
  • 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.
分享
微博
QQ
微信
回复
2024-12-25 15:25:52


相关问题
监听屏幕旋转案例哪些
1108浏览 • 1回复 待解决
OceanBase业务案例哪些?
4539浏览 • 1回复 待解决
HarmonyOS 新闻相关DEMO参考
679浏览 • 1回复 待解决
HarmonyOS 需要频道导航栏功能样例
810浏览 • 1回复 待解决
HarmonyOS 新闻类app demo
769浏览 • 2回复 待解决
HarmonyOS 提供新闻样板间代码
484浏览 • 1回复 待解决
HarmonyOS 获取经纬案例
785浏览 • 1回复 待解决
HarmonyOS 快速搭建案例
857浏览 • 1回复 待解决
ETS语言 文件管理了解吗?
3419浏览 • 1回复 待解决