列表中的每一个Item都对应一个复选框。当前,CheckBox仅支持逐个选中/取消选中,如何实现手指滑动经过CheckBox的时候,CheckBox能完成选中/取消选中的功能,同时不影响列表滑动。
CheckBox本身不提供滑动多选的能力,需要自定义选中逻辑:手指在CheckBox选择框区域进行滑动时,完成选中操作;手指在文本区域进行滑动时,进行列表滚动。
// 记录组件的位置 interface ComponentRect { left: number; top: number; right: number; bottom: number; }
// 判断手指当前在哪个组件的矩形区域内 detectCurrentComponent(x: number, y: number) { let cur = ''; this.componentRects.forEach((rect, id) => { if (x >= rect.left && x <= rect.right && y >= rect.top && y <= rect.bottom) { cur = id; } }); return cur; }
// 组件发生变化时,刷新组件的位置信息 .onAreaChange((oldVal, newVal) => { console.info(`${oldVal},${newVal}`); let area: ComponentRect = { left: Number(newVal.position.x), top: Number(newVal.position.y), right: Number(newVal.position.x) + Number(newVal.width), bottom: Number(newVal.position.y) + Number(newVal.height) }; this.componentRects.set(item.id, area); // 记录组件位置 })
// 获取手指处于组件上的横向比例 getRateWidth(x: number, index: string) { let item = this.componentRects.get(index); if (!item) { return 0; } return (x - item.left) / (item.right - item.left); }
.onTouch((event) => { // 获取当前手指的坐标(相对于父容器,与组件rect的坐标系一致) const fingerX = event.touches[0].x; const fingerY = event.touches[0].y; // 判断手指当前在哪个组件的矩形区域内 let cur = this.detectCurrentComponent(fingerX, fingerY); // 获取手指处于组件上的横向比例 let rate = this.getRateWidth(fingerX, cur); if (event.type === TouchType.Move && 0 < rate && rate < this.widthCheckRate) { this.isInCheckBox = true; // 更新CheckBox状态 if (cur !== this.curId) { this.curId = cur; let item = this.itemsMap.get(cur); if (item !== undefined) { item.checked = !item.checked; this.itemsMap.set(cur, item); } } } if (event.type === TouchType.Up) { setTimeout(() => { this.isInCheckBox = false; }, 100); this.curId = ''; } })
完整示例参考如下:
@Entry @Component struct CheckBoxTest { @State items: Array<CheckBoxStatus> = []; // 记录组件的位置信息 @State itemsMap: Map<string, CheckBoxStatus> = new Map<string, CheckBoxStatus>(); @State isInCheckBox: boolean = false; private componentRects: Map<string, ComponentRect> = new Map(); // 存储组件的位置信息 private widthCheckRate: number = 0.2; // 选择框所占比例 private curId: string = ''; // 当前手指所在的组件 aboutToAppear(): void { for (let index = 0; index < 20; index++) { this.itemsMap.set(String(index), { id: String(index), name: '选项序号 - ' + index, checked: false }); } this.items = Array.from(this.itemsMap.values()); } build() { Column() { List({ space: 10 }) { ForEach(this.items, (item: CheckBoxStatus) => { ListItem() { Row() { Flex({ justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) { Checkbox() .enabled(false) // 禁用CheckBox自己的选中逻辑 .select(this.itemsMap.get(item.id)?.checked) }.width(`${100 * this.widthCheckRate}%`) .hitTestBehavior(HitTestMode.Transparent) Flex({ alignItems: ItemAlign.Center }) { Text(item.name) .fontSize(18) }.width(`${100 - 100 * this.widthCheckRate}%`) } .width('100%') .height(50) } .backgroundColor('#fff1f3f5') .borderRadius(10) .margin({ left: 16, right: 16 }) // 组件发生变化时,刷新组件的位置信息 .onAreaChange((oldVal, newVal) => { console.info(`${oldVal},${newVal}`); let area: ComponentRect = { left: Number(newVal.position.x), top: Number(newVal.position.y), right: Number(newVal.position.x) + Number(newVal.width), bottom: Number(newVal.position.y) + Number(newVal.height) }; this.componentRects.set(item.id, area); // 记录组件位置 }) }) } .friction(this.isInCheckBox ? 1000 : 0) .onWillScroll(() => { // 手指不在CheckBox框位置时,禁止列表滑动 if (this.isInCheckBox) { return { offsetRemain: 0 }; } return; }) .width('100%') .height('100%') } .padding({ top: 10 }) .width('100%') .height('95%') .backgroundColor('#ffffffff') .onTouch((event) => { // 获取当前手指的坐标(相对于父容器,与组件rect的坐标系一致) const fingerX = event.touches[0].x; const fingerY = event.touches[0].y; // 判断手指当前在哪个组件的矩形区域内 let cur = this.detectCurrentComponent(fingerX, fingerY); // 获取手指处于组件上的横向比例 let rate = this.getRateWidth(fingerX, cur); if (event.type === TouchType.Move && 0 < rate && rate < this.widthCheckRate) { this.isInCheckBox = true; // 更新CheckBox状态 if (cur !== this.curId) { this.curId = cur; let item = this.itemsMap.get(cur); if (item !== undefined) { item.checked = !item.checked; this.itemsMap.set(cur, item); } } } if (event.type === TouchType.Up) { setTimeout(() => { this.isInCheckBox = false; }, 100); this.curId = ''; } }) } // 判断手指当前在哪个组件的矩形区域内 detectCurrentComponent(x: number, y: number) { let cur = ''; this.componentRects.forEach((rect, id) => { if (x >= rect.left && x <= rect.right && y >= rect.top && y <= rect.bottom) { cur = id; } }); return cur; } // 获取手指处于组件上的横向比例 getRateWidth(x: number, index: string) { let item = this.componentRects.get(index); if (!item) { return 0; } return (x - item.left) / (item.right - item.left); } } // 记录CheckBox的状态 interface CheckBoxStatus { id: string; name: string; checked: boolean; } // 记录组件的位置 interface ComponentRect { left: number; top: number; right: number; bottom: number; }