|
@@ -1,24 +1,86 @@
|
|
|
-import { ToolsHelper } from '../utils/ToolsHelper'
|
|
|
+import { ToolsHelper } from '../../utils/ToolsHelper'
|
|
|
+import { inspector } from '@kit.ArkUI'
|
|
|
+import { RefreshController } from './RefreshController'
|
|
|
|
|
|
@Component
|
|
|
export struct RefreshView {
|
|
|
@Link isLoading: boolean
|
|
|
- @Prop data: Array<ESObject>
|
|
|
+ @Prop @Watch('change') data: Array<ESObject>
|
|
|
@Require keyGenerator?: (item: ESObject, index: number) => string
|
|
|
+ //是否自动调用`onRefresh`方法,默认为`true`,加载组件会自动调用一次`onRefresh`
|
|
|
init: boolean = true
|
|
|
+ // 第一次加载完成,是否定位到底部,默认`false`,定位到顶部
|
|
|
+ positioningToBottom: boolean = false
|
|
|
pageSize: number = 10
|
|
|
onLoadMore?: (pageNum: number) => void
|
|
|
onRefresh?: () => void
|
|
|
+ controller: RefreshController | null = null
|
|
|
private startY: number = 0
|
|
|
private endY: number = 0
|
|
|
private lastNum: number = 0
|
|
|
private _openMore: boolean = false
|
|
|
private _oTime: number = 0
|
|
|
+ private _listener: inspector.ComponentObserver = inspector.createComponentObserver('Refresh_View_List');
|
|
|
+ private _scroller: Scroller = new Scroller()
|
|
|
+ private onDrawComplete = () => {
|
|
|
+ this._isLoad = false
|
|
|
+ this.isLoading = false
|
|
|
+ if (!this._firstFinish && this.positioningToBottom) {
|
|
|
+ this._firstFinish = true
|
|
|
+ this.toBottom()
|
|
|
+ } else {
|
|
|
+ if (this._toTop) {
|
|
|
+ this.toTop()
|
|
|
+ this._toTop = false
|
|
|
+ } else if (this._toBottom) {
|
|
|
+ this.toBottom()
|
|
|
+ this._toBottom = false
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+ private _firstFinish = false
|
|
|
+ private _isLoad = false
|
|
|
+ private _toTop = false
|
|
|
+ private _toBottom = false
|
|
|
+
|
|
|
+ change() {
|
|
|
+ this._isLoad = true
|
|
|
+ }
|
|
|
|
|
|
aboutToAppear(): void {
|
|
|
if (this.init) {
|
|
|
this.onRefresh && this.onRefresh()
|
|
|
}
|
|
|
+ if (this.controller) {
|
|
|
+ this.controller.toTop = (smooth?: boolean) => {
|
|
|
+ this.toTop(smooth)
|
|
|
+ }
|
|
|
+ this.controller.toBottom = (smooth?: boolean) => {
|
|
|
+ this.toBottom(smooth)
|
|
|
+ }
|
|
|
+ this.controller.isAtEnd = () => this._scroller.isAtEnd()
|
|
|
+ }
|
|
|
+ this._listener.on('draw', this.onDrawComplete)
|
|
|
+ }
|
|
|
+
|
|
|
+ toTop(smooth?: boolean) {
|
|
|
+ if (!this._isLoad) {
|
|
|
+ this._scroller.scrollToIndex(0, smooth, ScrollAlign.START)
|
|
|
+ } else {
|
|
|
+ this._toTop = true
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ toBottom(smooth?: boolean) {
|
|
|
+ if (!this._isLoad) {
|
|
|
+ this._scroller.scrollToIndex(this.data.length - 1, smooth, ScrollAlign.END)
|
|
|
+ } else {
|
|
|
+ this._toBottom = true
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ aboutToDisappear(): void {
|
|
|
+ this._listener.off('draw', this.onDrawComplete);
|
|
|
}
|
|
|
|
|
|
// 使用父组件@Builder装饰的方法初始化子组件@BuilderParam
|
|
@@ -28,7 +90,9 @@ export struct RefreshView {
|
|
|
|
|
|
Refresh({ refreshing: $$this.isLoading }) {
|
|
|
if (this.data && this.data.length > 0) {
|
|
|
- List() {
|
|
|
+ List({
|
|
|
+ scroller: this._scroller,
|
|
|
+ }) {
|
|
|
ForEach(this.data ?? [], (item: ESObject, index: number) => {
|
|
|
ListItem() {
|
|
|
this.customBuilderParam(item, index)
|
|
@@ -63,6 +127,7 @@ export struct RefreshView {
|
|
|
.onScrollIndex((first: number, last: number) => {
|
|
|
this.lastNum = last + 1
|
|
|
})
|
|
|
+ .id('Refresh_View_List')
|
|
|
.width('100%')
|
|
|
.height('100%')
|
|
|
.alignListItem(ListItemAlign.Center)
|