微信小程序列表页

article/2025/11/6 12:24:18

我们在做Android开发时,几乎每个app都有几个列表,在Android中列表一般是用listview,后来就使用recyclerview做了,不管是小程序还是Android或者ios,列表都是常见的一种数据展示方式,那么在小程序中怎么实现呢?先使用最笨的方法做,

如图:

针对上面的列表分析:它整体是一个垂直布局,第一部分 头像+日期是一个横向布局,下面是一张图片,再下面是一段文字描述,再下面也是一个横向布局 点赞+回复的图片和文字个数,那么现在就一步步来实现这个效果,先简单的布局:

<view><view><view><image src='/images/user_header.png'></image><text>Nov 2018-11-14</text></view><image src='/images/cat.png'></image><text>菊黄蟹正肥,品尝秋之味。徐志摩把,“看初花的荻芦”和“到楼外楼吃蟹”,并列为秋天来杭州不能错过的风雅之事;用林妹妹的话讲是“螯封嫩玉双双满</text></view>
</view>

预览:

发现和我们想要的效果实在差的太远,这是因为没有使用到样式 给它穿上漂亮的衣服

现在先给最外层以及头像和日期整块 和头像部分添加样式控制

<view><view class='posts-container'><view class='posts-author_date'><image src='/images/user_header.png' class='user_authr'></image><text>Nov 2018-11-14</text></view><image src='/images/cat.png'></image><text>菊黄蟹正肥,品尝秋之味。徐志摩把,“看初花的荻芦”和“到楼外楼吃蟹”,并列为秋天来杭州不能错过的风雅之事;用林妹妹的话讲是“螯封嫩玉双双满</text></view>
</view>

样式控制:


.posts-container{display: flex;flex-direction: column;margin-bottom: 40rpx;background-color: #fff;padding-bottom: 5px;border-bottom: 1px solid #ededed;border-top: 1px solid #ededed
}.posts-author_date{display: flex;flex-direction: row;
}
.user_authr{width: 60rpx;height: 60rpx;
}

其中最重要的还是display:flex 这是注明是弹性布局,flex-direction:表明放向 row是横向 column是竖向,这就好比是我们Android中的LinearLayout一样

现在的效果:

是不是比之前有进步,再进一步调样式

<view><view class='posts-container'><view class='posts-author_date'><image src='/images/user_header.png' class='user_authr'></image><text class='date'>Nov 2018-11-14</text></view><image src='/images/cat.png' class='posts_detail_img'></image><text class='posts_title'>菊黄蟹正肥,品尝秋之味。徐志摩把,“看初花的荻芦”和“到楼外楼吃蟹”,并列为秋天来杭州不能错过的风雅之事;用林妹妹的话讲是“螯封嫩玉双双满</text><view class='posts-bottom-like'><image src='/images/chat.png' class='posts-like'></image><text class='like-text'>96</text><image src='/images/view.png' class='reply-like'></image><text class='reply-text'>32</text></view></view>
</view>

样式:


.posts-container{flex-direction:column;display:flex;margin-top:20rpx;margin-bottom:40rpx;margin-left: 0rpx;background-color:#fff;border-bottom: 1px solid #ededed;border-top: 1px solid #ededed;padding-bottom: 5px;
}.posts-author_date{display: flex;flex-direction: row;margin-top:10rpx;margin-bottom: 20rpx;margin-left: 10px;
}
.date{vertical-align: middle;font-size: 26rpx;margin:  auto 0px;margin-left: 10px;}
.user_authr{width: 60rpx;height: 60rpx;vertical-align: middle
}
.posts_detail_img{width: 100%;height: 500rpx;
}
.posts_title{font-size: 28rpx;margin-left: 10px;color: #333;margin-top: 10px;margin-bottom: 10px;margin-right: 10px;
}
.posts-bottom-like{display: flex;flex-direction: row;
}
.posts-like{width: 24px;height: 24px;margin:  auto 0px;margin-left: 10px;
}
.reply-like{width: 20px;height: 20px;vertical-align: middle;margin:  auto 0px;margin-left: 10px;
}
.like-text{color: #333;font-size: 30rpx;vertical-align: middle;margin:  auto 0px;margin-left: 10px;
}
.reply-text{color: #333;font-size: 30rpx;vertical-align: middle;margin:  auto 0px;margin-left: 10px;
}

 在这说下当时想要text组件的内容垂直居中,好久没搞定,加上css属性不熟,就一直试试,vertical-align: middle;这个属性是垂直居中,后来发现不行,解决方案是:

