list-view.uvue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <script>
  2. type ScrollEventTest = {
  3. type : string;
  4. target : UniElement | null;
  5. currentTarget : UniElement | null;
  6. direction ?: string
  7. }
  8. import { ItemType } from '@/components/enum-data/enum-data-types'
  9. export default {
  10. data() {
  11. return {
  12. refresher_triggered_boolean: false,
  13. refresher_enabled_boolean: false,
  14. scroll_with_animation_boolean: false,
  15. show_scrollbar_boolean: false,
  16. bounces_boolean: true,
  17. scroll_y_boolean: true,
  18. scroll_x_boolean: false,
  19. scroll_direction: "vertical",
  20. upper_threshold_input: 50,
  21. lower_threshold_input: 50,
  22. scroll_top_input: 0,
  23. scroll_left_input: 0,
  24. refresher_background_input: "#FFF",
  25. scrollData: [] as Array<string>,
  26. size_enum: [{ "value": 0, "name": "item---0" }, { "value": 3, "name": "item---3" }] as ItemType[],
  27. scrollIntoView: "",
  28. refresherrefresh: false,
  29. refresher_default_style_input: "black",
  30. text: ['继续下拉执行刷新', '释放立即刷新', '刷新中', ""],
  31. state: 3,
  32. reset: true,
  33. // 自动化测试
  34. isScrollTest: '',
  35. isScrolltolowerTest: '',
  36. isScrolltoupperTest: '',
  37. scrollDetailTest: null as UniScrollEventDetail | null,
  38. scrollEndDetailTest: null as UniScrollEventDetail | null,
  39. }
  40. },
  41. onLoad() {
  42. let lists : Array<string> = []
  43. for (let i = 0; i < 10; i++) {
  44. lists.push("item---" + i)
  45. }
  46. this.scrollData = lists
  47. },
  48. methods: {
  49. list_view_click() { console.log("组件被点击时触发") },
  50. list_view_touchstart() { console.log("手指触摸动作开始") },
  51. list_view_touchmove() { console.log("手指触摸后移动") },
  52. list_view_touchcancel() { console.log("手指触摸动作被打断,如来电提醒,弹窗") },
  53. list_view_touchend() { console.log("手指触摸动作结束") },
  54. list_view_tap() { console.log("手指触摸后马上离开") },
  55. list_view_longpress() { console.log("如果一个组件被绑定了 longpress 事件,那么当用户长按这个组件时,该事件将会被触发。") },
  56. list_view_refresherpulling(e : RefresherEvent) {
  57. console.log("下拉刷新控件被下拉")
  58. if (this.reset) {
  59. if (e.detail.dy > 45) {
  60. this.state = 1
  61. } else {
  62. this.state = 0
  63. }
  64. }
  65. },
  66. list_view_refresherrefresh() {
  67. console.log("下拉刷新被触发 ")
  68. this.refresherrefresh = true
  69. this.refresher_triggered_boolean = true
  70. this.state = 2
  71. this.reset = false;
  72. setTimeout(() => {
  73. this.refresher_triggered_boolean = false
  74. }, 1500)
  75. },
  76. list_view_refresherrestore() {
  77. this.refresherrefresh = false
  78. this.state = 3
  79. this.reset = true
  80. console.log("下拉刷新被复位")
  81. },
  82. list_view_refresherabort() { console.log("下拉刷新被中止") },
  83. list_view_scrolltoupper(e : UniScrollToUpperEvent) {
  84. console.log("滚动到顶部/左边,会触发 scrolltoupper 事件 direction=" + e.detail.direction)
  85. this.checkEventTest({
  86. type: e.type,
  87. target: e.target,
  88. currentTarget: e.currentTarget,
  89. direction: e.detail.direction,
  90. } as ScrollEventTest, 'scrolltoupper')
  91. },
  92. list_view_scrolltolower(e : UniScrollToLowerEvent) {
  93. console.log("滚动到底部/右边,会触发 scrolltolower 事件 direction=" + e.detail.direction)
  94. this.checkEventTest({
  95. type: e.type,
  96. target: e.target,
  97. currentTarget: e.currentTarget,
  98. direction: e.detail.direction,
  99. } as ScrollEventTest, 'scrolltolower')
  100. },
  101. list_view_scroll(e : UniScrollEvent) {
  102. console.log("滚动时触发,event.detail = {scrollLeft, scrollTop, scrollHeight, scrollWidth, deltaX, deltaY}")
  103. this.scrollDetailTest = e.detail
  104. this.checkEventTest({
  105. type: e.type,
  106. target: e.target,
  107. currentTarget: e.currentTarget
  108. } as ScrollEventTest, 'scroll')
  109. },
  110. list_view_scrollend(e : UniScrollEvent) {
  111. console.log("滚动结束时触发", e.detail)
  112. this.scrollEndDetailTest = e.detail
  113. this.checkEventTest({
  114. type: e.type,
  115. target: e.target,
  116. currentTarget: e.currentTarget
  117. } as ScrollEventTest, 'scrollend')
  118. },
  119. list_item_click() { console.log("list-item组件被点击时触发") },
  120. list_item_touchstart() { console.log("手指触摸list-item组件动作开始") },
  121. list_item_touchmove() { console.log("手指触摸list-item组件后移动") },
  122. list_item_touchcancel() { console.log("手指触摸list-item组件动作被打断,如来电提醒,弹窗") },
  123. list_item_touchend() { console.log("手指触摸list-item组件动作结束") },
  124. list_item_tap() { console.log("手指触摸list-item组件后马上离开") },
  125. list_item_longpress() { console.log("list-item组件被绑定了 longpress 事件,那么当用户长按这个组件时,该事件将会被触发。") },
  126. change_refresher_triggered_boolean(checked : boolean) { this.refresher_triggered_boolean = checked },
  127. change_refresher_enabled_boolean(checked : boolean) { this.refresher_enabled_boolean = checked },
  128. change_scroll_with_animation_boolean(checked : boolean) { this.scroll_with_animation_boolean = checked },
  129. change_show_scrollbar_boolean(checked : boolean) { this.show_scrollbar_boolean = checked },
  130. change_bounces_boolean(checked : boolean) { this.bounces_boolean = checked },
  131. change_scroll_y_boolean(checked : boolean) {
  132. this.scroll_y_boolean = checked
  133. this.change_scroll_direction()
  134. },
  135. change_scroll_x_boolean(checked : boolean) {
  136. this.scroll_x_boolean = checked
  137. this.change_scroll_direction()
  138. },
  139. change_scroll_direction() {
  140. if (this.scroll_y_boolean && this.scroll_x_boolean || this.scroll_y_boolean) {
  141. this.scroll_direction = "vertical"
  142. } else if (!this.scroll_y_boolean && !this.scroll_x_boolean) {
  143. this.scroll_direction = "none"
  144. } else if (!this.scroll_y_boolean && this.scroll_x_boolean) {
  145. this.scroll_direction = "horizontal"
  146. }
  147. },
  148. confirm_upper_threshold_input(value : number) { this.upper_threshold_input = value },
  149. confirm_lower_threshold_input(value : number) { this.lower_threshold_input = value },
  150. confirm_scroll_top_input(value : number) { this.scroll_top_input = value },
  151. confirm_scroll_left_input(value : number) { this.scroll_left_input = value },
  152. confirm_refresher_background_input(value : string) { this.refresher_background_input = value },
  153. item_change_size_enum(index : number) { this.scrollIntoView = "item---" + index },
  154. //自动化测试专用
  155. setScrollIntoView(id : string) { this.scrollIntoView = id },
  156. // 自动化测试专用(由于事件event参数对象中存在循环引用,在ios端JSON.stringify报错,自动化测试无法page.data获取)
  157. checkEventTest(e : ScrollEventTest, eventName : String) {
  158. const isPass = e.type === eventName && e.target instanceof UniElement && e.currentTarget instanceof UniElement;
  159. const result = isPass ? `${eventName}:Success` : `${eventName}:Fail`;
  160. switch (eventName) {
  161. case 'scroll':
  162. this.isScrollTest = result
  163. break;
  164. case 'scrolltolower':
  165. this.isScrolltolowerTest = result + `-${e.direction}`
  166. break;
  167. case 'scrolltoupper':
  168. this.isScrolltoupperTest = result + `-${e.direction}`
  169. break;
  170. default:
  171. break;
  172. }
  173. },
  174. //自动化测试例专用
  175. check_scroll_height() : Boolean {
  176. var listElement = this.$refs["listview"] as UniElement
  177. console.log("check_scroll_height--" + listElement.scrollHeight)
  178. if (listElement.scrollHeight > 2000) {
  179. return true
  180. }
  181. return false
  182. },
  183. //自动化测试例专用
  184. check_scroll_width() : Boolean {
  185. var listElement = this.$refs["listview"] as UniElement
  186. console.log("check_scroll_width" + listElement.scrollWidth)
  187. if (listElement.scrollWidth > 2000) {
  188. return true
  189. }
  190. return false
  191. },
  192. change_refresher_style_boolean(checked : boolean) {
  193. if (checked) {
  194. this.refresher_default_style_input = "none"
  195. } else {
  196. this.refresher_default_style_input = "black"
  197. }
  198. }
  199. }
  200. }
  201. </script>
  202. <template>
  203. <view class="main">
  204. <list-view :direction="scroll_direction" :bounces="bounces_boolean" :upper-threshold="upper_threshold_input"
  205. :lower-threshold="lower_threshold_input" :scroll-top="scroll_top_input" :scroll-left="scroll_left_input"
  206. :show-scrollbar="show_scrollbar_boolean" :scroll-into-view="scrollIntoView"
  207. :scroll-with-animation="scroll_with_animation_boolean" :refresher-enabled="refresher_enabled_boolean"
  208. :refresher-background="refresher_background_input" :refresher-triggered="refresher_triggered_boolean"
  209. :refresher-default-style="refresher_default_style_input" @click="list_view_click"
  210. @touchstart="list_view_touchstart" @touchmove="list_view_touchmove" @touchcancel="list_view_touchcancel"
  211. @touchend="list_view_touchend" @tap="list_view_tap" @longpress="list_view_longpress"
  212. @refresherpulling="list_view_refresherpulling" @refresherrefresh="list_view_refresherrefresh"
  213. @refresherrestore="list_view_refresherrestore" @refresherabort="list_view_refresherabort"
  214. @scrolltoupper="list_view_scrolltoupper" ref="listview" @scrolltolower="list_view_scrolltolower"
  215. @scroll="list_view_scroll" @scrollend="list_view_scrollend" style="width:100%;" id="listview">
  216. <list-item v-for="key in scrollData" :key="key" :id="key" @click="list_item_click"
  217. @touchstart="list_item_touchstart" @touchmove="list_item_touchmove" @touchcancel="list_item_touchcancel"
  218. @touchend="list_item_touchend" @tap="list_item_tap" @longpress="list_item_longpress" class="list-item">
  219. <text>{{key}}</text>
  220. </list-item>
  221. <!-- #ifdef APP -->
  222. <list-item slot="refresher" class="refresh-box">
  223. <text class="tip-text">{{text[state]}}</text>
  224. </list-item>
  225. <!-- #endif -->
  226. </list-view>
  227. </view>
  228. <scroll-view style="flex:1" direction="vertical">
  229. <view class="content">
  230. <!-- #ifdef APP -->
  231. <boolean-data :defaultValue="false" title="设置当前下拉刷新状态,true 表示下拉刷新已经被触发,false 表示下拉刷新未被触发"
  232. @change="change_refresher_triggered_boolean"></boolean-data>
  233. <boolean-data :defaultValue="false" title="开启下拉刷新" @change="change_refresher_enabled_boolean"></boolean-data>
  234. <boolean-data :defaultValue="false" title="开启自定义样式" @change="change_refresher_style_boolean"></boolean-data>
  235. <!-- #endif -->
  236. <boolean-data :defaultValue="false" title="是否在设置滚动条位置时使用滚动动画,设置false没有滚动动画"
  237. @change="change_scroll_with_animation_boolean"></boolean-data>
  238. <boolean-data :defaultValue="false" title="控制是否出现滚动条" @change="change_show_scrollbar_boolean"></boolean-data>
  239. <!-- #ifdef APP -->
  240. <boolean-data :defaultValue="true" title="控制是否回弹效果" @change="change_bounces_boolean"></boolean-data>
  241. <!-- #endif -->
  242. <boolean-data :defaultValue="true" title="允许纵向滚动" @change="change_scroll_y_boolean"></boolean-data>
  243. <!-- #ifdef APP -->
  244. <boolean-data :defaultValue="false" title="允许横向滚动" @change="change_scroll_x_boolean"></boolean-data>
  245. <!-- #endif -->
  246. <input-data defaultValue="50" title="距顶部/左边多远时(单位px),触发 scrolltoupper 事件" type="number"
  247. @confirm="confirm_upper_threshold_input"></input-data>
  248. <input-data defaultValue="50" title="距底部/右边多远时(单位px),触发 scrolltolower 事件" type="number"
  249. @confirm="confirm_lower_threshold_input"></input-data>
  250. <input-data defaultValue="0" title="设置竖向滚动条位置" type="number" @confirm="confirm_scroll_top_input"></input-data>
  251. <!-- #ifdef APP -->
  252. <input-data defaultValue="0" title="设置横向滚动条位置" type="number" @confirm="confirm_scroll_left_input"></input-data>
  253. <input-data defaultValue="#FFF" title="设置下拉刷新区域背景颜色" type="text"
  254. @confirm="confirm_refresher_background_input"></input-data>
  255. <enum-data :items="size_enum" title="通过id位置跳转" @change="item_change_size_enum"></enum-data>
  256. <navigator url="/pages/component/list-view/list-view-refresh">
  257. <button type="primary" class="button">
  258. list-view 下拉刷新
  259. </button>
  260. </navigator>
  261. <!-- #endif -->
  262. <navigator url="/pages/component/list-view/list-view-multiplex">
  263. <button type="primary" class="button">
  264. list-view 对list-item复用测试
  265. </button>
  266. </navigator>
  267. <navigator url="/pages/component/list-view/list-view-multiplex-input">
  268. <button type="primary" class="button">
  269. list-view 复用测试(item中嵌入input)
  270. </button>
  271. </navigator>
  272. <navigator url="/pages/component/list-view/list-view-multiplex-video">
  273. <button type="primary" class="button">
  274. list-view 复用测试(item中嵌入video)
  275. </button>
  276. </navigator>
  277. <navigator url="/pages/component/list-view/list-view-children-in-slot">
  278. <button type="primary" class="button">
  279. list-view 测试插槽中子组件增删
  280. </button>
  281. </navigator>
  282. <navigator url="/pages/component/list-view/list-view-children-if-show">
  283. <button type="primary" class="button">
  284. list-item v-if v-show 组合增删
  285. </button>
  286. </navigator>
  287. <navigator url="/pages/component/list-view/list-view-long-press">
  288. <button type="primary" class="button">
  289. list-item 中设置长按事件测试
  290. </button>
  291. </navigator>
  292. </view>
  293. </scroll-view>
  294. </template>
  295. <style>
  296. .main {
  297. max-height: 250px;
  298. padding: 5px 0;
  299. border-bottom: 1px solid rgba(0, 0, 0, .06);
  300. flex-direction: row;
  301. justify-content: center;
  302. }
  303. .main .list-item {
  304. width: 100%;
  305. height: 250px;
  306. border: 1px solid #666;
  307. background-color: #66ccff;
  308. align-items: center;
  309. justify-content: center;
  310. }
  311. .tip-text {
  312. color: #888;
  313. font-size: 12px;
  314. }
  315. .refresh-box {
  316. justify-content: center;
  317. align-items: center;
  318. flex-direction: row;
  319. height: 45px;
  320. width: 100%;
  321. }
  322. .button {
  323. margin-top: 15px;
  324. }
  325. </style>