智能客服
你问我答,随时在线为你解决问题
如何在新闻等列表页面中实现可展开的缩略图?

- @Entry
- @Component
- struct ExpandableThumbnailDemo {
- @State arr: string[] = ['国内新闻', '国际新闻', '生活娱乐', '信息世界'];
-
- build() {
- Scroll() {
- Column() {
- ForEach(this.arr, (item: string, index: number) => {
- scrollItems({ text: item, colorID: index }).transition({ type: TransitionType.All });
- }, (value: string) => value);
- };
- }
- .width('100%')
- .height('100%')
- .expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.BOTTOM])
- .align(Alignment.Top);
- }
- }
-
- @Component
- struct scrollItems {
- @State flag: boolean = true;
- text: string = '';
- colorID: number = 0;
- color: string = ((index: number): string => {
- const colors: string[] = ['255,0,0', '255,0,0', '255,0,0', '255,0,0'];
- return colors[index];
- })(this.colorID);
-
- build() {
- Column() {
- Text(this.text)
- .width('100%')
- .height(25)
- .margin({ bottom: 3 })
- .fontColor(this.color);
- Flex({
- direction: this.flag ? FlexDirection.Row : FlexDirection.Column, // 横向布局和纵向布局切换
- justifyContent: FlexAlign.Start,
- alignItems: ItemAlign.Start
- }) {
- Column() {
- Image($r('app.media.startIcon')); // 此处仅为样例,请开发者更换为可用图片
- }
- .width(this.flag ? 80 : '100%')
- .height(this.flag ? 80 : 240)
- .borderRadius(4)
- .backgroundColor('#ffadcfff')
- .margin({ right: 7, bottom: 7 });
-
- Column() {
- Row() {
- Text('国内发生重大新闻');
- }
- .width('100%')
- .height(20)
- .backgroundColor('#ffadcfff')
- .borderRadius(4)
- .padding({ left: 10 });
-
- Row() {
- Text('国外发生重大新闻');
- }
- .width('70%')
- .height(20)
- .backgroundColor('#FFE8F0FF')
- .borderRadius(4)
- .padding({ left: 10 })
- .margin({ top: 10 });
-
- Row() {
- Text('国外发生了重大新闻');
- }
- .width('75%')
- .height(20)
- .backgroundColor('#FFE8F0FF')
- .padding({ left: 10 })
- .borderRadius(4)
- .margin({ top: 10 });
-
- if (!this.flag) {
- Row() {
- Text('国内发生了重大新闻');
- }
- .width('70%')
- .height(20)
- .backgroundColor('#FFE8F0FF')
- .borderRadius(4)
- .padding({ left: 10 })
- .margin({ top: 10 });
-
- Row() {
- Text('国外发生大新闻');
- } // 最小每行内容
- .width('75%')
- .height(20)
- .backgroundColor('#FFE8F0FF')
- .padding({ left: 10 })
- .borderRadius(4)
- .margin({ top: 10 });
-
- Row() {
- Text('国内发生大新闻');
- } // 最小每行内容
- .width('75%')
- .height(20)
- .backgroundColor('#FFE8F0FF')
- .padding({ left: 10 })
- .borderRadius(4)
- .margin({ top: 10 });
- }
- }
- .alignItems(HorizontalAlign.Start);
- }
- .width('100%')
- .height(this.flag ? 95 : 430)
- .padding({
- top: 7,
- left: 7,
- bottom: 7,
- right: 7
- })
- .margin({ bottom: 5 })
- .borderRadius(8)
- .shadow({ radius: 10, color: 'rgba(0,0,0,1)' })
- .onClick(() => {
- // 布局切换动画效果
- this.getUIContext().animateTo({ duration: 500, curve: Curve.FastOutSlowIn }, () => {
- this.flag = !this.flag;
- });
- });
- }
- .width('100%')
- .padding({ left: 20, right: 20 })
- .margin({ top: 20 });
- }
- }
智能客服
你问我答,随时在线为你解决问题
合作咨询
我们的专家服务团队将竭诚为您提供专业的合作咨询服务
解决方案
精准高效的一站式服务支持,助力开发者商业成功