margin: auto 0px; 如果你左边还有组件,想离左边组件10px,后面就加上margin-left: 10px;

现在整体效果:

虽然谈不上多好,但是整体效果已经出来了 这是列表中单独一个子item,那么想多个呢?我现在使用最笨的方法,就是复制上面的代码多添加几行就可以了,就相当于我们不使用recyclerview展示数据,就使用LinearLayout,把它的子布局复制就行

wxml:

<view><view class='posts-container'><view class='posts-author_date'><image src='/images/user_header.png' class='user_authr'></image><text class='date'>Nov 2018-11-14</text></view><image src='/images/cat.png' class='posts_detail_img'></image><text class='posts_title'>菊黄蟹正肥,品尝秋之味。徐志摩把,“看初花的荻芦”和“到楼外楼吃蟹”,并列为秋天来杭州不能错过的风雅之事;用林妹妹的话讲是“螯封嫩玉双双满</text><view class='posts-bottom-like'><image src='/images/chat.png' class='posts-like'></image><text class='like-text'>96</text><image src='/images/view.png' class='reply-like'></image><text class='reply-text'>32</text></view></view><view class='posts-container'><view class='posts-author_date'><image src='/images/user_header.png' class='user_authr'></image><text class='date'>Nov 2018-11-14</text></view><image src='/images/cat.png' class='posts_detail_img'></image><text class='posts_title'>菊黄蟹正肥,品尝秋之味。徐志摩把,“看初花的荻芦”和“到楼外楼吃蟹”,并列为秋天来杭州不能错过的风雅之事;用林妹妹的话讲是“螯封嫩玉双双满</text><view class='posts-bottom-like'><image src='/images/chat.png' class='posts-like'></image><text class='like-text'>96</text><image src='/images/view.png' class='reply-like'></image><text class='reply-text'>32</text></view></view><view class='posts-container'><view class='posts-author_date'><image src='/images/user_header.png' class='user_authr'></image><text class='date'>Nov 2018-11-14</text></view><image src='/images/cat.png' class='posts_detail_img'></image><text class='posts_title'>菊黄蟹正肥,品尝秋之味。徐志摩把,“看初花的荻芦”和“到楼外楼吃蟹”,并列为秋天来杭州不能错过的风雅之事;用林妹妹的话讲是“螯封嫩玉双双满</text><view class='posts-bottom-like'><image src='/images/chat.png' class='posts-like'></image><text class='like-text'>96</text><image src='/images/view.png' class='reply-like'></image><text class='reply-text'>32</text></view></view>
</view>

效果如下:

右边有个滚动条很难看,这就好比Android的listview有个默认的滚动条,那么去掉这个滚动条呢?查文档,可惜文档没有这个属性,算了留在这里以后再弄

 

除非列表数据都是静态的才可以这么弄,就算是静态的数据也不会这么弄,太笨了,肯定还有更好的方法,先把变成动态的,之前发布的博客写了关于数据绑定,现在就能用的上呢?我们在每个Page下有个data json里面可以写入json数据,然后wxml数据就动态的绑定到data中的json,比如:<text class='date'>{{date}}</text>

data: {date:"2018-11-13",condition :false},

组件text中的数据就是来自data  json下的date数据

修改wxml文件改成数据动态绑定:

