HarmonyOS 鸿蒙Next中通过Swiper实现京东m站功能入口效果

HarmonyOS 鸿蒙Next中通过Swiper实现京东m站功能入口效果 通过Swiper实现京东m站功能入口效果

cke_706.png


更多关于HarmonyOS 鸿蒙Next中通过Swiper实现京东m站功能入口效果的实战教程也可以访问 https://www.itying.com/category-93-b0.html

4 回复

效果预览:

cke_23327.png

实现思路:

  1. 通过线性布局Row的linearGradient属性实现渐变背景
Row() {
  // 更多代码
}.width('100%').justifyContent(FlexAlign.Center).linearGradient({
  direction: GradientDirection.Bottom, // 渐变方向
  repeating: true, // 渐变颜色是否重复
  colors: [['#ff5454', 0.0], ['#fffff', 0.3], ['#fff', 0.7], ['#fff', 1.0]] // 数组末尾元素占比小于1时满足重复着色效果
}).padding(10)
  1. 在线性布局里面,给Swiper组件的indicator属性自定义导航点的位置和样式。
Swiper() {
  Row() {
  }

  Row() {
  }
}
.width('94%')
.borderRadius(10)
.backgroundColor(Color.White)
.padding({ top: 10 })
.indicator(
  Indicator.dot()
    .top(50)
    .space(LengthMetrics.vp(0))
    .itemWidth(15)
    .selectedItemWidth(15)
    .selectedColor('#fa2c19')
    .color('#f0f0f0')
)

indicator属性手册 https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/arkts-layout-development-create-looping#导航点样式

3.配置网络权限module.json5(因为图片用的互联网地址)

{
  "module": {
    // ...
    "requestPermissions": [
      {"name":  "ohos.permission.INTERNET"}
    ],
    // ...
  }
}

完整代码

@Entry
@Component
struct Index {
  build() {
    Row() {
      Swiper() {
        Row() {
          Column() {
            Image('https://img12.360buyimg.com/babel/jfs/t20270715/38278/23/22574/7960/6694edb4F07db03e3/d663cd498321eadc.png')
              .width(35)
            Text('京东超市').fontSize(12).margin({ top: 10 }).fontColor('#595959')
          }.layoutWeight(1).height(80).backgroundColor(Color.White)

          Column() {
            Image('https://m.360buyimg.com/babel/jfs/t20270715/237082/37/21845/7616/6694edddFc764124a/38d00b686257b0f4.png')
              .width(35)
            Text('京东电器').fontSize(12).margin({ top: 10 }).fontColor('#595959')
          }.layoutWeight(1).height(80).backgroundColor(Color.White)

          Column() {
            Image('https://img20.360buyimg.com/babel/jfs/t20270715/36751/25/21385/7651/6694ee02F878cddef/13ce837dd39ad1ad.png')
              .width(35)
            Text('服饰美妆').fontSize(12).margin({ top: 10 }).fontColor('#595959')
          }.layoutWeight(1).height(80).backgroundColor(Color.White)

          Column() {
            Image('https://img20.360buyimg.com/babel/jfs/t20270715/44839/8/24550/7935/6694ee27F8775a577/b63c6a2fa0327964.png')
              .width(35)
            Text('充值中心').fontSize(12).margin({ top: 10 }).fontColor('#595959')
          }.layoutWeight(1).height(80).backgroundColor(Color.White)

          Column() {
            Image('https://img14.360buyimg.com/babel/jfs/t20270715/243181/3/13649/9018/6694ee5fF6aa391d4/1b020aa3f9cf89a0.png')
              .width(35)
            Text('PLUS会员').fontSize(12).margin({ top: 10 }).fontColor('#595959')
          }.layoutWeight(1).height(80).backgroundColor(Color.White)
        }

        Row() {
          Column() {
            Image('https://m.360buyimg.com/babel/jfs/t20270715/22456/27/20943/10381/6694ee81F684396bb/0ba51f592d28dfdd.png')
              .width(35)
            Text('京东生鲜').fontSize(12).margin({ top: 10 }).fontColor('#595959')
          }.layoutWeight(1).height(80).backgroundColor(Color.White)

          Column() {
            Image('https://img11.360buyimg.com/babel/jfs/t20270715/29760/28/21267/11992/6694eea3F0fe3dca2/d5672661722bfc42.png')
              .width(35)
            Text('京东国际').fontSize(12).margin({ top: 10 }).fontColor('#595959')
          }.layoutWeight(1).height(80).backgroundColor(Color.White)

          Column() {
            Image('https://img10.360buyimg.com/babel/jfs/t20270715/233990/3/23983/8102/6694eec4F2aad82cf/2144631769da49b9.png')
              .width(35)
            Text('京东拍卖').fontSize(12).margin({ top: 10 }).fontColor('#595959')
          }.layoutWeight(1).height(80).backgroundColor(Color.White)

          Column() {
            Image('https://img20.360buyimg.com/babel/jfs/t20270926/241563/14/18702/9401/66f4bc8aFc8e6d309/ed7c86f700aba111.png')
              .width(35)
            Text('红包惊喜').fontSize(12).margin({ top: 10 }).fontColor('#595959')
          }.layoutWeight(1).height(80).backgroundColor(Color.White)

          Column() {
            Image('https://img14.360buyimg.com/babel/jfs/t20270715/42046/6/20985/8690/6694ef01Ff3769032/bfa11aada78ce515.png')
              .width(35)
            Text('全部').fontSize(12).margin({ top: 10 }).fontColor('#595959')
          }.layoutWeight(1).height(80).backgroundColor(Color.White)
        }
      }
      .width('94%')
      .borderRadius(10)
      .backgroundColor(Color.White)
      .padding({ top: 10 })
      .indicator(
        Indicator.dot()
          .top(50)
          .itemWidth(15)
          .selectedItemWidth(15)
          .selectedColor('#fa2c19')
      )
    }.width('100%').justifyContent(FlexAlign.Center).linearGradient({
      direction: GradientDirection.Bottom, // 渐变方向
      repeating: true, // 渐变颜色是否重复
      colors: [['#ff5454', 0.0], ['#fffff', 0.3], ['#fff', 0.7], ['#fff', 1.0]] // 数组末尾元素占比小于1时满足重复着色效果
    }).padding(10)
  }
}

