鸿蒙开发:自定义一个搜索模版

程序员一鸣
• 阅读 31

前言

代码案例基于Api13。

在之前的文章中,我们简单分析了弹性布局Flex,并使用Flex实现了一个简单的流式布局,今天这篇文章,我们就结合搜索框,完成一个常见的搜索页面,具体的效果如下图所示:

鸿蒙开发:自定义一个搜索模版

这样的一个模版,可以简单的分为,三个部分,分别是上边的搜索框,中间的历史搜索和下边的热门搜索,搜索框,我们直接可以使用系统的组件Search,历史搜索,由于是内容不一的搜索的内容,这里使用弹性布局Flex,下边的热门搜索,条目规格一致,这里我们直接使用Grid网格组件。

快速使用

目前已经上传到了中心仓库,大家可以直接远程依赖使用,当然,你也可以下载源码进行使用。

远程依赖方式,有两种方式可供选择,分别如下:

方式一:在Terminal窗口中,执行如下命令安装三方包,DevEco Studio会自动在工程的oh-package.json5中自动添加三方包依赖。

建议:在使用的模块路径下进行执行命令。

ohpm install @abner/search

方式二:在工程的oh-package.json5中设置三方包依赖,配置示例如下:

"dependencies": { "@abner/search": "^1.0.0"}

代码使用

总体来说就是一个UI组件,在需要的页面直接调用SearchLayout组件即可,相关代码如下:

import { HotBean, SearchLayout } from '@abner/search';

@Entry
  @Component
  struct Index {
    @State hotList: HotBean[] = []

    aboutToAppear(): void {
      this.hotList.push(new HotBean("程序员一鸣", { bgColor: Color.Red }))
      this.hotList.push(new HotBean("AbnerMing", { bgColor: Color.Orange }))
      this.hotList.push(new HotBean("鸿蒙干货铺", { bgColor: Color.Pink }))
      this.hotList.push(new HotBean("程序员一哥", { bgColor: Color.Gray }))
    }

    build() {
      RelativeContainer() {
        SearchLayout({
          hotList: this.hotList,
          onItemClick: (text: string) => {
            console.log("===条目点击:" + text)
          },
          onSearchAttribute: (attr) => {
            attr.onSubmit = (text) => {
              console.log("===点击搜索:" + text)
            }
          }
        })
          .alignRules({
            center: { anchor: '__container__', align: VerticalAlign.Center },
            middle: { anchor: '__container__', align: HorizontalAlign.Center }
          })
      }
      .height('100%')
        .width('100%')
    }
  }

源码分析

搜索组件

顶部的搜索组件直接使用系统的Search组件,虽然系统的已经满足了大部分的场景,但是考虑到其它的一些特殊场景,这里也暴露了左右两个自定义视图,可以在搜索组件的左右设置自己需要的组件,这里需要注意,关于Search组件的一些属性,我们该暴露的暴露,方便调用者进行使用。

RelativeContainer() {

        Column() {
          if (this.searchLeftView != undefined) {
            this.searchLeftView()
          }
        }.id("search_left")
        .height("100%")
        .justifyContent(FlexAlign.Center)

        Search({
          placeholder: this.searchAttribute.placeholder,
          value: this.searchText,
          icon: this.searchAttribute.icon,
          controller: this.controller
        })
          .placeholderFont(this.searchAttribute.placeholderFont)
          .placeholderColor(this.searchAttribute.placeholderColor)
          .textFont(this.searchAttribute.textFont)
          .textAlign(this.searchAttribute.textAlign)
          .copyOption(this.searchAttribute.copyOption)
          .searchIcon(this.searchAttribute.searchIcon)
          .cancelButton(this.searchAttribute.cancelButton)
          .fontColor(this.searchAttribute.fontColor)
          .caretStyle(this.searchAttribute.caretStyle)
          .enableKeyboardOnFocus(this.searchAttribute.enableKeyboardOnFocus)
          .selectionMenuHidden(this.searchAttribute.selectionMenuHidden)
          .maxLength(this.searchAttribute.maxLength)
          .enterKeyType(this.searchAttribute.enterKeyType)
          .type(this.searchAttribute.type)
          .alignRules({
            left: { anchor: "search_left", align: HorizontalAlign.End },
            right: { anchor: "search_right", align: HorizontalAlign.Start },
            center: { anchor: "__container__", align: VerticalAlign.Center }
          })
          .onSubmit((text: string) => {

            this.submit(text)
          })
          .onChange((value) => {
            this.searchResult = value
          })

        Column() {
          if (this.searchRightView != undefined) {
            this.searchRightView()
          }
        }.id("search_right")
        .height("100%")
        .alignRules({
          center: { anchor: "__container__", align: VerticalAlign.Center },
          right: { anchor: "__container__", align: HorizontalAlign.End }
        }).justifyContent(FlexAlign.Center)

      }.width("100%")
      .height(this.searchAttribute.height)
      .margin(this.searchAttribute.margin)