<view><view class='posts-container'><view class='posts-author_date'><image src='{{img}}' class='user_authr'></image><text class='date'>{{date}}</text></view><image src='{{content_img}}' class='posts_detail_img'></image><text class='posts_title'>{{content}}</text><view class='posts-bottom-like'><image src='{{chat_icon}}' class='posts-like'></image><text class='like-text'>{{collect}}</text><image src='{{reply_icon}}' class='reply-like'></image><text class='reply-text'>{{replyCount}}</text></view></view><view class='posts-container'><view class='posts-author_date'><image src='{{img}}' class='user_authr'></image><text class='date'>{{date}}</text></view><image src='{{content_img}}' class='posts_detail_img'></image><text class='posts_title'>{{content}}</text><view class='posts-bottom-like'><image src='{{chat_icon}}' class='posts-like'></image><text class='like-text'>{{collect}}</text><image src='{{reply_icon}}' class='reply-like'></image><text class='reply-text'>{{replyCount}}</text></view></view><view class='posts-container'><view class='posts-author_date'><image src='{{img}}' class='user_authr'></image><text class='date'>{{date}}</text></view><image src='{{content_img}}' class='posts_detail_img'></image><text class='posts_title'>{{content}}</text><view class='posts-bottom-like'><image src='{{chat_icon}}' class='posts-like'></image><text class='like-text'>{{collect}}</text><image src='{{reply_icon}}' class='reply-like'></image><text class='reply-text'>{{replyCount}}</text></view></view>
</view>

数据来源:

/*** 页面的初始数据*/data: {date:"2018-11-13",condition :false,content:"菊黄蟹正肥,品尝秋之味。徐志摩把,“看初花的荻芦”和“到楼外楼吃蟹”,并列为秋天来杭州不能错过的风雅之事;用林妹妹的话讲是“螯封嫩玉双双满",collect :90,replyCount:40,img:"/images/user_header.png",reply_icon:"/images/view.png",chat_icon:"/images/chat.png",content_img:"/images/cat.png"},

 其实这个data--json中的数据我们看起来也是静态写死的,那么我就在Page生命周期函数onLoad中去模拟网络数据请求后再绑定数据:

现在把data json对象为空,把里面的数据写在onLoad()方法中:

 onLoad: function (options) {var dataList={date: "2018-11-13",condition: false,content: "菊黄蟹正肥,品尝秋之味。徐志摩把,“看初花的荻芦”和“到楼外楼吃蟹”,并列为秋天来杭州不能错过的风雅之事;用林妹妹的话讲是“螯封嫩玉双双满",collect: 90,replyCount: 40,img: "/images/user_header.png",reply_icon: "/images/view.png",chat_icon: "/images/chat.png",content_img: "/images/cat.png"}this.setData(dataList);},

this.setData(dataList)就相当于把变量dataList对应的json写到data:{} 里面

现在问题又来了,我们是一个布局,上面的每个item中的每个子控件显示的数据都是一样的,这肯定是不对的 现在做下组件复用,就好比我们android使用listview来展示数据,

记得在你子组件中添加block父组件:

<view>
<block><view class='posts-container'><view class='posts-author_date'><image src='{{img}}' class='user_authr'></image><text class='date'>{{date}}</text></view><image src='{{content_img}}' class='posts_detail_img'></image><text class='posts_title'>{{content}}</text><view class='posts-bottom-like'><image src='{{chat_icon}}' class='posts-like'></image><text class='like-text'>{{collect}}</text><image src='{{reply_icon}}' class='reply-like'></image><text class='reply-text'>{{replyCount}}</text></view></view>
</block>
</view>

可以把block理解成for循环中的{} 号

