scroll-view-refresher.uvue 4.6 KB
Newer Older
DCloud-WZF's avatar
DCloud-WZF 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
<template>
  <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"
      @refresherpulling="onRefresherpulling" @scrolltolower="onScrolltolower" :show-scrollbar="showScrollbar">
      <view v-for="key in scrollData" :key="key">
        <view class="scroll-item">
          <text class="scroll-item-title">{{key}}</text>
        </view>
      </view>
    </scroll-view>
  </view>
</template>
<script>
  type RefresherEventTest = {
    type : string;
    target : UniElement | null;
    currentTarget : UniElement | null;
    dy : number;
  }
  export default {
    data() {
      return {
        scrollData: [] as Array<string>,
        refresherTriggered: false,
        refresherrefresh: false,
        refresherrefreshTimes: 0,
        showScrollbar: false,
        // 自动化测试使用
        refresherrefreshTest: "",
        onRefresherabortTest: "",
        onRefresherrestoreTest: "",
        onRefresherpullingTest: ""
      };
    },
    onLoad() {
      let lists : Array<string> = []
      for (let i = 0; i < 20; i++) {
        lists.push("item---" + i)
      }
      this.scrollData = lists
    },

    methods: {
      onRefresherrefresh(e : UniRefresherEvent) {
        this.refresherrefresh = true
        console.log("onRefresherrefresh------下拉刷新触发")
        this.checkEventTest({
          type: e.type,
          target: e.target,
          currentTarget: e.currentTarget,
          dy: e.detail.dy,
        } as RefresherEventTest, 'refresherrefresh')
        this.refresherTriggered = true
        this.refresherrefreshTimes++
        setTimeout(() => {
          this.refresherTriggered = false
        }, 1500)
      },
      onRefresherabort(e : UniRefresherEvent) {
        console.log("onRefresherabort------下拉刷新被中止")
        this.checkEventTest({
          type: e.type,
          target: e.target,
          currentTarget: e.currentTarget,
          dy: e.detail.dy,
        } as RefresherEventTest, 'refresherabort')
      },
      onRefresherrestore(e : UniRefresherEvent) {
        this.refresherrefresh = false
        console.log("onRefresherrestore------下拉刷新被复位")
        this.checkEventTest({
          type: e.type,
          target: e.target,
          currentTarget: e.currentTarget,
          dy: e.detail.dy,
        } as RefresherEventTest, 'refresherrestore')
      },
      onRefresherpulling(e : UniRefresherEvent) {
        console.log("onRefresherpulling------拉刷新控件被下拉-dy=" + e.detail.dy)
        this.checkEventTest({
          type: e.type,
          target: e.target,
          currentTarget: e.currentTarget,
          dy: e.detail.dy,
        } as RefresherEventTest, 'refresherpulling')
      },
      onScrolltolower(e : UniScrollToLowerEvent) {
        console.log("onScrolltolower 滚动到底部-----" + e.detail.direction)
      },
      // 自动化测试专用(由于事件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;
        }
      }
    }
  };
</script>

<style>
  .container {
    display: flex;
    flex-direction: column;
    flex: 1;
  }

  .scroll {
    background-color: #eee;
    position: relative;
    width: 100%;
    flex: 1;
    display: flex;
    flex-direction: column;
    border-color: red;
  }

  .scroll-item {
    margin-left: 6px;
    margin-right: 6px;
    margin-top: 6px;
    background-color: #fff;
    border-radius: 4px;
  }

  .scroll-item-title {
    width: 100%;
    height: 60px;
    line-height: 60px;
    text-align: center;
    color: #555;
  }
149
</style>