微信小程序列表页

我们在做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://xiahunao.cn/news/143542.html

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

微信小程序信息展示列表

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

微信小程序列表开发-个人中心界面(一)

先放个效果图让你们看一下是不是你们需要的&#xff0c;到时候会写几篇博客把里面的组件也讲一下 .wxml .wxss .js .json 我里面的引用的照片都放在image文件里的&#xff0c;image文件是放在pages文件下面的&#xff0c;因为放的位置不同&#xff0c;图片引用的路径…

JDK8-JDK17中的新特性(var类型推断、模式匹配、Record、密封类)

文章目录 1. 新语法结构1.1 Java的REPL工具&#xff1a; jShell命令1.2 异常处理之try-catch资源关闭1.3 局部变量类型推断1.4 instanceof的模式匹配1.5 switch表达式1.6 文本块1.7 Record1.8 密封类 2. API的变化2.1 Optional类2.2 String存储结构和API变更2.3 JDK17&#xff…

linux下安装使用dig命令

有时候用的精简版linux系统会发现没有dig命令,这时候就需要安装一下。 centos系 yum install bind-utils dig命令大多时候可以取代nslookup 简明使用,只会输出A记录(写脚本的时候容易获取ip地址) dig www.baidu.com +short只输出mx记录,简明使用 dig mx www.baidu.com…

dig命令后+trace的含义

dig trace命令 dig ip 域名 类型 trace工作过程 以 dig pbc.gov.cn trace 为例。 用户看到的结果 wireshark抓包看到的结果&#xff08;只显示了查询部分&#xff0c;响应就是查询目的对源地址的响应&#xff09; 步骤源目的查询1本机ip中指定的ip.的NS2本机本地DNS.的NS…

linux dig命令使用详解

linux dig命令使用详解 Linux下解析域名除了使用nslookup之外&#xff0c;开可以使用dig命令来解析域名&#xff0c;dig命令可以得到更多的域名信息。dig 命令主要用来从 DNS 域名服务器查询主机地址信息。 dig的全称是 (domain information groper)。它是一个用来灵活探测DN…

dig命令笔记

dig 命令全称域信息搜索器&#xff0c;是一个用于查询 DNS 域名服务器信息的命令行工具。因为dig命令灵活&#xff0c;容易使用&#xff0c;多数DNS管理员使用dig命令来诊断 DNS 问题。 dig 常用命令格式 dig [server] [-p port] [-t type] [-4] [-6] [trace] name 指定 DNS …

linux中dig 命令解释

dig&#xff08;域信息搜索器&#xff09;命令是个用于询问 DNS 域名服务器的灵活的工具。他执行 DNS 搜索&#xff0c;显示从受请求的域名服务器返回的答复。多数 DNS 管理员利用 dig 作为 DNS 问题的故障诊断&#xff0c;因为他灵活性好、易用、输出清楚 1、A记录&#xff1…

从dig命令理解DNS

DNS&#xff08;Domain Name System&#xff0c;域名系统&#xff09;&#xff0c;是一种用于将域名解析为IP的服务器系统&#xff0c;当你上网时输入一个网址&#xff0c;它之所以能够找到该网址指向的服务器地址&#xff0c;都是靠域名系统来进行解析的。 先来讲讲域名。以华…

dig命令(dig命令怎么用)

林肯公园digdeeper是哪首歌 出自专辑《Minutes to Midnight》 nslookup、dig和host这几个命令有什么作用&#xff1f; nslookup、dig和host 这几个命令在UNIX和linux系统中使用,都可以进行域名的解析?nslookup使用交互方式查询域名与IP地址的映射关系?dig的功能是发送域名查…

Windows10 下安装 dig 命令的步骤(一)

前言&#xff1a; 今天在电脑上解析域名&#xff0c;但是发现dig命令报以下错误&#xff1a; Dig 工具全称为域名信息搜索器&#xff08;Domain InformationGroper&#xff09;&#xff0c;能够显示详细的DNS查询过程&#xff0c;是一个非常强大的DNS故障诊断工具。一般Linux…

linux中dig命令返回结果解释

dig baidu.com 返回 下面说明各项意义&#xff1a; ; <<>> DiG 9.3.6-P1-RedHat-9.3.6-20.P1.el5_8.6 <<>> baidu.com ;; global options: printcmd dig程序的版本号&#xff0c;和要查询的域名 Dig的部分输出告诉我们一些有关于它的版本信息(versio…

Windows系统下安装dig命令

dig 是一个 Linux 下用来 DNS 查询信息的工具&#xff0c;全称是Domain Information Groper&#xff0c;与 nslookup 类似&#xff0c;但比 nslookup 功能更强大。Windows 下只有 nslookup&#xff0c;如果也想用到 dig 命令&#xff0c;就只能自己动手安装了。 dig 作为 bind …

(学习日记)AD学习 #4

写在前面&#xff1a; 由于时间的不足与学习的碎片化&#xff0c;写博客变得有些奢侈。 但是对于记录学习&#xff08;忘了以后能快速复习&#xff09;的渴望一天天变得强烈。 既然如此 不如以天为单位&#xff0c;以时间为顺序&#xff0c;仅仅将博客当做一个知识学习的目录&a…

Excel表格匹配合并

在日常的工作中&#xff0c;免不了存在多个表格根据相同数据匹配合并的情况&#xff0c;很多人会因为复杂的公式导致匹配失败或错误。接下来&#xff0c;我将用一个简单的方式完成这一个任务。 1、打开网址www.excelutil.com 2、选择匹配合并 3、上传左文件和右文件 这两个文…

Excel模糊匹配相同内容的数据求和,使用SUMIF函数

看到朋友在对Excel表格数据进行机械操作&#xff0c;想到excel应该是个很强大的应用&#xff0c;这些机械操作应该可以通过函数简化操作的&#xff0c;于是不正经的研究下。 首先我们上图&#xff1a; 简单的需求&#xff0c;把每个店铺的每个月充值分别是200,500,1000,1500,2…

大模型时代的prompt学习(持续更新)

目录 为什么要学prompt基本原则prompt撰写框架Base Prompt FrameworkCRISPE Prompt Framework 场景撰写文案文档竞品分析产品设计数据分析 chain of thoughtzero shotin context learning(few shot)Self-Consistency Program-Aidedprompt tipsTo Do and Not To Doadd examples引…

excel数据分析 - 8个关联匹配类函数

1. LOOKUP函数 ①单条件定位查找 lookup( 待匹配内容, 待匹配内容所在区域 , 结果范围显示区域 ) 两个区域的列数需相同 e.g. 查找 “东区”对应的C1省会城市&#xff0c; lookup &#xff08;A2, A:A , C:C &#xff09; 杭州 A1B1C1东区 浙江杭州西区甘肃兰州 ②多条…

python excel 数据匹配_python将一个excel表格的数据匹配到另一个表中

python将一个excel表格的数据匹配到另一个表中 python将一个excel表格的数据匹配到另一个表中 打开excel表&#xff0c;需要在另一个表中匹配相应学生姓名的学号信息。 之前尝试了excel中的VLOOKUP函数&#xff0c;试了很多次都没有成功&#xff0c;因此&#xff0c;用python试…

EXCEL省份和年份的数据匹配

目录 EXCEL对两个满足两个条件的数据匹配公式 使用的函数 实例一 实例二 可能存在的问题 参考 本文用于记录处理数据的一些经验和方法 EXCEL对两个满足两个条件的数据匹配公式 使用的函数 INDEX(array, row_num, [column_num]) 说明&#xff1a;函数返回表格或区域中…