<view>
<block wx:for="{{posts_key}}" wx:for-item="item"><view class='posts-container'><view class='posts-author_date'><image src='{{item.img}}' class='user_authr'></image><text class='date'>{{item.date}}</text></view><image src='{{item.content_img}}' class='posts_detail_img'></image><text class='posts_title'>{{item.content}}</text><view class='posts-bottom-like'><image src='{{item.chat_icon}}' class='posts-like'></image><text class='like-text'>{{item.collect}}</text><image src='{{item.reply_icon}}' class='reply-like'></image><text class='reply-text'>{{vitem.replyCount}}</text></view></view>
</block>
</view>

js中:

 /*** 生命周期函数--监听页面加载*/onLoad: function (options) {var dataList=[{date: "2018-11-13",condition: false,content: "菊黄蟹正肥,品尝秋之味。徐志摩把,“看初花的荻芦”和“到楼外楼吃蟹”,并列为秋天来杭州不能错过的风雅之事;用林妹妹的话讲是“螯封嫩玉双双满",collect: 90,replyCount: 40,img: "/images/user_header.png",reply_icon: "/images/view.png",chat_icon: "/images/chat.png",content_img: "/images/cat.png"},{date: "2018-11-14",condition: false,content: "菊黄蟹正肥,品尝秋之味。徐志摩把,“看初花的荻芦”和“到楼外楼吃蟹”,并列为秋天来杭州不能错过的风雅之事;用林妹妹的话讲是“螯封嫩玉双双满",collect: 91,replyCount: 41,img: "/images/user_header.png",reply_icon: "/images/view.png",chat_icon: "/images/chat.png",content_img: "/images/cat.png"},{date: "2018-11-15",condition: false,content: "菊黄蟹正肥,品尝秋之味。徐志摩把,“看初花的荻芦”和“到楼外楼吃蟹”,并列为秋天来杭州不能错过的风雅之事;用林妹妹的话讲是“螯封嫩玉双双满",collect: 92,replyCount: 42,img: "/images/user_header.png",reply_icon: "/images/view.png",chat_icon: "/images/chat.png",content_img: "/images/cat.png"},{date: "2018-11-16",condition: false,content: "菊黄蟹正肥,品尝秋之味。徐志摩把,“看初花的荻芦”和“到楼外楼吃蟹”,并列为秋天来杭州不能错过的风雅之事;用林妹妹的话讲是“螯封嫩玉双双满",collect: 93,replyCount: 43,img: "/images/user_header.png",reply_icon: "/images/view.png",chat_icon: "/images/chat.png",content_img: "/images/cat.png"},{date: "2018-11-17",condition: false,content: "菊黄蟹正肥,品尝秋之味。徐志摩把,“看初花的荻芦”和“到楼外楼吃蟹”,并列为秋天来杭州不能错过的风雅之事;用林妹妹的话讲是“螯封嫩玉双双满",collect: 94,replyCount: 44,img: "/images/user_header.png",reply_icon: "/images/view.png",chat_icon: "/images/chat.png",content_img: "/images/cat.png"},{date: "2018-11-18",condition: false,content: "菊黄蟹正肥,品尝秋之味。徐志摩把,“看初花的荻芦”和“到楼外楼吃蟹”,并列为秋天来杭州不能错过的风雅之事;用林妹妹的话讲是“螯封嫩玉双双满",collect: 95,replyCount: 45,img: "/images/user_header.png",reply_icon: "/images/view.png",chat_icon: "/images/chat.png",content_img: "/images/cat.png"},{date: "2018-11-19",condition: false,content: "菊黄蟹正肥,品尝秋之味。徐志摩把,“看初花的荻芦”和“到楼外楼吃蟹”,并列为秋天来杭州不能错过的风雅之事;用林妹妹的话讲是“螯封嫩玉双双满",collect: 96,replyCount: 46,img: "/images/user_header.png",reply_icon: "/images/view.png",chat_icon: "/images/chat.png",content_img: "/images/cat.png"},]this.setData({ posts_key: dataList});},

 这是在onLoad()生命周期方法中定义了一个变量,如果定义的变量时对象的话,可以使用this.setData(变量名);如果是数组或者集合的话就不能这么弄了,要这么写:

 this.setData({ posts_key: dataList});

