scroll-view-refresher.uvue 4.4 KB
Newer Older
1
<template>
H
hdx 已提交
2 3 4 5
  <view class="container">
    <page-head title="scroll-view 下拉刷新"></page-head>
    <scroll-view class="scroll" refresher-enabled=true :refresher-triggered="refresherTriggered"
      @refresherrefresh="onRefresherrefresh" @refresherabort="onRefresherabort" @refresherrestore="onRefresherrestore"
6
      @refresherpulling="onRefresherpulling" @scrolltolower="onScrolltolower" :show-scrollbar="showScrollbar">
H
hdx 已提交
7 8 9 10 11 12 13
      <view v-for="key in scrollData" :key="key">
        <view class="scroll-item">
          <text class="scroll-item-title">{{key}}</text>
        </view>
      </view>
    </scroll-view>
  </view>
14 15
</template>
<script>
16 17 18 19 20 21
  type RefresherEventTest = {
    type: string;
    target: UniElement | null;
    currentTarget: UniElement | null;
    dy:number;
  }
H
hdx 已提交
22 23 24 25 26
  export default {
    data() {
      return {
        scrollData: [] as Array<string>,
        refresherTriggered: false,
27
        refresherrefresh: false,
雪洛's avatar
雪洛 已提交
28
        refresherrefreshTimes: 0,
29
        showScrollbar: false,
30 31 32 33 34
        // 自动化测试使用
        refresherrefreshTest:"",
        onRefresherabortTest:"",
        onRefresherrestoreTest:"",
        onRefresherpullingTest:""
H
hdx 已提交
35 36 37 38 39 40 41 42 43
      };
    },
    onLoad() {
      let lists : Array<string> = []
      for (let i = 0; i < 20; i++) {
        lists.push("item---" + i)
      }
      this.scrollData = lists
    },
44

H
hdx 已提交
45
    methods: {
46
      onRefresherrefresh(e : UniRefresherEvent)) {
H
hdx 已提交
47
        this.refresherrefresh = true
48 49 50 51 52 53 54
        console.log("onRefresherrefresh------下拉刷新触发")
        this.checkEventTest({
          type:e.type,
          target:e.target,
          currentTarget:e.currentTarget,
          dy:e.detail.dy,
        } as RefresherEventTest,'refresherrefresh')
H
hdx 已提交
55
        this.refresherTriggered = true
雪洛's avatar
雪洛 已提交
56
        this.refresherrefreshTimes++
H
hdx 已提交
57 58 59 60
        setTimeout(() => {
          this.refresherTriggered = false
        }, 1500)
      },
61
      onRefresherabort(e : UniRefresherEvent)) {
H
hdx 已提交
62
        console.log("onRefresherabort------下拉刷新被中止")
63 64 65 66 67 68
        this.checkEventTest({
          type:e.type,
          target:e.target,
          currentTarget:e.currentTarget,
          dy:e.detail.dy,
        } as RefresherEventTest,'refresherabort')
H
hdx 已提交
69
      },
70
      onRefresherrestore(e : UniRefresherEvent)) {
H
hdx 已提交
71 72
        this.refresherrefresh = false
        console.log("onRefresherrestore------下拉刷新被复位")
73 74 75 76 77 78
        this.checkEventTest({
          type:e.type,
          target:e.target,
          currentTarget:e.currentTarget,
          dy:e.detail.dy,
        } as RefresherEventTest,'refresherrestore')
H
hdx 已提交
79
      },
80
      onRefresherpulling(e : UniRefresherEvent)) {
81
        console.log("onRefresherpulling------拉刷新控件被下拉-dy=" + e.detail.dy)
82 83 84 85 86 87
        this.checkEventTest({
          type:e.type,
          target:e.target,
          currentTarget:e.currentTarget,
          dy:e.detail.dy,
        } as RefresherEventTest,'refresherpulling')
H
hdx 已提交
88
      },
89
      onScrolltolower(e : UniScrollToLowerEvent) {
H
hdx 已提交
90
        console.log("onScrolltolower 滚动到底部-----" + e.detail.direction)
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
      },
      // 自动化测试专用(由于事件event参数对象中存在循环引用,在ios端JSON.stringify报错,自动化测试无法page.data获取)
      checkEventTest(e:RefresherEventTest, eventName:String){
        const isPass = e.type === eventName && e.target instanceof UniElement && e.currentTarget instanceof UniElement && e.dy>0;
        const result = isPass ? `${eventName}:Success` : `${eventName}:Fail`;
        switch (eventName){
          case 'refresherrefresh':
            this.refresherrefreshTest = result
            break;
          case 'refresherpulling':
            this.onRefresherpullingTest =  result
            break;
          case 'refresherrestore':
            this.onRefresherrestoreTest =  result
            break;
          case 'refresherabort':
            this.onRefresherabortTest =  result
            break;
          default:
            break;
        }
H
hdx 已提交
112 113 114
      }
    }
  };
115 116 117
</script>

<style>
H
hdx 已提交
118 119 120 121 122
  .container {
    display: flex;
    flex-direction: column;
    flex: 1;
  }
123

H
hdx 已提交
124 125 126 127 128 129 130 131 132
  .scroll {
    background-color: #eee;
    position: relative;
    width: 100%;
    flex: 1;
    display: flex;
    flex-direction: column;
    border-color: red;
  }
133

H
hdx 已提交
134 135 136 137 138 139 140
  .scroll-item {
    margin-left: 6px;
    margin-right: 6px;
    margin-top: 6px;
    background-color: #fff;
    border-radius: 4px;
  }
141

H
hdx 已提交
142 143 144 145 146 147 148
  .scroll-item-title {
    width: 100%;
    height: 60px;
    line-height: 60px;
    text-align: center;
    color: #555;
  }
149
</style>