Ionic中如何实现无需点击即可滑动触发事件的ion-item?
Ionic 列表项滑动触发操作实现方案
1. 修改 person-entry 组件模板
将原有<ion-item>包裹在ion-item-sliding组件内,添加左右滑动触发配置,滑动超过阈值自动触发事件,无需点击侧边按钮:
<ion-item-sliding> <!-- 右滑触发短信操作 --> <ion-item-options side="start" swipeThreshold="50" (ionSwipe)="onSms()"> <ion-item-option color="success"> <ion-icon name="chatbubble" slot="icon-only"></ion-icon> </ion-item-option> </ion-item-options> <!-- 原有条目内容 --> <ion-item (click)="onClick()"> <ion-avatar slot="start"> <img src={{person.icon}}> </ion-avatar> <div> <div class="container"> <ion-label>{{person.lastname}}</ion-label> <ion-label>{{person.firstname}}</ion-label> </div> <div class="container"> <ion-label>{{person.postalCode}}</ion-label> <ion-label>{{person.city}}</ion-label> <ion-label>{{person.address}}</ion-label> </div> </div> </ion-item> <!-- 左滑触发拨号操作 --> <ion-item-options side="end" swipeThreshold="50" (ionSwipe)="onCall()"> <ion-item-option color="primary"> <ion-icon name="call" slot="icon-only"></ion-icon> </ion-item-option> </ion-item-options> </ion-item-sliding>
2. 配置 person-entry 组件事件输出
在组件ts文件中添加事件输出,把滑动操作回调传递给父页面:
import { Component, Input, Output, EventEmitter } from '@angular/core'; // 替换为你自己的Person类型定义路径 import { Person } from './person.interface'; @Component({ selector: 'app-person-entry', templateUrl: './person-entry.component.html', styleUrls: ['./person-entry.component.scss'], }) export class PersonEntryComponent { @Input() person: Person; @Output() call = new EventEmitter<Person>(); @Output() sms = new EventEmitter<Person>(); @Output() click = new EventEmitter<Person>(); onCall() { this.call.emit(this.person); } onSms() { this.sms.emit(this.person); } onClick() { this.click.emit(this.person); } }
3. 父页面绑定滑动事件
修改父页面列表代码,绑定滑动事件实现对应业务逻辑:
<ion-list *ngFor="let person of persons | filter:searchTerm"> <app-person-entry [person]="person" (click)="openModal(person)" (call)="handleCall($event)" (sms)="handleSms($event)" ></app-person-entry> </ion-list>
在父页面ts中补充handleCall拨打电话、handleSms发送短信的业务逻辑即可。
可调整
swipeThreshold属性值修改触发灵敏度,取值1-100代表滑动距离占条目宽度的百分比;如果不需要显示滑动时的图标,可给ion-item-option设置opacity:0样式,完全匹配三星联系人的交互效果。
内容的提问来源于stack exchange,提问作者Daniel Fischer
相关产品推荐
相关产品推荐