这个就相当于在data:{posts_key:dataList}  还有就是在block  wx:for={{传递的是key}},还有一点,这里:

<block wx:for="{{posts_key}}" wx:for-item="item">

wx:for-item="item"其实可以看做是java中List<Person> 集合中遍历Person person =  list.get(i);这个person变量相当于item,当然也可以不指定 它默认就是item,当然这个wx:for-item=""这字符串中的值你可以随便定义都行,比如test  person都是OK的,如果你想拿到集合中的下标值,可以这么做在block中添加wx:for-index = "idx" 这个idx就相当于下标值了。


http://chatgpt.dhexx.cn/article/rSjn1ikY.shtml

相关文章

小程序列表展示

小程序列表展示 wxml代码 <view class"content"><form catchsubmit"formSuos" catchreset"formReset"><view class"group"><view class"sous"><input class"text1_3" auto-focus pl…

微信小程序信息展示列表

微信小程序信息展示列表 效果展示: 代码展示: wxml <view class"head"><view class"head_item">分类</view><view class"ring"></view><view class"head_item">价格</view> </view>…

微信小程序-列表展示、条目点击。

直接贴图&#xff1a; wx:for"{{datas}}"设置数据源&#xff1b; wx:for-item"item"设置条目名称&#xff1b; data-index{{index}}设置角标。 bindtapitemClick点击事件。 {{item.item}}显示数据 datas数据源。 条目点击 itemClick: function(e) { …

【微信小程序】-- 案例 - 本地生活(列表页面)(三十)

&#x1f48c; 所属专栏&#xff1a;【微信小程序开发教程】 &#x1f600; 作  者&#xff1a;我是夜阑的狗&#x1f436; &#x1f680; 个人简介&#xff1a;一个正在努力学技术的CV工程师&#xff0c;专注基础和实战分享 &#xff0c;欢迎咨询&#xff01; &…

微信小程序删除list指定列表项

一、删除效果展示&#xff1a; // 重要代码片段async deleteListItem(e) {const sureResult await wx.showModal({title: "提示",content: "确定要删除这项吗",});if (sureResult.confirm) {const { index } e.currentTarget.dataset;setTimeout(()>{…

【小程序从0到1】小程序常用组件一览

欢迎来到我的博客 &#x1f4d4;博主是一名大学在读本科生&#xff0c;主要学习方向是前端。 &#x1f36d;目前已经更新了【Vue】、【React–从基础到实战】、【TypeScript】等等系列专栏 &#x1f6e0;目前正在学习的是&#x1f525; R e a c t / 小程序 React/小程序 React/…

微信小程序商城系列之商品列表页(一)

微信小程序商城列表页 wxml: <view classlist><block wx:for{{dataList}} wx:keylist wx:for-item"item"><view class"list_item"><navigator urldetails?id{{item.goods_id}}><view classimg><image src"{{imghr…

微信小程序——商品列表

主页面 <view class"container"><googsList goodsClassList"{{goodsClassList}}" goodsList"{{goodsList}}" bind:leftClick"leftClick" bind:toDetails"toDetails" bind:scrolltolowerGoodsClass"scroll…

微信小程序 - 商城项目 - 列表排序

sort ( ) 排序 需要实现两个排序功能&#xff0c;价格排序和好评排序&#xff0c;且都可以正反切换 数字比较 价格排序中&#xff0c;价格为数字类型&#xff0c;属于数字的比较&#xff0c;无需转换类型 // 点击价格排序的图标handlePrice(){this.setData({priceOrder:!this…

微信小程序之多列表的显示和隐藏功能【附源码】

效果图&#xff1a; 实现思路&#xff1a; 实现单个列表的显示和隐藏应该使用唯一元素让程序知道你应该显示和隐藏哪个列表项&#xff0c;可以用数据的id&#xff1b;css中定义一个hidden{display&#xff1a;none}控制显示和隐藏&#xff0c;然后通过三元运算符来判断&#x…