历史搜索

由于历史搜索的内容,每次的长度是不一样的,这里,我们使用弹性布局Flex来实现,设置wrap属性为FlexWrap.Wrap,支持多行展示。

需要注意的是,历史搜索需要把搜索的记录进行持久化存储,也就是用户退出应用再次进来,还是能够看到之前的搜索记录的,这里使用的用户首选项preferences.Preferences,在用户执行搜索的时候,先用临时变量用于存储,待页面退出的时候,进行存储,页面初始化的时候再进行取出展示。

在实际的开发中,每个历史搜索的条目,需要支持点击,方便调用者执行逻辑,这里需要把条目的点击事件进行暴露。

为了更好的展示UI视图,可以根据当前是否有历史记录进行动态的展示历史搜索组件。

RelativeContainer() {

        Text(this.historyTagAttribute.title)
          .fontColor(this.historyTagAttribute.fontColor)
          .fontSize(this.historyTagAttribute.fontSize)
          .fontWeight(this.historyTagAttribute.fontWeight)

        Image(this.historyTagAttribute.deleteSrc)
          .width(this.historyTagAttribute.imageWidth)
          .width(this.historyTagAttribute.imageHeight)
          .alignRules({
            center: { anchor: "__container__", align: VerticalAlign.Center },
            right: { anchor: "__container__", align: HorizontalAlign.End }
          }).onClick(() => {
          //删除
          this.historyList = []
          PreferencesUtil.getPreferencesUtil().delete("searchList")
        })

      }.margin(this.historyTagAttribute.margin)
      .height(this.historyTagAttribute.height)
      .visibility(this.historyList.length == 0 ? Visibility.None : Visibility.Visible)

      Flex({
        direction: FlexDirection.Row, wrap: FlexWrap.Wrap,
        space: { main: LengthMetrics.vp(10), cross: LengthMetrics.vp(10) }
      }) {
        ForEach(this.historyList, (item: string) => {
          Text(item)
            .padding(this.historyItemAttribute.padding)
            .borderRadius(this.historyItemAttribute.borderRadius)
            .fontColor(this.historyItemAttribute.fontColor)
            .fontSize(this.historyItemAttribute.fontSize)
            .fontWeight(this.historyItemAttribute.fontWeight)
            .backgroundColor(this.historyItemAttribute.backgroundColor)
            .onClick(() => {
              //历史搜索点击
              if (this.onItemClick != undefined) {
                this.onItemClick(item)
              }
            })

        })
      }
      .margin({ top: this.historyViewMarginTop, bottom: this.historyViewMarginBottom })
      .visibility(this.historyList.length == 0 ? Visibility.None : Visibility.Visible)

热门搜索

热门搜索就比较的简单的,就是一个Grid网格列表,需要注意的就是,条目的UI样式,这里使用的对象数据传递,方便前缀的label设置,当然,在实际的开发中,大家可以把子条目的UI暴露出去,由调用者传递,这样针对UI的展示则更加的灵活。

 Text(this.hotTagAttribute.title)
        .fontColor(this.hotTagAttribute.fontColor)
        .fontSize(this.hotTagAttribute.fontSize)
        .fontWeight(this.hotTagAttribute.fontWeight)
        .margin(this.hotTagAttribute.margin)

      Grid() {
        ForEach(this.hotList, (item: HotBean) => {
          GridItem() {
            Row() {
              Text(item.label)
                .backgroundColor(item.labelBgColor)
                .width(this.hotItemAttribute.labelSize)
                .height(this.hotItemAttribute.labelSize)
                .textAlign(TextAlign.Center)
                .borderRadius(this.hotItemAttribute.labelSize)
                .margin({ right: this.hotItemAttribute.labelMarginRight })

              Text(item.name)
                .fontSize(this.hotItemAttribute.fontSize)
                .fontColor(this.hotItemAttribute.fontColor)
                .fontWeight(this.hotItemAttribute.fontWeight)
            }
            .width("100%")
            .backgroundColor(this.hotItemAttribute.backgroundColor)
            .justifyContent(FlexAlign.Start)
            .onClick(() => {
              //历史搜索点击
              if (this.onItemClick != undefined) {
                this.onItemClick(item.name!)
              }
            })
          }
        })
      }.columnsTemplate(this.hotItemAttribute.columnsTemplate)
      .margin({ top: this.hotViewMarginTop })
      .rowsGap(this.hotItemAttribute.rowsGap)

相关总结

在日常的组件封装中,如果把所有的属性,都统一暴露至自定义组件一级的属性中,我们会发现,属性设置的是非常之多,再有小组件独立的情况下,也显得杂乱不堪,针对这种情况,其实我们可以把单独的小组件属性,独立的封装出来,使用回调函数的形式进行逐一设置即可。就比如我们这个自定义搜索模版,里面就分了很多个小组件属性。

