场景描述
通过左边一级列表的选择,联动更新右边二级列表的数据,常用于商品分类选择、编辑风格等二级类别选择页面。
本场景以商品分类列表页面为例,分别通过List组件,对左侧分类导航和右侧导航内容进行展示。在进入页面后,点击左侧分类导航,右侧展示对应导航分类详情列表数据;滑动右侧列表内容,列表标题吸顶展示,左侧对应导航内容则高亮显示。
页面整体结构图 | 页面效果图 |
---|---|
![]() |
![]() |
实现原理
左右各用一个List实现,分别设置其onScrollIndex()事件,左侧List在回调中判断数据项切换时,调用右侧List滚动到相应类别的对应位置,右侧同理。
开发步骤
分别通过List组件构建左侧分类导航数据和右侧分类内容数据。
// Left List Data Display.
List({ scroller: this.navTitleScroller }) {
ForEach(this.categoryList, (item: NavTitleModel, index: number) => {
ListItem() {
Text(item.titleName)
// ...
}
}, (item: NavTitleModel) => JSON.stringify(item.titleName))
}
// ...
// Display of List Content on the Right.
List({ scroller: this.goodsListScroller }) {
ForEach(this.categoryList, (item: NavTitleModel) => {
ListItemGroup({ space: 12, header: this.goodsHeaderBuilder(item.titleName) }) {
ForEach(item.goodsList, (goodsItem: GoodsDataModel) => {
ListItem() {
Row() {
Image(goodsItem.imgUrl)
// ...
Column() {
Text(goodsItem.goodsName)
// ...
Text('¥' + goodsItem.price)
// ...
}
// ...
}
// ...
}
}, (goodsItem: GoodsDataModel) => JSON.stringify(goodsItem.goodsId))
}
}, (item: NavTitleModel) => JSON.stringify(item.goodsList))
}
CategoryPage.ets
给左侧导航列表添加点击事件,右侧分类详情列表添加onScrollIndex()事件,并调用自定义事件listChange方法,在listChange方法内部根据isGoods变量的值,调用对应列表控制器的scrollToIndex()事件,实现导航列表和分类详情数据的联动效果。
// List sliding event.
listChange(index: number, isGoods: boolean) {
if (this.currentTitleId !== index) {
this.currentTitleId = index;
if (isGoods) {
// IsGoods is true, controlling the data in the right-hand list to slide to the specified index.
this.goodsListScroller.scrollToIndex(index);
} else {
// IsGoods is set to false, controlling the left list data to slide to the specified index.
this.navTitleScroller.scrollToIndex(index);
}
}
}
CategoryPage.ets
// Left List Data Display.
List({ scroller: this.navTitleScroller }) {
ForEach(this.categoryList, (item: NavTitleModel, index: number) => {
ListItem() {
Text(item.titleName)
// ...
.onClick(() => {
// Pass in the current list item index and true.
this.listChange(index, true);
})
}
}, (item: NavTitleModel) => JSON.stringify(item.titleName))
}
// ...
// Display of List Content on the Right.
List({ scroller: this.goodsListScroller }) {
// ...
}
// ...
.onScrollIndex((index: number) => {
// Pass in the current list item index and false.
this.listChange(index, false)
})
CategoryPage.ets
实现效果
本文主要引用整理于鸿蒙官方文档
狗的胡须有什么用 | 眼睛为什么老是流眼泪 | 36什么意思 | 试管婴儿是什么意思 | 一点点奶茶什么最好喝 |
子宫小结节是什么意思 | 儿童鼻炎吃什么药 | 叙字五行属什么 | 天哭星是什么意思 | 刚怀孕有什么症状 |
伊索寓言有什么故事 | 飞刀是什么意思 | 拔罐之后要注意什么 | 平肝潜阳是什么意思 | 腹泻能吃什么食物 |
梦到吃苹果是什么意思 | 吃什么有奶 | 大米放什么不生虫子 | 什么是火象星座 | 覆水难收什么意思 |
舌头干涩是什么病前兆0735v.com | 胃热吃什么药hcv8jop7ns7r.cn | 更年期什么时候jiuxinfghf.com | 冷战的男人是什么心理hcv9jop2ns6r.cn | 沐猴而冠代表什么生肖shenchushe.com |
med是什么意思hcv8jop6ns1r.cn | 肉桂跟桂皮有什么区别hcv7jop6ns1r.cn | 什么是有氧运动什么是无氧运动shenchushe.com | 海绵体充血不足吃什么药hcv8jop0ns9r.cn | 12月27日是什么星座hcv9jop5ns5r.cn |
俄罗斯用什么货币hcv8jop0ns3r.cn | 肺气虚吃什么药hcv8jop6ns4r.cn | 解酒喝什么好hcv8jop0ns8r.cn | alb医学上是什么意思hcv7jop6ns6r.cn | 体内湿气重吃什么药hcv9jop5ns5r.cn |
什么屎不臭hcv8jop8ns9r.cn | 甲状腺欠均匀什么意思hcv8jop9ns9r.cn | 拔罐出水是什么原因hcv9jop6ns0r.cn | 04年属猴的是什么命hcv9jop6ns6r.cn | 麟字五行属什么hcv9jop1ns0r.cn |