import { window } from '@kit.ArkUI';
import { common } from '@kit.AbilityKit';
const leftItemWidth = 100;
const rightItemWidth = 100;
const itemHeight = 90;
const groupHeight = 50;
@Component
struct ItemComponent {
private arr: string[] = [
'1', '2', '3', '4', '5', '6', '7', '8'];
dataSource = new CommonDataSource<string>();
scroller?: Scroller = undefined;
scrollCallBack?: (param: number) => void;
remainOffsetCallBack?: (param: number) => void;
aboutToAppear(): void {
let context: common.UIAbilityContext = this.getUIContext().getHostContext() as common.UIAbilityContext;
window.getLastWindow(context).then((lastWindow) => {
lastWindow.setWindowLayoutFullScreen(true); // 设置页面全屏
});
this.dataSource.setData(this.arr);
}
// 下部分参数列表每行数据List
@Builder
rightSingleLineList() {
List({ scroller: this.scroller }) {
LazyForEach(this.dataSource, (item: string) => {
ListItem() {
Text(item)
.height('100%')
.width('100%')
.fontSize(16)
.textAlign(TextAlign.Start)
.borderRadius(0)
.padding(10)
.backgroundColor(0xFFFFFF);
}
.width(rightItemWidth);
}, (item: string) => item);
}
.height('100%')
.width('100%')
.layoutWeight(1)
.listDirection(Axis.Horizontal)
.scrollBar(BarState.Off)
.friction(0.6)
.divider({ strokeWidth: 0.5, color: 0xeeeeee }) // 每行之间的分界线
.edgeEffect(EdgeEffect.None) // 边缘效果设置为Spring
.onDidScroll(() => {
// 通过callBack回调行在横向滚动时,Scroller当前的offset
if (this.remainOffsetCallBack) {
this.remainOffsetCallBack(this.scroller!.currentOffset().xOffset);
}
})
.onScrollFrameBegin((offset: number) => {
if (this.scrollCallBack) {
this.scrollCallBack(this.scroller!.currentOffset().xOffset + offset);
}
return { offsetRemain: offset };
});
}
build() {
Column() {
this.rightSingleLineList();
Line().width('100%').height(0.5).backgroundColor(0xeeeeee);
}.height(itemHeight);
}
}
@Entry
@Component
export struct Compare {
private topRightArr: string[] = [
'苹果', '香蕉', '橙子', '草莓', '芒果', '葡萄', '西瓜', '桃子'];
// 参数行在横向滚动时的offset
@State remainOffset: number = 0;
private bottomRightScroller: Scroller = new Scroller();
private bottomLeftScroller: Scroller = new Scroller();
private topRightScroller: Scroller = new Scroller();
private showDataArray: ShowData[] = [];
private dataSource = new CommonDataSource<ShowData>();
private leftItems: string[] = [
'市场零售价',
'原产地',
'水果等级',
'品种类型',
'上市季节',
'核心品种',
'甜度指数',
'果径大小',
'保鲜方式',
'外形特征',
'果皮类型',
'最佳储存温度',
'成熟周期',
'糖度含量',
'实测酸度',
'保质期限',
'运输成本'
];
aboutToAppear(): void {
this.loadData();
}
private async loadData() {
// 水果数据
let resp: DataItem[] = [
{
'sticky': '营养成分',
'sub': ['热量', '糖分', '维生素', '膳食纤维', '水分含量']
},
{
'sticky': '外观特征',
'sub': ['颜色', '形状', '大小', '重量', '表皮质地']
},
{
'sticky': '口感风味',
'sub': ['甜度', '酸度', '香味', '口感', '汁水']
}
];
for (let i = 0; i < resp.length; i++) {
let item = resp[i];
let showData = new ShowData();
showData.sticky = item.sticky;
showData.sub = item.sub;
showData.sub!.forEach(() => {
let scroller: Scroller = new Scroller();
showData.scrollerArray!.push(scroller);
});
this.showDataArray.push(showData);
}
this.dataSource.setData(this.showDataArray);
}
build() {
Column() {
// 上部分
this.topFixed();
// 下部分
Row() {
this.leftList();
this.rightList();
Line()
.height('100%')
.width(0.5)
.backgroundColor('#EEEEEE')
.position({ x: leftItemWidth });
}
.justifyContent(FlexAlign.Start)
.alignItems(VerticalAlign.Top);
}
.height('100%')
.justifyContent(FlexAlign.Start)
.alignItems(HorizontalAlign.Start);
}
@Builder
// 修改leftStickyHeader方法
leftStickyHeader(title: string) {
Text(title)
.width(leftItemWidth)
.height(groupHeight)
.backgroundColor('#f1f3f5')
.textAlign(TextAlign.Center)
.border({ width: 1 });
}
// 上部分整体Row(Column + List)
@Builder
topFixed() {
Row() {
// 上部分左侧固定信息
Column() {
Text('水果')
.fontSize(15);
}
.width(leftItemWidth)
.height(100)
.backgroundColor(Color.White)
.justifyContent(FlexAlign.Center)
.alignItems(HorizontalAlign.Start)
.padding(10);
// 分割线
Line()
.height(100)
.width(0.5)
.backgroundColor(0xeeeeee);
// 上部分右侧车型横向滚动列表
List({ scroller: this.topRightScroller/* 绑定Scroller控制器与其他控制器联动*/ }) {
ForEach(this.topRightArr, (item: string) => {
ListItem() {
Text(item)
.height(100)
.width(rightItemWidth)
.fontSize(16)
.textAlign(TextAlign.Start)
.borderRadius(0)
.padding(10)
.backgroundColor(0xFFFFFF);
};
}, (item: string) => item);
}
.listDirection(Axis.Horizontal) // 设置滚动方向为横向滚动
.edgeEffect(EdgeEffect.None)
.divider({ strokeWidth: 0.5, color: 0xeeeeee })
.scrollBar(BarState.Off)
.width('100%')
.height('100%')
.layoutWeight(1)
.onScrollFrameBegin((offset: number) => {
// 关键联动,通过对象保存的Scroller控制器数组遍历保持offset同步
this.dataSource.getAllData().forEach(showData => {
showData.scrollerArray!.forEach(scroller => {
scroller.scrollTo({ xOffset: this.topRightScroller.currentOffset().xOffset + offset, yOffset: 0 });
});
});
return { offsetRemain: offset };
});
}
.height(100)
.width('100%');
}
// 下部分左侧行标题列表纵向滚动List
@Builder
leftList() {
List({ scroller: this.bottomLeftScroller/* 绑定控制器与其他控制器联动*/ }) {
ForEach(this.leftItems, (item: string) => {
ListItem() {
Text(item)
.width(leftItemWidth)
.height(itemHeight)
.textAlign(TextAlign.Center);
};
}, (item: string) => item);
}
.sticky(StickyStyle.Header)
.listDirection(Axis.Vertical)
.scrollBar(BarState.Off)
.friction(0.6)
.edgeEffect(EdgeEffect.None)
.height('calc(100% - 100vp)')
.width(leftItemWidth)
.onScrollFrameBegin((offset: number) => {
// 带动右侧纵向Scroll组件绑定的Scroller控制器同步滑动
this.bottomRightScroller.scrollTo({
xOffset: 0,
yOffset: this.bottomLeftScroller.currentOffset().yOffset + offset,
animation: false
});
return { offsetRemain: offset };
});
}
// 下部分右侧内容显示区域纵向List(ListItem(List))
@Builder
rightList() {
List({ initialIndex: 0, scroller: this.bottomRightScroller }) {
// 通过LazyForEach加载每一行
LazyForEach(this.dataSource, (item: ShowData) => {
ListItemGroup({}) {
ForEach(item.sub, (subItem: string) => {
// 自定义ListItem中包含横向滚动List
ItemComponent({
scroller: item.scrollerArray![item.sub!.indexOf(subItem)],
scrollCallBack: (value) => {
// value为子List横向滚动onScrollFrameBegin回传offset,在手指拖动时保持联动一致
// 顶部车型List跟随联动
this.topRightScroller.scrollTo({ xOffset: value, yOffset: 0 });
// 通过对象保存的Scroller数组跟随保持联动
this.dataSource.getAllData().forEach(showData => {
showData.scrollerArray!.forEach(scroller => {
if (scroller !== item.scrollerArray![item.sub!.indexOf(subItem)]) {
scroller.scrollTo({ xOffset: value, yOffset: 0 });
}
});
});
},
remainOffsetCallBack: (value) => {
// 滚动过程中回传保持同步的offset值
this.remainOffset = value;
}
});
}, (item: string) => item);
};
}, (item: ShowData, index: number) => item.sticky! + index);
}
.sticky(StickyStyle.Header)
.listDirection(Axis.Vertical)
.scrollBar(BarState.Off)
.friction(0.6)
.edgeEffect(EdgeEffect.None)
.onScrollFrameBegin((offset: number) => {
// 内容List纵向滚动带动左侧标题List跟随滚动
this.bottomLeftScroller.scrollTo({
xOffset: 0,
yOffset: this.bottomRightScroller.currentOffset().yOffset + offset,
animation: false
});
return { offsetRemain: offset };
})
.onDidScroll(() => {
// 内容List纵向滚动过程中,每一行中子List的Scroller滚动到remainOffset与已显示的行位置保持一致
this.dataSource.getAllData().forEach(showData => {
showData.scrollerArray!.forEach(scroller => {
scroller.scrollTo({ xOffset: this.remainOffset, yOffset: 0 });
});
});
})
.position({ x: leftItemWidth, y: 0 })
.edgeEffect(EdgeEffect.None) // 边缘效果设置为Spring
.backgroundColor(0xDCDCDC)
.height('calc(100% - 100vp)')
.width('calc(100% - 100vp)');
}
}
class ShowData {
sticky?: string;
sub?: string[];
scrollerArray?: Scroller[] = [];
}
export class CommonDataSource<T> implements IDataSource {
private listeners: DataChangeListener[] = [];
protected originDataArray: T[] = [];
totalCount(): number {
return this.originDataArray.length;
}
getAllData(): T[] {
return this.originDataArray;
}
getData(index: number) {
return this.originDataArray[index];
}
addData(index: number, data: T): void {
this.originDataArray.splice(index, 0, data);
this.notifyDataAdd(index);
}
pushData(data: T): void {
this.originDataArray.push(data);
this.notifyDataAdd(this.originDataArray.length - 1);
}
pushDataArray(...items: T[]): void {
for (let data of items) {
this.originDataArray.push(data);
this.notifyDataAdd(this.originDataArray.length - 1);
}
}
deleteDataUseContent(data: T): void {
let delIndex: number = -1;
for (let index = 0; index < this.originDataArray.length; index++) {
const element = this.originDataArray[index];
if (data === element) {
delIndex = index;
}
}
if (delIndex != -1) {
this.deleteData(delIndex);
}
}
deleteData(index: number): void {
this.originDataArray.splice(index, 1);
this.notifyDataDelete(index);
}
setData(dataArray?: T[]) {
if (dataArray) {
this.originDataArray = dataArray;
} else {
this.originDataArray = [];
}
this.notifyDataReload();
}
registerDataChangeListener(listener: DataChangeListener): void {
if (this.listeners.indexOf(listener) < 0) {
this.listeners.push(listener);
}
}
unregisterDataChangeListener(listener: DataChangeListener): void {
const pos = this.listeners.indexOf(listener);
if (pos >= 0) {
this.listeners.splice(pos, 1);
}
}
notifyDataReload() {
this.listeners.forEach(listener => {
listener.onDataReloaded();
});
}
notifyDataAdd(index: number) {
this.listeners.forEach(listener => {
listener.onDataAdd(index);
});
}
notifyDataMove(from: number, to: number) {
this.listeners.forEach(listener => {
listener.onDataMove(from, to);
});
}
notifyDataDelete(index: number) {
this.listeners.forEach(listener => {
listener.onDataDelete(index);
});
}
}
interface DataItem {
sticky: string;
sub: string [];
} import { window } from '@kit.ArkUI';
import { common } from '@kit.AbilityKit';
const leftItemWidth = 100;
const rightItemWidth = 100;
const itemHeight = 90;
const groupHeight = 50;
@Component
struct ItemComponent {
private arr: string[] = [
'1', '2', '3', '4', '5', '6', '7', '8'];
dataSource = new CommonDataSource<string>();
scroller?: Scroller = undefined;
scrollCallBack?: (param: number) => void;
remainOffsetCallBack?: (param: number) => void;
aboutToAppear(): void {
let context: common.UIAbilityContext = this.getUIContext().getHostContext() as common.UIAbilityContext;
window.getLastWindow(context).then((lastWindow) => {
lastWindow.setWindowLayoutFullScreen(true); // 设置页面全屏
});
this.dataSource.setData(this.arr);
}
// 下部分参数列表每行数据List
@Builder
rightSingleLineList() {
List({ scroller: this.scroller }) {
LazyForEach(this.dataSource, (item: string) => {
ListItem() {
Text(item)
.height('100%')
.width('100%')
.fontSize(16)
.textAlign(TextAlign.Start)
.borderRadius(0)
.padding(10)
.backgroundColor(0xFFFFFF);
}
.width(rightItemWidth);
}, (item: string) => item);
}
.height('100%')
.width('100%')
.layoutWeight(1)
.listDirection(Axis.Horizontal)
.scrollBar(BarState.Off)
.friction(0.6)
.divider({ strokeWidth: 0.5, color: 0xeeeeee }) // 每行之间的分界线
.edgeEffect(EdgeEffect.None) // 边缘效果设置为Spring
.onDidScroll(() => {
// 通过callBack回调行在横向滚动时,Scroller当前的offset
if (this.remainOffsetCallBack) {
this.remainOffsetCallBack(this.scroller!.currentOffset().xOffset);
}
})
.onScrollFrameBegin((offset: number) => {
if (this.scrollCallBack) {
this.scrollCallBack(this.scroller!.currentOffset().xOffset + offset);
}
return { offsetRemain: offset };
});
}
build() {
Column() {
this.rightSingleLineList();
Line().width('100%').height(0.5).backgroundColor(0xeeeeee);
}.height(itemHeight);
}
}
@Entry
@Component
export struct Compare {
private topRightArr: string[] = [
'苹果', '香蕉', '橙子', '草莓', '芒果', '葡萄', '西瓜', '桃子'];
// 参数行在横向滚动时的offset
@State remainOffset: number = 0;
private bottomRightScroller: Scroller = new Scroller();
private bottomLeftScroller: Scroller = new Scroller();
private topRightScroller: Scroller = new Scroller();
private showDataArray: ShowData[] = [];
private dataSource = new CommonDataSource<ShowData>();
private leftItems: string[] = [
'市场零售价',
'原产地',
'水果等级',
'品种类型',
'上市季节',
'核心品种',
'甜度指数',
'果径大小',
'保鲜方式',
'外形特征',
'果皮类型',
'最佳储存温度',
'成熟周期',
'糖度含量',
'实测酸度',
'保质期限',
'运输成本'
];
aboutToAppear(): void {
this.loadData();
}
private async loadData() {
// 水果数据
let resp: DataItem[] = [
{
'sticky': '营养成分',
'sub': ['热量', '糖分', '维生素', '膳食纤维', '水分含量']
},
{
'sticky': '外观特征',
'sub': ['颜色', '形状', '大小', '重量', '表皮质地']
},
{
'sticky': '口感风味',
'sub': ['甜度', '酸度', '香味', '口感', '汁水']
}
];
for (let i = 0; i < resp.length; i++) {
let item = resp[i];
let showData = new ShowData();
showData.sticky = item.sticky;
showData.sub = item.sub;
showData.sub!.forEach(() => {
let scroller: Scroller = new Scroller();
showData.scrollerArray!.push(scroller);
});
this.showDataArray.push(showData);
}
this.dataSource.setData(this.showDataArray);
}
build() {
Column() {
// 上部分
this.topFixed();
// 下部分
Row() {
this.leftList();
this.rightList();
Line()
.height('100%')
.width(0.5)
.backgroundColor('#EEEEEE')
.position({ x: leftItemWidth });
}
.justifyContent(FlexAlign.Start)
.alignItems(VerticalAlign.Top);
}
.height('100%')
.justifyContent(FlexAlign.Start)
.alignItems(HorizontalAlign.Start);
}
@Builder
// 修改leftStickyHeader方法
leftStickyHeader(title: string) {
Text(title)
.width(leftItemWidth)
.height(groupHeight)
.backgroundColor('#f1f3f5')
.textAlign(TextAlign.Center)
.border({ width: 1 });
}
// 上部分整体Row(Column + List)
@Builder
topFixed() {
Row() {
// 上部分左侧固定信息
Column() {
Text('水果')
.fontSize(15);
}
.width(leftItemWidth)
.height(100)
.backgroundColor(Color.White)
.justifyContent(FlexAlign.Center)
.alignItems(HorizontalAlign.Start)
.padding(10);
// 分割线
Line()
.height(100)
.width(0.5)
.backgroundColor(0xeeeeee);
// 上部分右侧车型横向滚动列表
List({ scroller: this.topRightScroller/* 绑定Scroller控制器与其他控制器联动*/ }) {
ForEach(this.topRightArr, (item: string) => {
ListItem() {
Text(item)
.height(100)
.width(rightItemWidth)
.fontSize(16)
.textAlign(TextAlign.Start)
.borderRadius(0)
.padding(10)
.backgroundColor(0xFFFFFF);
};
}, (item: string) => item);
}
.listDirection(Axis.Horizontal) // 设置滚动方向为横向滚动
.edgeEffect(EdgeEffect.None)
.divider({ strokeWidth: 0.5, color: 0xeeeeee })
.scrollBar(BarState.Off)
.width('100%')
.height('100%')
.layoutWeight(1)
.onScrollFrameBegin((offset: number) => {
// 关键联动,通过对象保存的Scroller控制器数组遍历保持offset同步
this.dataSource.getAllData().forEach(showData => {
showData.scrollerArray!.forEach(scroller => {
scroller.scrollTo({ xOffset: this.topRightScroller.currentOffset().xOffset + offset, yOffset: 0 });
});
});
return { offsetRemain: offset };
});
}
.height(100)
.width('100%');
}
// 下部分左侧行标题列表纵向滚动List
@Builder
leftList() {
List({ scroller: this.bottomLeftScroller/* 绑定控制器与其他控制器联动*/ }) {
ForEach(this.leftItems, (item: string) => {
ListItem() {
Text(item)
.width(leftItemWidth)
.height(itemHeight)
.textAlign(TextAlign.Center);
};
}, (item: string) => item);
}
.sticky(StickyStyle.Header)
.listDirection(Axis.Vertical)
.scrollBar(BarState.Off)
.friction(0.6)
.edgeEffect(EdgeEffect.None)
.height('calc(100% - 100vp)')
.width(leftItemWidth)
.onScrollFrameBegin((offset: number) => {
// 带动右侧纵向Scroll组件绑定的Scroller控制器同步滑动
this.bottomRightScroller.scrollTo({
xOffset: 0,
yOffset: this.bottomLeftScroller.currentOffset().yOffset + offset,
animation: false
});
return { offsetRemain: offset };
});
}
// 下部分右侧内容显示区域纵向List(ListItem(List))
@Builder
rightList() {
List({ initialIndex: 0, scroller: this.bottomRightScroller }) {
// 通过LazyForEach加载每一行
LazyForEach(this.dataSource, (item: ShowData) => {
ListItemGroup({}) {
ForEach(item.sub, (subItem: string) => {
// 自定义ListItem中包含横向滚动List
ItemComponent({
scroller: item.scrollerArray![item.sub!.indexOf(subItem)],
scrollCallBack: (value) => {
// value为子List横向滚动onScrollFrameBegin回传offset,在手指拖动时保持联动一致
// 顶部车型List跟随联动
this.topRightScroller.scrollTo({ xOffset: value, yOffset: 0 });
// 通过对象保存的Scroller数组跟随保持联动
this.dataSource.getAllData().forEach(showData => {
showData.scrollerArray!.forEach(scroller => {
if (scroller !== item.scrollerArray![item.sub!.indexOf(subItem)]) {
scroller.scrollTo({ xOffset: value, yOffset: 0 });
}
});
});
},
remainOffsetCallBack: (value) => {
// 滚动过程中回传保持同步的offset值
this.remainOffset = value;
}
});
}, (item: string) => item);
};
}, (item: ShowData, index: number) => item.sticky! + index);
}
.sticky(StickyStyle.Header)
.listDirection(Axis.Vertical)
.scrollBar(BarState.Off)
.friction(0.6)
.edgeEffect(EdgeEffect.None)
.onScrollFrameBegin((offset: number) => {
// 内容List纵向滚动带动左侧标题List跟随滚动
this.bottomLeftScroller.scrollTo({
xOffset: 0,
yOffset: this.bottomRightScroller.currentOffset().yOffset + offset,
animation: false
});
return { offsetRemain: offset };
})
.onDidScroll(() => {
// 内容List纵向滚动过程中,每一行中子List的Scroller滚动到remainOffset与已显示的行位置保持一致
this.dataSource.getAllData().forEach(showData => {
showData.scrollerArray!.forEach(scroller => {
scroller.scrollTo({ xOffset: this.remainOffset, yOffset: 0 });
});
});
})
.position({ x: leftItemWidth, y: 0 })
.edgeEffect(EdgeEffect.None) // 边缘效果设置为Spring
.backgroundColor(0xDCDCDC)
.height('calc(100% - 100vp)')
.width('calc(100% - 100vp)');
}
}
class ShowData {
sticky?: string;
sub?: string[];
scrollerArray?: Scroller[] = [];
}
export class CommonDataSource<T> implements IDataSource {
private listeners: DataChangeListener[] = [];
protected originDataArray: T[] = [];
totalCount(): number {
return this.originDataArray.length;
}
getAllData(): T[] {
return this.originDataArray;
}
getData(index: number) {
return this.originDataArray[index];
}
addData(index: number, data: T): void {
this.originDataArray.splice(index, 0, data);
this.notifyDataAdd(index);
}
pushData(data: T): void {
this.originDataArray.push(data);
this.notifyDataAdd(this.originDataArray.length - 1);
}
pushDataArray(...items: T[]): void {
for (let data of items) {
this.originDataArray.push(data);
this.notifyDataAdd(this.originDataArray.length - 1);
}
}
deleteDataUseContent(data: T): void {
let delIndex: number = -1;
for (let index = 0; index < this.originDataArray.length; index++) {
const element = this.originDataArray[index];
if (data === element) {
delIndex = index;
}
}
if (delIndex != -1) {
this.deleteData(delIndex);
}
}
deleteData(index: number): void {
this.originDataArray.splice(index, 1);
this.notifyDataDelete(index);
}
setData(dataArray?: T[]) {
if (dataArray) {
this.originDataArray = dataArray;
} else {
this.originDataArray = [];
}
this.notifyDataReload();
}
registerDataChangeListener(listener: DataChangeListener): void {
if (this.listeners.indexOf(listener) < 0) {
this.listeners.push(listener);
}
}
unregisterDataChangeListener(listener: DataChangeListener): void {
const pos = this.listeners.indexOf(listener);
if (pos >= 0) {
this.listeners.splice(pos, 1);
}
}
notifyDataReload() {
this.listeners.forEach(listener => {
listener.onDataReloaded();
});
}
notifyDataAdd(index: number) {
this.listeners.forEach(listener => {
listener.onDataAdd(index);
});
}
notifyDataMove(from: number, to: number) {
this.listeners.forEach(listener => {
listener.onDataMove(from, to);
});
}
notifyDataDelete(index: number) {
this.listeners.forEach(listener => {
listener.onDataDelete(index);
});
}
}
interface DataItem {
sticky: string;
sub: string [];
} import { window } from '@kit.ArkUI';
import { common } from '@kit.AbilityKit';
const leftItemWidth = 100;
const rightItemWidth = 100;
const itemHeight = 90;
const groupHeight = 50;
@Component
struct ItemComponent {
private arr: string[] = [
'1', '2', '3', '4', '5', '6', '7', '8'];
dataSource = new CommonDataSource<string>();
scroller?: Scroller = undefined;
scrollCallBack?: (param: number) => void;
remainOffsetCallBack?: (param: number) => void;
aboutToAppear(): void {
let context: common.UIAbilityContext = this.getUIContext().getHostContext() as common.UIAbilityContext;
window.getLastWindow(context).then((lastWindow) => {
lastWindow.setWindowLayoutFullScreen(true); // 设置页面全屏
});
this.dataSource.setData(this.arr);
}
// 下部分参数列表每行数据List
@Builder
rightSingleLineList() {
List({ scroller: this.scroller }) {
LazyForEach(this.dataSource, (item: string) => {
ListItem() {
Text(item)
.height('100%')
.width('100%')
.fontSize(16)
.textAlign(TextAlign.Start)
.borderRadius(0)
.padding(10)
.backgroundColor(0xFFFFFF);
}
.width(rightItemWidth);
}, (item: string) => item);
}
.height('100%')
.width('100%')
.layoutWeight(1)
.listDirection(Axis.Horizontal)
.scrollBar(BarState.Off)
.friction(0.6)
.divider({ strokeWidth: 0.5, color: 0xeeeeee }) // 每行之间的分界线
.edgeEffect(EdgeEffect.None) // 边缘效果设置为Spring
.onDidScroll(() => {
// 通过callBack回调行在横向滚动时,Scroller当前的offset
if (this.remainOffsetCallBack) {
this.remainOffsetCallBack(this.scroller!.currentOffset().xOffset);
}
})
.onScrollFrameBegin((offset: number) => {
if (this.scrollCallBack) {
this.scrollCallBack(this.scroller!.currentOffset().xOffset + offset);
}
return { offsetRemain: offset };
});
}
build() {
Column() {
this.rightSingleLineList();
Line().width('100%').height(0.5).backgroundColor(0xeeeeee);
}.height(itemHeight);
}
}
@Entry
@Component
export struct Compare {
private topRightArr: string[] = [
'苹果', '香蕉', '橙子', '草莓', '芒果', '葡萄', '西瓜', '桃子'];
// 参数行在横向滚动时的offset
@State remainOffset: number = 0;
private bottomRightScroller: Scroller = new Scroller();
private bottomLeftScroller: Scroller = new Scroller();
private topRightScroller: Scroller = new Scroller();
private showDataArray: ShowData[] = [];
private dataSource = new CommonDataSource<ShowData>();
private leftItems: string[] = [
'市场零售价',
'原产地',
'水果等级',
'品种类型',
'上市季节',
'核心品种',
'甜度指数',
'果径大小',
'保鲜方式',
'外形特征',
'果皮类型',
'最佳储存温度',
'成熟周期',
'糖度含量',
'实测酸度',
'保质期限',
'运输成本'
];
aboutToAppear(): void {
this.loadData();
}
private async loadData() {
// 水果数据
let resp: DataItem[] = [
{
'sticky': '营养成分',
'sub': ['热量', '糖分', '维生素', '膳食纤维', '水分含量']
},
{
'sticky': '外观特征',
'sub': ['颜色', '形状', '大小', '重量', '表皮质地']
},
{
'sticky': '口感风味',
'sub': ['甜度', '酸度', '香味', '口感', '汁水']
}
];
for (let i = 0; i < resp.length; i++) {
let item = resp[i];
let showData = new ShowData();
showData.sticky = item.sticky;
showData.sub = item.sub;
showData.sub!.forEach(() => {
let scroller: Scroller = new Scroller();
showData.scrollerArray!.push(scroller);
});
this.showDataArray.push(showData);
}
this.dataSource.setData(this.showDataArray);
}
build() {
Column() {
// 上部分
this.topFixed();
// 下部分
Row() {
this.leftList();
this.rightList();
Line()
.height('100%')
.width(0.5)
.backgroundColor('#EEEEEE')
.position({ x: leftItemWidth });
}
.justifyContent(FlexAlign.Start)
.alignItems(VerticalAlign.Top);
}
.height('100%')
.justifyContent(FlexAlign.Start)
.alignItems(HorizontalAlign.Start);
}
@Builder
// 修改leftStickyHeader方法
leftStickyHeader(title: string) {
Text(title)
.width(leftItemWidth)
.height(groupHeight)
.backgroundColor('#f1f3f5')
.textAlign(TextAlign.Center)
.border({ width: 1 });
}
// 上部分整体Row(Column + List)
@Builder
topFixed() {
Row() {
// 上部分左侧固定信息
Column() {
Text('水果')
.fontSize(15);
}
.width(leftItemWidth)
.height(100)
.backgroundColor(Color.White)
.justifyContent(FlexAlign.Center)
.alignItems(HorizontalAlign.Start)
.padding(10);
// 分割线
Line()
.height(100)
.width(0.5)
.backgroundColor(0xeeeeee);
// 上部分右侧车型横向滚动列表
List({ scroller: this.topRightScroller/* 绑定Scroller控制器与其他控制器联动*/ }) {
ForEach(this.topRightArr, (item: string) => {
ListItem() {
Text(item)
.height(100)
.width(rightItemWidth)
.fontSize(16)
.textAlign(TextAlign.Start)
.borderRadius(0)
.padding(10)
.backgroundColor(0xFFFFFF);
};
}, (item: string) => item);
}
.listDirection(Axis.Horizontal) // 设置滚动方向为横向滚动
.edgeEffect(EdgeEffect.None)
.divider({ strokeWidth: 0.5, color: 0xeeeeee })
.scrollBar(BarState.Off)
.width('100%')
.height('100%')
.layoutWeight(1)
.onScrollFrameBegin((offset: number) => {
// 关键联动,通过对象保存的Scroller控制器数组遍历保持offset同步
this.dataSource.getAllData().forEach(showData => {
showData.scrollerArray!.forEach(scroller => {
scroller.scrollTo({ xOffset: this.topRightScroller.currentOffset().xOffset + offset, yOffset: 0 });
});
});
return { offsetRemain: offset };
});
}
.height(100)
.width('100%');
}
// 下部分左侧行标题列表纵向滚动List
@Builder
leftList() {
List({ scroller: this.bottomLeftScroller/* 绑定控制器与其他控制器联动*/ }) {
ForEach(this.leftItems, (item: string) => {
ListItem() {
Text(item)
.width(leftItemWidth)
.height(itemHeight)
.textAlign(TextAlign.Center);
};
}, (item: string) => item);
}
.sticky(StickyStyle.Header)
.listDirection(Axis.Vertical)
.scrollBar(BarState.Off)
.friction(0.6)
.edgeEffect(EdgeEffect.None)
.height('calc(100% - 100vp)')
.width(leftItemWidth)
.onScrollFrameBegin((offset: number) => {
// 带动右侧纵向Scroll组件绑定的Scroller控制器同步滑动
this.bottomRightScroller.scrollTo({
xOffset: 0,
yOffset: this.bottomLeftScroller.currentOffset().yOffset + offset,
animation: false
});
return { offsetRemain: offset };
});
}
// 下部分右侧内容显示区域纵向List(ListItem(List))
@Builder
rightList() {
List({ initialIndex: 0, scroller: this.bottomRightScroller }) {
// 通过LazyForEach加载每一行
LazyForEach(this.dataSource, (item: ShowData) => {
ListItemGroup({}) {
ForEach(item.sub, (subItem: string) => {
// 自定义ListItem中包含横向滚动List
ItemComponent({
scroller: item.scrollerArray![item.sub!.indexOf(subItem)],
scrollCallBack: (value) => {
// value为子List横向滚动onScrollFrameBegin回传offset,在手指拖动时保持联动一致
// 顶部车型List跟随联动
this.topRightScroller.scrollTo({ xOffset: value, yOffset: 0 });
// 通过对象保存的Scroller数组跟随保持联动
this.dataSource.getAllData().forEach(showData => {
showData.scrollerArray!.forEach(scroller => {
if (scroller !== item.scrollerArray![item.sub!.indexOf(subItem)]) {
scroller.scrollTo({ xOffset: value, yOffset: 0 });
}
});
});
},
remainOffsetCallBack: (value) => {
// 滚动过程中回传保持同步的offset值
this.remainOffset = value;
}
});
}, (item: string) => item);
};
}, (item: ShowData, index: number) => item.sticky! + index);
}
.sticky(StickyStyle.Header)
.listDirection(Axis.Vertical)
.scrollBar(BarState.Off)
.friction(0.6)
.edgeEffect(EdgeEffect.None)
.onScrollFrameBegin((offset: number) => {
// 内容List纵向滚动带动左侧标题List跟随滚动
this.bottomLeftScroller.scrollTo({
xOffset: 0,
yOffset: this.bottomRightScroller.currentOffset().yOffset + offset,
animation: false
});
return { offsetRemain: offset };
})
.onDidScroll(() => {
// 内容List纵向滚动过程中,每一行中子List的Scroller滚动到remainOffset与已显示的行位置保持一致
this.dataSource.getAllData().forEach(showData => {
showData.scrollerArray!.forEach(scroller => {
scroller.scrollTo({ xOffset: this.remainOffset, yOffset: 0 });
});
});
})
.position({ x: leftItemWidth, y: 0 })
.edgeEffect(EdgeEffect.None) // 边缘效果设置为Spring
.backgroundColor(0xDCDCDC)
.height('calc(100% - 100vp)')
.width('calc(100% - 100vp)');
}
}
class ShowData {
sticky?: string;
sub?: string[];
scrollerArray?: Scroller[] = [];
}
export class CommonDataSource<T> implements IDataSource {
private listeners: DataChangeListener[] = [];
protected originDataArray: T[] = [];
totalCount(): number {
return this.originDataArray.length;
}
getAllData(): T[] {
return this.originDataArray;
}
getData(index: number) {
return this.originDataArray[index];
}
addData(index: number, data: T): void {
this.originDataArray.splice(index, 0, data);
this.notifyDataAdd(index);
}
pushData(data: T): void {
this.originDataArray.push(data);
this.notifyDataAdd(this.originDataArray.length - 1);
}
pushDataArray(...items: T[]): void {
for (let data of items) {
this.originDataArray.push(data);
this.notifyDataAdd(this.originDataArray.length - 1);
}
}
deleteDataUseContent(data: T): void {
let delIndex: number = -1;
for (let index = 0; index < this.originDataArray.length; index++) {
const element = this.originDataArray[index];
if (data === element) {
delIndex = index;
}
}
if (delIndex != -1) {
this.deleteData(delIndex);
}
}
deleteData(index: number): void {
this.originDataArray.splice(index, 1);
this.notifyDataDelete(index);
}
setData(dataArray?: T[]) {
if (dataArray) {
this.originDataArray = dataArray;
} else {
this.originDataArray = [];
}
this.notifyDataReload();
}
registerDataChangeListener(listener: DataChangeListener): void {
if (this.listeners.indexOf(listener) < 0) {
this.listeners.push(listener);
}
}
unregisterDataChangeListener(listener: DataChangeListener): void {
const pos = this.listeners.indexOf(listener);
if (pos >= 0) {
this.listeners.splice(pos, 1);
}
}
notifyDataReload() {
this.listeners.forEach(listener => {
listener.onDataReloaded();
});
}
notifyDataAdd(index: number) {
this.listeners.forEach(listener => {
listener.onDataAdd(index);
});
}
notifyDataMove(from: number, to: number) {
this.listeners.forEach(listener => {
listener.onDataMove(from, to);
});
}
notifyDataDelete(index: number) {
this.listeners.forEach(listener => {
listener.onDataDelete(index);
});
}
}
interface DataItem {
sticky: string;
sub: string [];
}