onSearchAttribute?: (attribute: SearchViewAttribute) => void //搜索属性
private searchAttribute: SearchViewAttribute = new SearchViewAttribute()

在需要设置搜索小组件属性的时候,直接调用onSearchAttribute即可:

SearchLayout({
        hotList: this.hotList,
        onItemClick: (text: string) => {
          console.log("===条目点击:" + text)
        },
        onSearchAttribute: (attr) => {
          //搜索属性
          attr.placeholder = "搜索"
          attr.onSubmit = (text) => {
            console.log("===点击搜索:" + text)
          }
        }
      })

有一点需要注意,在使用回调函数设置属性的时候,一定要记得初始化设置,也就是把我们初始化的属性回调出去,接收调用者的设置。

aboutToAppear(): void {

    if (this.onSearchAttribute != undefined) {
      this.onSearchAttribute(this.searchAttribute)
    }

  }
点赞
收藏
评论区
推荐文章
Easter79 Easter79
3年前
taro 组件的外部样式和全局样式
自定义组件对应的样式文件,只对该组件内的节点生效。编写组件样式时,需要注意以下几点:1.组件和引用组件的页面不能使用id选择器(a)、属性选择器(\a\)和标签名选择器,请改用class选择器。2.组件和引用组件的页面中使用后代选择器(.a.b)在一些极端情况下会有非预期的表现,如遇,请避免使用。3.子
CuterCorley CuterCorley
4年前
uni-app入门教程(9)字体库、自定义组件、打包和新闻实战
前言本文主要介绍了4方面内容:在uniapp项目中使用iconfont提供的图标字体库,美化页面;实现自定义组件,并且可以自定义属性和实现父子组件之间的消息传递;微信小程序预览和真机测试,APP端云打包和本地打包;新闻列表和详情实战练习。一、使用iconfont字体库uniapp中可以使用iconfont()提供的图标字体。大致过程如下:
亚瑟 亚瑟
4年前
自定义 Hook – React
自定义Hook_Hook_是React16.8的新增特性。它可以让你在不编写class的情况下使用state以及其他的React特性。通过自定义Hook,可以将组件逻辑提取到可重用的函数中。在我们学习时,我们已经见过这个聊天程序中的组件,该组件用于显示好友的在线状态:importReact,{useSta
Stella981 Stella981
3年前
ReactNative遇到的坑总结(持续更新)
问题:在Android中input组件,文字会有遮挡在Android中,input组件默认会有内边距,所以把padding改为0可以解决问题问题:在Android中input组件,底部会有条白线添加红色的属性underlineColorAndroid<TextInputplaceholder"搜索关键字"
Stella981 Stella981
3年前
ListView的属性详解和探究
在我们的日常开发中,ListView是一个最常用的组件,所以我们非常有必要对它的属性进行全面的了解。现在就以一个简单的实例,对ListView的属性做一个简单的讲解。          首先我们给出简单的布局文件,就一个简单的ListView列表    :         <LinearLayout x
Stella981 Stella981
3年前
JavaScript数组索引检测中的数据类型问题
之前在写微信小程序项目时,里面有一个“城市选择”的功能,笔者用的是<pickerview组件,这个组件比较特别,因为它的value属性规定是数组格式的。比如:value"1"。因为当时对JS变量类型转换的不了解,笔者在代码中写下了这样的几行判断:(这是严谨的)letval_onetypeoft
Wesley13 Wesley13
3年前
2020.8.07 微信小程序(组件封装)
今天说一下微信小程序组件的封装…为什么要封装组件?写组件的目的就是为了复用,它的好处太多了a.写更少的代码。b.减少开发时间。c.代码的bug更少。d.占用的字节更少。…组件复用使我们的代码组织变得非常灵活。那么组件到底怎么封装,其实就和vue组件封装的思
Stella981 Stella981
3年前
Lightning Web Components html_templates(三)
LightningWebComponents强大之处在于模版系统,使用了虚拟dom进行智能高效的组件渲染。使用简单语法以声明方式将组件的模板绑定到组件的JavaScript类中的数据数据绑定我们可以使用{property}绑定组件模版属性到一个组件js类中的属性一个简单的例子组件class
程序员一鸣 程序员一鸣
2个月前
鸿蒙开发:使用Rect绘制矩形
几何矩形,在实际的开发中,有多种的实现方式,并非一定需要Rect组件,但是,如果有需要用到矩形的场景,建议还是使用Rect组件,因为Rect组件自身携带了很多样式属性,可以满足我们日常的不同的需求。
程序员一鸣 程序员一鸣
2星期前
HarmonyOS开发:组件如何实现属性的动态设置
如果是属性值的动态切换,使用方式一便可满足需求,如果是属性的动态设置,使用方式二即可。