list-view-refresh.uvue 3.7 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"
3
    :refresher-triggered="refresherTriggered" @refresherrefresh="onRefresherrefresh" @refresherabort="onRefresherabort" @refresherrestore="onRefresherrestore" @refresherpulling="onRefresherpulling">
shutao-dc's avatar
shutao-dc 已提交
4 5 6 7 8 9 10 11 12
    <list-item class="item">
      <text>向下滑动触发下拉刷新</text>
    </list-item>
    <list-item v-for="index in item_count" class="item">
      <text>item-------{{index}}</text>
    </list-item>
  </list-view>
</template>

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

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