微信小程序列表实现

1.在 .json文件中使用 navigationBarTitleText定义当前窗口title {"navigationBarTitleText": "小程序列表实现" }2.在 .wxss文件中编写如下&#xff1a; // page设置当前窗口背景色 page{background-color: #F5F5F5 }// 定义.container设置容器样式 .co…

微信小程序 开发列表

一.知识点 (一).列表渲染 wx:for tip:wx:for“array”可以等于参数名&#xff0c;在js中调用 Page({ data:{ array: [{name: 小李},{ name: 小高}]} })&#xff0c;获取值&#xff1b;也可以直接把wx:for"{{[1, 2, 3]}}"&#xff0c;把值放在上面 1.在组件上使用wx:f…

微信小程序实现分类列表

1、效果展示 2、fenleiliebiao.wxml文件代码 <!-- 头部搜索 --> <view class"title_search"></view> <!-- 头部搜索 --> <!-- Tab布局 --> <view catchtouchmovetrue><view classnavBox><view classtitleBox1 id"…

微信小程序--数据列表

设置页面标题 首页可以使用&#xff1a; app.json中window对象的"navigationBarTitleText"参数进行标题设置&#xff0c; 每个页面也可以在对应json文件中使用"navigationBarTitleText"参数进行标题设置 也可以在js文件的onRead函数中使用&#xff1a;js中…

用小程序完成简单的详情列表功能

最近学习了微信小程序&#xff0c;写了一个详情列表 主要思路是&#xff1a; 1.先通过接口拿到数据&#xff0c;在onLoad(options){}中调用wx.request(){}函数&#xff0c;获取到数据 2.完成数据渲染后&#xff0c;&#xff0c;用catchtab给它绑定一个点击事件&#xff0c;点…

微信小程序--列表展示

小知识&#xff1a; wx:for"{{list}}"用来循环数组。 wx:for-item‘变量名&#xff08;随便起&#xff09;’ 它是指定循环数据当前的变量名&#xff0c;可以通过 {{变量名.属性}} 展示数组的元素。 wx:for-index‘变量名&#xff08;随便起&#xff09;’&#x…

vmware 下 ubuntu 双网卡设置

写在前面&#xff1a; 本文章旨在总结备份、方便以后查询&#xff0c;由于是个人总结&#xff0c;如有不对&#xff0c;欢迎指正&#xff1b;另外&#xff0c;内容大部分来自网络、书籍、和各类手册&#xff0c;如若侵权请告知&#xff0c;马上删帖致歉。 目录 step 1&#xf…

Ubuntu18.04 配置网卡

2019/10/29, Ubuntu Server 18.04 摘要&#xff1a;Ubuntu Server 18.04 采用netplan作为网络配置管理&#xff0c;修改IP使其连上网络&#xff0c;修改替换软件源 修改网卡配置# 首先使用ip a查看当前网卡名称&#xff1a; 可以看到eth0就是我们的网卡名称&#xff0c;这个要…

linux Ubuntu网卡配置,Windows 7下用VirtualBox安装Ubuntu网卡配置

Windows 7下用VirtualBox安装Ubuntu&#xff0c;要求Ubuntu能连上互联网&#xff0c;同时在Ubuntu中配置SSH、FTP、NFS服务&#xff0c;能从Win7进行访问。 此时Linux虚拟机需要两块网卡&#xff0c;一块使用NAT方式(网络地址翻译网络)连接&#xff0c;用于连接互联网&#xff…

Ubuntu 双网卡配置优先级

Ubuntu 双网卡配置优先级 作者&#xff1a;Bright Xu Ubuntu的网卡配置跟CentOS不太一样。 更多请参考&#xff1a;CentOS 双网卡配置优先级 根据业务需要&#xff0c;有时候服务器需要两张网卡&#xff0c;一张用于访问外网&#xff0c;另一种用于连接内网。 如果在安装系统是…