当前位置:   article > 正文

鸿蒙listitem和tabcontent+页面传参_鸿蒙listitem不能加入tab

鸿蒙listitem不能加入tab

效果展示

新建项目

写Index.ets

  1. import router from '@ohos.router';
  2. @Entry
  3. @Component
  4. struct Index {
  5. @State fontColor: string = '#182431'
  6. @State selectedFontColor: string = '#007DFF'
  7. @State currentIndex: number = 0
  8. private controller: TabsController = new TabsController()
  9. arrayList=[
  10. {"name":"苹果","price":1,"imageUrl":"https://img1.baidu.com/it/u=1513650579,1757297287&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1681059600&t=2bdf979a877dd8723c72d7285b140734"},
  11. {"name":"香蕉","price":2,"imageUrl":"https://img0.baidu.com/it/u=387808086,3839707072&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1681059600&t=eb4513cd82a1e087c2e5fbb6a887b376"},
  12. {"name":"苹果","price":1,"imageUrl":"https://img1.baidu.com/it/u=1513650579,1757297287&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1681059600&t=2bdf979a877dd8723c72d7285b140734"},
  13. {"name":"香蕉","price":2,"imageUrl":"https://img0.baidu.com/it/u=387808086,3839707072&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1681059600&t=eb4513cd82a1e087c2e5fbb6a887b376"},
  14. {"name":"苹果","price":1,"imageUrl":"https://img1.baidu.com/it/u=1513650579,1757297287&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1681059600&t=2bdf979a877dd8723c72d7285b140734"},
  15. {"name":"香蕉","price":2,"imageUrl":"https://img0.baidu.com/it/u=387808086,3839707072&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1681059600&t=eb4513cd82a1e087c2e5fbb6a887b376"},
  16. {"name":"苹果","price":1,"imageUrl":"https://img1.baidu.com/it/u=1513650579,1757297287&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1681059600&t=2bdf979a877dd8723c72d7285b140734"},
  17. {"name":"香蕉","price":2,"imageUrl":"https://img0.baidu.com/it/u=387808086,3839707072&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1681059600&t=eb4513cd82a1e087c2e5fbb6a887b376"},
  18. ]
  19. arrayList1=[
  20. {"name":"白花甲","price":3,"imageUrl":"https://cp1.douguo.com/upload/caiku/4/7/1/200_476b6ccce2615ebded56edf961473ee1.jpg"},
  21. {"name":"黑花甲","price":4,"imageUrl":"https://img11.360buyimg.com/n1/jfs/t21088/34/576914115/285294/c5eddaf4/5b120ee3N23d15df2.png"},
  22. {"name":"白花甲","price":3,"imageUrl":"https://cp1.douguo.com/upload/caiku/4/7/1/200_476b6ccce2615ebded56edf961473ee1.jpg"},
  23. {"name":"黑花甲","price":4,"imageUrl":"https://img11.360buyimg.com/n1/jfs/t21088/34/576914115/285294/c5eddaf4/5b120ee3N23d15df2.png"},
  24. {"name":"白花甲","price":3,"imageUrl":"https://cp1.douguo.com/upload/caiku/4/7/1/200_476b6ccce2615ebded56edf961473ee1.jpg"},
  25. {"name":"黑花甲","price":4,"imageUrl":"https://img11.360buyimg.com/n1/jfs/t21088/34/576914115/285294/c5eddaf4/5b120ee3N23d15df2.png"},
  26. {"name":"白花甲","price":3,"imageUrl":"https://cp1.douguo.com/upload/caiku/4/7/1/200_476b6ccce2615ebded56edf961473ee1.jpg"},
  27. {"name":"黑花甲","price":4,"imageUrl":"https://img11.360buyimg.com/n1/jfs/t21088/34/576914115/285294/c5eddaf4/5b120ee3N23d15df2.png"},
  28. ]
  29. arrayList2=[
  30. {"name":"白色马桶刷","price":3.5,"imageUrl":"https://img.alicdn.com/bao/uploaded/i2/2211344733691/O1CN01veC6f01rmHTxQrc8x_!!0-item_pic.jpg_300x300q90.jpg"},
  31. {"name":"彩色马桶刷","price":5,"imageUrl":"https://p2.ssl.qhimgs1.com/sdr/400__/t016e6ba4514daa77e2.jpg"},
  32. {"name":"白色马桶刷","price":3.5,"imageUrl":"https://img.alicdn.com/bao/uploaded/i2/2211344733691/O1CN01veC6f01rmHTxQrc8x_!!0-item_pic.jpg_300x300q90.jpg"},
  33. {"name":"彩色马桶刷","price":5,"imageUrl":"https://p2.ssl.qhimgs1.com/sdr/400__/t016e6ba4514daa77e2.jpg"},
  34. {"name":"白色马桶刷","price":3.5,"imageUrl":"https://img.alicdn.com/bao/uploaded/i2/2211344733691/O1CN01veC6f01rmHTxQrc8x_!!0-item_pic.jpg_300x300q90.jpg"},
  35. {"name":"彩色马桶刷","price":5,"imageUrl":"https://p2.ssl.qhimgs1.com/sdr/400__/t016e6ba4514daa77e2.jpg"},
  36. {"name":"白色马桶刷","price":3.5,"imageUrl":"https://img.alicdn.com/bao/uploaded/i2/2211344733691/O1CN01veC6f01rmHTxQrc8x_!!0-item_pic.jpg_300x300q90.jpg"},
  37. {"name":"彩色马桶刷","price":5,"imageUrl":"https://p2.ssl.qhimgs1.com/sdr/400__/t016e6ba4514daa77e2.jpg"},
  38. ]
  39. arrayList3=[
  40. {"name":"白色吊带","price":7,"imageUrl":"http://image4.suning.cn/uimg/b2c/newcatentries/0070171179-000000000709981013_4_200x200.jpg"},
  41. {"name":"吊带","price":13.5,"imageUrl":"https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=2453905705,2017021135&fm=173&app=25&f=JPEG?w=640&h=924&s=B08B9557C6134FCE592D88630300805B"},
  42. {"name":"白色吊带","price":7,"imageUrl":"http://image4.suning.cn/uimg/b2c/newcatentries/0070171179-000000000709981013_4_200x200.jpg"},
  43. {"name":"吊带","price":13.5,"imageUrl":"https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=2453905705,2017021135&fm=173&app=25&f=JPEG?w=640&h=924&s=B08B9557C6134FCE592D88630300805B"},
  44. {"name":"白色吊带","price":7,"imageUrl":"http://image4.suning.cn/uimg/b2c/newcatentries/0070171179-000000000709981013_4_200x200.jpg"},
  45. {"name":"吊带","price":13.5,"imageUrl":"https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=2453905705,2017021135&fm=173&app=25&f=JPEG?w=640&h=924&s=B08B9557C6134FCE592D88630300805B"},
  46. {"name":"白色吊带","price":7,"imageUrl":"http://image4.suning.cn/uimg/b2c/newcatentries/0070171179-000000000709981013_4_200x200.jpg"},
  47. {"name":"吊带","price":13.5,"imageUrl":"https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=2453905705,2017021135&fm=173&app=25&f=JPEG?w=640&h=924&s=B08B9557C6134FCE592D88630300805B"},
  48. ]
  49. @Builder TabBuilder(index: number, name: string) {
  50. Column() {
  51. Text(name)
  52. .fontColor(this.currentIndex === index ?
  53. this.selectedFontColor : this.fontColor)
  54. .fontSize(16)
  55. .fontWeight(this.currentIndex === index ? 500 : 400)
  56. .lineHeight(22)
  57. .margin({ top: 17, bottom: 7 })
  58. Divider()
  59. .strokeWidth(2)
  60. .color('#6666ff')
  61. .opacity(this.currentIndex === index ? 1 : 0)
  62. }.width('100%')
  63. }
  64. build() {
  65. Tabs({ barPosition: BarPosition.Start, controller:this.controller }) {
  66. TabContent() {
  67. Column(){
  68. List(){
  69. ForEach(this.arrayList,(item,index)=>{
  70. ListItem(){
  71. Row(){
  72. Image(item.imageUrl)
  73. .layoutWeight(1)
  74. .width(200)
  75. .height(200)
  76. Text(item.name)
  77. .fontSize(20)
  78. .margin({left:20})
  79. Text(item.price+"/kg")
  80. .fontSize(20)
  81. .margin({left:20,right:20})
  82. }
  83. }.onClick(()=>{
  84. router.pushUrl({
  85. url: "pages/Second",
  86. params: {
  87. name: item.name,
  88. price: item.price+"/kg",
  89. image:item.imageUrl
  90. }
  91. }, router.RouterMode.Single)
  92. })
  93. })
  94. }
  95. }.width('100%').height('100%').backgroundColor('#00CB87')
  96. }.tabBar(this.TabBuilder(0, '水果'))
  97. TabContent() {
  98. Column(){
  99. List(){
  100. ForEach(this.arrayList1,(item,index)=>{
  101. ListItem(){
  102. Row(){
  103. Image(item.imageUrl)
  104. .layoutWeight(1)
  105. .height(200)
  106. Text(item.name)
  107. .fontSize(20)
  108. .margin({left:20})
  109. Text(item.price+"/kg")
  110. .fontSize(20)
  111. .margin({left:20,right:20})
  112. }
  113. }.onClick(()=>{
  114. router.pushUrl({
  115. url: "pages/Second",
  116. params: {
  117. name: item.name,
  118. price: item.price+"/kg",
  119. image:item.imageUrl
  120. }
  121. }, router.RouterMode.Single)
  122. })
  123. })
  124. }
  125. }.width('100%').height('100%').backgroundColor('#ff77b0e9')
  126. }.tabBar(this.TabBuilder(1, '海鲜'))
  127. TabContent() {
  128. Column(){
  129. List(){
  130. ForEach(this.arrayList2,(item,index)=>{
  131. ListItem(){
  132. Row(){
  133. Image(item.imageUrl)
  134. .layoutWeight(1)
  135. .height(200)
  136. Text(item.name)
  137. .fontSize(20)
  138. .margin({left:20})
  139. Text(item.price+"/kg")
  140. .fontSize(20)
  141. .margin({left:20,right:20})
  142. }
  143. }.onClick(()=>{
  144. router.pushUrl({
  145. url: "pages/Second",
  146. params: {
  147. name: item.name,
  148. price: item.price+"/kg",
  149. image:item.imageUrl
  150. }
  151. }, router.RouterMode.Single)
  152. })
  153. })
  154. }
  155. }.width('100%').height('100%').backgroundColor('#FFBF00')
  156. }.tabBar(this.TabBuilder(2, '百货'))
  157. TabContent() {
  158. Column(){
  159. List(){
  160. ForEach(this.arrayList3,(item,index)=>{
  161. ListItem(){
  162. Row(){
  163. Image(item.imageUrl)
  164. .layoutWeight(1)
  165. .height(200)
  166. Text(item.name)
  167. .fontSize(20)
  168. .margin({left:20})
  169. Text(item.price+"/kg")
  170. .fontSize(20)
  171. .margin({left:20,right:20})
  172. }
  173. }.onClick(()=>{
  174. router.pushUrl({
  175. url: "pages/Second",
  176. params: {
  177. name: item.name,
  178. price: item.price+"/kg",
  179. image:item.imageUrl
  180. }
  181. }, router.RouterMode.Single)
  182. })
  183. })
  184. }
  185. }.width('100%').height('100%').backgroundColor('#E67C92')
  186. }.tabBar(this.TabBuilder(3, '衣服'))
  187. }
  188. .vertical(false) //Tabs 方向 false:横向 true:纵向
  189. .barMode(BarMode.Fixed) //布局模式 Fixed:平均分配页签栏宽度或长度
  190. .barWidth(360) //设置页签栏宽度
  191. .barHeight(56) //设置页签栏高度
  192. .animationDuration(400) //设置切换页签的滑动动画时长
  193. .onChange((index: number) => {
  194. this.currentIndex = index
  195. })
  196. .width(360)
  197. .height(780)
  198. .backgroundColor('#F1F3F5')
  199. /
  200. }
  201. clickNext() {
  202. router.pushUrl({
  203. url: "pages/Second",
  204. params: {
  205. name: "fdfs",
  206. count: 100
  207. }
  208. }, router.RouterMode.Single)
  209. }
  210. }

