list-view-refresh.uvue 3.8 KB
Newer Older
shutao-dc's avatar
shutao-dc 已提交
1 2
<template>
  <list-view v-if="list_show" id="listview" style="flex: 1;" show-scrollbar=false :refresher-enabled="true"
DCloud-WZF's avatar
DCloud-WZF 已提交
3 4 5 6 7
    :refresher-triggered="refresherTriggered" @refresherrefresh="onRefresherrefresh" @refresherabort="onRefresherabort"
    @refresherrestore="onRefresherrestore" @refresherpulling="onRefresherpulling">
    <list-item class="item">
      <text>向下滑动触发下拉刷新</text>
    </list-item>
shutao-dc's avatar
shutao-dc 已提交
8 9 10 11 12 13
    <list-item v-for="index in item_count" class="item">
      <text>item-------{{index}}</text>
    </list-item>
  </list-view>
</template>

DCloud-WZF's avatar
DCloud-WZF 已提交
14 15 16 17 18 19
<script>
  type RefresherEventTest = {
    type : string;
    target : UniElement | null;
    currentTarget : UniElement | null;
    dy : number;
20
  }
shutao-dc's avatar
shutao-dc 已提交
21 22 23 24 25
  export default {
    data() {
      return {
        item_count: 20,
        list_show: false,
DCloud-WZF's avatar
DCloud-WZF 已提交
26 27 28 29 30 31
        refresherTriggered: true,
        // 自动化测试使用
        refresherrefreshTest: "",
        onRefresherabortTest: "",
        onRefresherrestoreTest: "",
        onRefresherpullingTest: ""
shutao-dc's avatar
shutao-dc 已提交
32 33
      }
    },
DCloud-WZF's avatar
DCloud-WZF 已提交
34
    onLoad() {
shutao-dc's avatar
shutao-dc 已提交
35 36 37 38 39 40
      //延迟显示list-view
      setTimeout(() => {
        this.list_show = true;
      }, 500)
    },
    methods: {
DCloud-WZF's avatar
DCloud-WZF 已提交
41
      onRefresherrefresh(e : UniRefresherEvent) {
42
        console.log('refresherrefresh-----下拉刷新被触发', e.detail.dy);
DCloud-WZF's avatar
DCloud-WZF 已提交
43 44 45 46 47 48 49
        this.refresherTriggered = true;
        this.checkEventTest({
          type: e.type,
          target: e.target,
          currentTarget: e.currentTarget,
          dy: e.detail.dy,
        } as RefresherEventTest, 'refresherrefresh')
shutao-dc's avatar
shutao-dc 已提交
50 51 52
        setTimeout(() => {
          this.refresherTriggered = false;
        }, 1000)
DCloud-WZF's avatar
DCloud-WZF 已提交
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
      },
      onRefresherabort(e : UniRefresherEvent) {
        console.log("onRefresherabort------下拉刷新被中止", e.detail)
        this.checkEventTest({
          type: e.type,
          target: e.target,
          currentTarget: e.currentTarget,
          dy: e.detail.dy,
        } as RefresherEventTest, 'refresherabort')
      },
      onRefresherrestore(e : UniRefresherEvent) {
        console.log("onRefresherrestore------下拉刷新被复位", e.detail.dy)
        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')
      },
      // 自动化测试专用(由于事件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;
        }
      }
shutao-dc's avatar
shutao-dc 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    }
  }
</script>
<style>
  .item {
    padding: 15px;
    margin: 0 0 5px 0;
    background-color: #fff;
    border-radius: 5px;
  }

  .button_item {
    width: 200px;
  }
</style>