@Entry
@ComponentV2
struct ScrollControlDemo {
private scroller: Scroller = new Scroller()
build() {
Column() {
Row({ space: 12 }) {
Button('回到顶部').onClick(() => {
this.scroller.scrollTo({ xOffset: 0, yOffset: 0 })
})
Button('滚到底部').onClick(() => {
this.scroller.scrollEdge(Edge.Bottom)
}).margin({ bottom: 12 })
}
RcList({
rcListHeight: 400,
rcListScrollable: true,
rcListScroller: this.scroller
}) {
ForEach(Array.from<number, number>({ length: 20 }, (_: number, i: number): number => i), (index: number) => {
RcListItem({
rcListItemTitle: `列表项 ${index + 1}`,
rcListItemNote: `第 ${index + 1} 条数据`
})
}, (index: number): string => `item-${index}`)
}.padding(16)
}
}
}
import { RcList, RcListItem } from 'rchoui'
@Entry
@ComponentV2
struct RcListInteractionDemo {
@Local switchA: boolean = true
@Local switchB: boolean = false
@Local switchC: boolean = true
@Local logs: string[] = []
private scroller: Scroller = new Scroller()
private addLog(msg: string): void {
const time = new Date().toLocaleTimeString()
this.logs = [`[${time}] ${msg}`, ...this.logs.slice(0, 9)]
}
build() {
Column({ space: 0 }) {
Column() {
Text('操作日志').fontSize(14).fontWeight(FontWeight.Medium).margin({ bottom: 8 })
ForEach(this.logs.slice(0, 5), (log: string) => {
Text(log).fontSize(12).fontColor('#606266').width('100%')
}, (log: string): string => log)
if (this.logs.length === 0) {
Text('暂无操作记录').fontSize(12).fontColor('#c0c4cc')
}
}.width('100%').padding(12).backgroundColor('#fff').border({ width: { bottom: 1 }, color: '#e4e7ed', style: BorderStyle.Solid })
Scroll() {
Column({ space: 16 }) {
Text('点击交互').fontSize(16).fontWeight(FontWeight.Medium).margin({ top: 16, bottom: 4 })
RcList({ rcListBorder: true }) {
RcListItem({
rcListItemTitle: '普通点击项',
rcListItemNote: '点击后记录日志',
rcListItemClickable: true,
onRcListItemClick: () => {
this.addLog('点击了「普通点击项」')
}
})
RcListItem({
rcListItemTitle: '带右侧文字',
rcListItemRightText: '详情',
rcListItemClickable: true,
onRcListItemClick: () => {
this.addLog('点击了「带右侧文字」,即将跳转')
}
})
RcListItem({
rcListItemTitle: '无按压反馈',
rcListItemNote: '不设置 clickable,无视觉反馈',
rcListItemClickable: false,
onRcListItemClick: () => {
this.addLog('点击了「无按压反馈」,事件仍然触发')
}
})
}
Text('禁用状态保护').fontSize(16).fontWeight(FontWeight.Medium).margin({ bottom: 4 })
RcList({ rcListBorder: true }) {
RcListItem({
rcListItemTitle: '正常可点击',
rcListItemNote: '点击会触发事件',
rcListItemDisabled: false,
rcListItemClickable: true,
onRcListItemClick: () => {
this.addLog('「正常项」被点击')
}
})
RcListItem({
rcListItemTitle: '已禁用(不可点击)',
rcListItemNote: '即使设置了 clickable 也无效',
rcListItemDisabled: true,
rcListItemClickable: true,
onRcListItemClick: () => {
this.addLog('这行永远不会出现在日志中')
}
})
RcListItem({
rcListItemTitle: '已禁用的开关',
rcListItemNote: '开关无法切换',
rcListItemShowSwitch: true,
rcListItemSwitchChecked: true,
rcListItemDisabled: true,
rcListItemShowArrow: false
})
}
Text('开关状态管理').fontSize(16).fontWeight(FontWeight.Medium).margin({ bottom: 4 })
RcList({ rcListBorder: true }) {
RcListItem({
rcListItemTitle: '通知推送',
rcListItemNote: this.switchA ? '已开启' : '已关闭',
rcListItemShowSwitch: true,
rcListItemSwitchChecked: this.switchA,
rcListItemShowArrow: false,
onRcListItemSwitchChange: (checked: boolean) => {
this.switchA = checked
this.addLog(`通知推送:${checked ? '开启' : '关闭'}`)
}
})
RcListItem({
rcListItemTitle: '自动更新',
rcListItemNote: this.switchB ? '已开启' : '已关闭',
rcListItemShowSwitch: true,
rcListItemSwitchChecked: this.switchB,
rcListItemShowArrow: false,
onRcListItemSwitchChange: (checked: boolean) => {
this.switchB = checked
this.addLog(`自动更新:${checked ? '开启' : '关闭'}`)
}
})
RcListItem({
rcListItemTitle: '定位服务',
rcListItemNote: this.switchC ? '已开启' : '已关闭',
rcListItemShowSwitch: true,
rcListItemSwitchChecked: this.switchC,
rcListItemShowArrow: false,
rcListItemShowBorder: false,
onRcListItemSwitchChange: (checked: boolean) => {
this.switchC = checked
this.addLog(`定位服务:${checked ? '开启' : '关闭'}`)
}
})
}
Text('滚动事件监听').fontSize(16).fontWeight(FontWeight.Medium).margin({ bottom: 4 })
Row({ space: 8 }) {
Button('回到顶部').onClick(() => {
this.scroller.scrollEdge(Edge.Top)
this.addLog('手动滚动到顶部')
})
Button('跳到底部').onClick(() => {
this.scroller.scrollEdge(Edge.Bottom)
this.addLog('手动滚动到底部')
})
}
RcList({
rcListHeight: 240,
rcListScrollable: true,
rcListScroller: this.scroller,
rcListScrollBarState: BarState.On,
rcListBorder: true,
onRcListScrollToLower: () => {
this.addLog('滚动到底部,可加载更多')
},
onRcListScrollToUpper: () => {
this.addLog('滚动到顶部,可下拉刷新')
}
}) {
ForEach(Array.from<number, number>({ length: 15 }, (_: number, i: number): number => i), (index: number) => {
RcListItem({
rcListItemTitle: `滚动列表项 ${index + 1}`,
rcListItemNote: `这是第 ${index + 1} 条数据`,
rcListItemRightText: `${index + 1}`,
rcListItemClickable: true,
onRcListItemClick: () => {
this.addLog(`点击了第 ${index + 1} 项`)
}
})
}, (index: number): string => `scroll-item-${index}`)
}
Text('事件演示结束').fontSize(13).fontColor('#909399').textAlign(TextAlign.Center).margin({ top: 8, bottom: 30 })
}.width('100%').padding(16)
}.layoutWeight(1).backgroundColor('#f5f5f5')
}.width('100%').height('100%')
}
}