long-list.uvue 2.8 KB
Newer Older
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
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
<template>
	<view style="width: 100%;height: 100%;">
		<list class="uni-list" refresher-enabled=true @refresherrefresh="onRefresherrefresh"
			:refresher-triggered="refresherTriggered">
			<cell v-for="(value, index) in listData" :key="index">
				<view class="uni-list-cell" hover-class="uni-list-cell-hover" @click="goDetail(value)">
					<view class="uni-media-list">
						<image class="uni-media-list-logo" :src="value.img"></image>
						<view class="uni-media-list-body">
							<text class="uni-media-list-text-top">{{ value.title }}</text>
							<view class="uni-media-list-text-bottom">
								<text class="uni-media-list-text">{{ value.subTitle }}</text>
							</view>
						</view>
					</view>
				</view>
			</cell>
		</list>
	</view>
</template>

<script>
import JSONObject from 'com.alibaba.fastjson.JSONObject';
	type Item = {
		title: string
		subTitle: string,
		img: string,
		detail: string,
	}


	export default {
		data() {
			return {
				refresherTriggered: false,
				listData: [] as Item[],
				last_id: '',
				pageVisible: false
			};
		},
		onLoad() {
			this.pageVisible = true;
			this.getList();
		},
		onUnload() {
			this.pageVisible = false;
		},
		methods: {
			getList() {
				const content = UTSAndroid.getFileContent("static/list-mock/mock.json");
				if (content != null){
					const items = [] as Item[];
					const jsonArr = JSON.parse(content);
					jsonArr?.forEach((res)=>{
						const json = res as JSONObject;
						const title = json["title"] as string;
						const subTitle = json["subTitle"] as string;
						const img = json["img"] as string;
						const detail = json["detail"] as string;
						const item:Item = {
							title,
							subTitle,
							img,
							detail
						}
						items.push(item);
					})
					let temp = [] as Item[];
					for(let i = 0; i < 100; i++){
						temp = temp.concat(items);
					}
					listData = temp;
				}
				
				setTimeout(()=>{
					this.refresherTriggered = false
				},0);
			},
			goDetail(e: Item) {
				uni.navigateTo({
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
81
					url: 'pages/component/long-list/detail/detail?content=' + e.detail
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
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
				});
			},
			onRefresherrefresh() {
				if(this.pageVisible){
					this.refresherTriggered = true
					this.getList();
				}
			}
		}
	};
</script>

<style>
	.uni-media-list {
		padding: 22rpx 30rpx;
		box-sizing: border-box;
		display: flex;
		width: 100%;
		flex-direction: row;
	}

	.uni-media-list-logo {
		width: 180rpx;
		height: 140rpx;
	}

	.uni-media-list-body {
		flex: 1;
		padding-left: 15rpx;
	}

	.uni-media-list-text-top {
		font-size: 28rpx;
		lines:2;
		overflow: hidden;
	}

	.uni-media-list-text-bottom {
		display: flex;
		margin-top: 10rpx;
	}
	
	.uni-media-list-text {
		color: #9D9D9F;
		font-size: 25rpx;
	}
</style>