更多关于HarmonyOS 鸿蒙Next中通过Swiper实现京东m站功能入口效果的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


如何查看Linux系统版本信息

1. 使用 lsb_release 命令

lsb_release 命令是查看系统版本信息的常用工具。

lsb_release -a

输出示例:

No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.3 LTS
Release:        20.04
Codename:       focal

2. 查看 /etc/os-release 文件

该文件包含系统发行版信息。

cat /etc/os-release

输出示例:

NAME="Ubuntu"
VERSION="20.04.3 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.3 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal

3. 查看 /etc/issue 文件

该文件包含系统登录前的版本信息。

cat /etc/issue

输出示例:

Ubuntu 20.04.3 LTS \n \l

4. 使用 uname 命令

uname 命令用于查看内核版本信息。

uname -a

输出示例:

Linux ubuntu-server 5.4.0-91-generic #102-Ubuntu SMP Fri Nov 5 16:31:28 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

5. 查看 /proc/version 文件

该文件包含内核版本和编译信息。

cat /proc/version

输出示例:

Linux version 5.4.0-91-generic (buildd@lgw01-amd64-060) (gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)) #102-Ubuntu SMP Fri Nov 5 16:31:28 UTC 2021

6. 使用 hostnamectl 命令

该命令用于查看系统主机名和系统信息。

hostnamectl

输出示例:

   Static hostname: ubuntu-server
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 1234567890abcdef1234567890abcdef
           Boot ID: abcdef1234567890abcdef1234567890
    Virtualization: kvm
  Operating System: Ubuntu 20.04.3 LTS
            Kernel: Linux 5.4.0-91-generic
      Architecture: x86-64

总结

以上方法可以帮助您快速查看Linux系统的版本和内核信息。根据不同的发行版,某些命令可能不可用,但大多数现代Linux发行版都支持这些方法。

在HarmonyOS Next中,通过Swiper组件可实现类似京东M站的功能入口轮播效果。使用Swiper的loop属性开启循环播放,设置indicator属性自定义指示器样式,结合Image和Text组件展示入口图标与文字。通过SwiperController控制自动轮播与交互切换。

在HarmonyOS Next中,使用Swiper组件实现类似京东M站功能入口的轮播效果,核心在于结合Swiper的循环播放、分页指示器以及自定义子项布局。以下是关键实现思路和代码示例:

1. 布局设计

使用Swiper作为容器,每个子项为一个RowGrid,内部放置多个功能入口(通常用Column包含图标和文本)。

2. 关键属性配置

  • loop(true):启用循环播放。
  • indicator:设置分页指示器样式。
  • duration(500):设置切换动画时长。
  • vertical(false):横向滚动(默认)。

3. 示例代码

// 示例数据
const functionEntries = [
  [
    { icon: $r('app.media.icon1'), text: '功能1' },
    { icon: $r('app.media.icon2'), text: '功能2' },
    // ... 每页最多8个入口
  ],
  // ... 更多页数据
];

@Entry
@Component
struct FunctionEntrySwiper {
  build() {
    Swiper() {
      ForEach(functionEntries, (page, pageIndex) => {
        SwiperItem() {
          Grid() {
            ForEach(page, (item) => {
              GridItem() {
                Column() {
                  Image(item.icon)
                    .width(40)
                    .height(40)
                  Text(item.text)
                    .fontSize(12)
                }
                .justifyContent(FlexAlign.Center)
              }
            })
          }
          .columnsTemplate('1fr 1fr 1fr 1fr') // 每行4个
          .rowsTemplate('1fr 1fr') // 每页2行
          .layoutDirection(GridDirection.Row)
        }
      })
    }
    .loop(true)
    .indicator(new CircleIndicator()) // 圆形指示器
    .duration(500)
  }
}

4. 优化建议

  • 使用cachedCount预加载相邻页提升性能。
  • 通过onChange事件监听页面切换,可同步更新指示器或触发其他逻辑。
  • 若需自动轮播,结合setInterval控制SwiperController切换。

此方案能高效实现类京东M站的功能入口布局,兼顾流畅交互与视觉一致性。

回到顶部