写Second.ets

  1. import router from '@ohos.router';
  2. @Entry
  3. @Component
  4. struct Second {
  5. @State message: string = 'Hello World'
  6. @State paramsFromIndex: object = router.getParams()
  7. build() {
  8. Row() {
  9. Column() {
  10. Text(this.paramsFromIndex?.['name'])
  11. .fontSize(50)
  12. .fontWeight(FontWeight.Bold)
  13. Image(this.paramsFromIndex?.['image'])
  14. .width(200)
  15. .height(200)
  16. Text(this.paramsFromIndex?.['price'])
  17. .fontSize(50)
  18. .fontWeight(FontWeight.Bold)
  19. Button() {
  20. Text('返回')
  21. .fontSize(25)
  22. .fontWeight(FontWeight.Bold)
  23. }
  24. .type(ButtonType.Capsule)
  25. .margin({
  26. top: 20
  27. })
  28. .backgroundColor('#0D9FFB')
  29. .width('40%')
  30. .height('5%')
  31. // 返回按钮绑定onClick事件,点击按钮时返回到第一页
  32. .onClick(() => {
  33. router.back()
  34. })
  35. }
  36. .width('100%')
  37. }
  38. .height('100%')
  39. }
  40. }

 如何运行:右上角眼睛

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/231510
推荐阅读