list-view-long-press.uvue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <template>
  2. <view style="flex:1">
  3. <scroll-view :show-scrollbar="false" :scroll-with-animation="true" style="flex: 1;padding-bottom: 10px;">
  4. <list-view ref="messageView" @scrolltoupper="clearData" @scrolltolower="loadData" :scroll-with-animation="true"
  5. :show-scrollbar="false" style="flex: 1;transform: rotate(180deg)">
  6. <list-item v-for="(item, index) in messageList" @longpress="listItemLongPress(index)" :key="`listItem${index}`"
  7. :id="`listItem${item.id}`" style="transform: rotate(180deg);padding: 10px 12px;" type="0">
  8. <view class="left-row">
  9. <image :fade-show="true" :src="item.avatar"
  10. :style="{width: `${avatarWidth}px`,height: `${avatarWidth}px`,borderRadius: `${avatarWidth}px`}"></image>
  11. <view :id="`listItemContent${item.id}`" class="flex-row content"
  12. style="padding: 9px 11px 9px 11px;border-radius: 7px;align-items: center;margin-left: 8px;background-color: #3c99ff;">
  13. <text style="font-size: 13px;line-height: 20px;color:#fff;">
  14. {{ item.content }}
  15. </text>
  16. </view>
  17. <view style="flex: 1;">
  18. </view>
  19. </view>
  20. </list-item>
  21. <list-item slot="load-more" type="3" class="loading">
  22. <uni-loading :loading="loading" color="#999"></uni-loading>
  23. </list-item>
  24. </list-view>
  25. </scroll-view>
  26. </view>
  27. </template>
  28. <script setup lang="uts">
  29. type MessageItem = {
  30. id : number;
  31. avatar : string;
  32. left : boolean;
  33. content : string;
  34. height : number;
  35. }
  36. const messageView = ref<UniElement | null>(null)
  37. const bottom = ref<UniElement | null>(null)
  38. const avatarWidth = ref(38)
  39. const scrollTop = ref(0)
  40. const message = ref('')
  41. const showBottom = ref(false)
  42. const isFocus = ref(false)
  43. const loading = ref(false)
  44. const audio = ref(false)
  45. const recording = ref(false)
  46. const messageList = ref<MessageItem[]>([])
  47. const screenWidth = computed(() : number => {
  48. return 1080
  49. })
  50. const clearData = () => {
  51. if (messageList.value.length >= 40) {
  52. messageList.value.splice(39, messageList.value.length - 1)
  53. }
  54. }
  55. const listItemLongPress = (index : number) => {
  56. uni.showToast({
  57. title: '触发长按:' + index,
  58. position: "bottom"
  59. })
  60. }
  61. const loadData = () => {
  62. loading.value = true
  63. setTimeout(() => {
  64. loading.value = false
  65. let length = messageList.value.length
  66. for (let i = 0; i < 20; i++) {
  67. let item = {
  68. id: length + i,
  69. avatar: "/static/uni.png",
  70. left: true,
  71. content: '这是一些占位消息',
  72. height: 0
  73. } as MessageItem
  74. messageList.value.push(item)
  75. }
  76. }, 800)
  77. }
  78. const getMessage = () => {
  79. let idList : number[] = []
  80. for (let index = 0; index < 20; index++) {
  81. let item = {
  82. id: index,
  83. avatar: '/static/uni.png',
  84. left: false,
  85. content: `这是一条消息${index}`,
  86. height: 0
  87. } as MessageItem
  88. messageList.value.push(item)
  89. idList.push(item.id)
  90. }
  91. }
  92. onReady(() => {
  93. getMessage()
  94. })
  95. </script>
  96. <style scoped>
  97. .flex-row {
  98. display: flex;
  99. flex-direction: row;
  100. }
  101. .left-row {
  102. flex-direction: row-reverse;
  103. }
  104. .content {
  105. margin: 0 8px;
  106. }
  107. .loading {
  108. height: 60px;
  109. justify-content: center;
  110. align-items: center;
  111. }
  112. </style>