微信小程序可谓是9月21号之后最火的一个名词了,一经出现真是轰炸了整个开发人员,当然很多app开发人员有了一个担心,微信小程序的到来会不会让移动端app颠覆,让移动端的程序员失业,身为一个android开发者我是不相信的,即使有,那也是需要个一两年的过度和打磨才能实现的吧。
不管微信小程序是否能颠覆当今的移动开发格局,我们都要积极向上的心态去接收,去学习。不排斥新技术,所以,心动不如行动,赶紧先搭建一个微信小程序开发工具。那么接下来就让我们来开始学习列表的上拉加载和下拉刷新的实现吧(通过聚合数据平台获取微信新闻)。
1.介绍几个组件
1.1 scroll-view 组件
图片:1.jpg
注意:使用竖向滚动时,需要给一个固定高度,通过 wxss 设置 height。
1.2 image组件
图片:2.jpg
注意:mode有12种模式,其中3种是缩放模式,9种是裁剪模式。
1.3 icon组件
图片:3.jpg
icontype: [ ‘success', ‘info', ‘warn', ‘waiting', ‘safe_success', ‘safe_warn', ‘success_circle', ‘success_no_circle', ‘waiting_circle', ‘circle', ‘download',
‘info_circle', ‘cancel', ‘search', ‘clear'
]
2.列表的上拉加载和下拉刷新的实现
2.1先来张效果图
图片:4.gif
2.2逻辑很简单,直接上代码
2.2.1 detail.wxml 布局文件
<loading hidden="pw_hidden" bindchange="loadingchange">
加载中... </loading>
<scroll-view scroll-y="true" style="height: 100%;" bindscrolltolower="loadmore" bindscrolltoupper="refesh"> <view wx:if="pw_hasrefesh" style="display: flex;flex-direction: row;align-items: center;align-self: center;justify-content: center;"> <icon type="waiting" size="45"/><text>刷新中...</text></view> <view wx:else style="display:none" ><text></text></view> <view class="lll" wx:for="pw_list" wx:for-item="item" bindtap="bindviewtap" data-title="pw_item.title" > <image style=" width: 50px;height: 50px;margin: 20rpx;" src="pw_item.firstimg" ></image> <view class="eee" >
<view style="margin:5px;font-size:8px"> 标题:pw_item.title</view> <view style="margin:5px;color:red;font-size:6px"> 来源:pw_item.source</view> </view></view><view class="tips1"> <view wx:if="pw_hasmore" style="display: flex;flex-direction: row;align-items: center;align-self: center;justify-content: center;"> <icon type="waiting" size="45"/><text>玩命的加载中...</text></view> <view wx:else><text>没有更多内容了</text></view> </view> </scroll-view>
2.2.1 detail.js逻辑代码文件
var network_util = require('../../utils/network_util.js');var json_util = require('../../utils/json_util.js');page({
data:{ // text:"这是一个页面"
list:[],
dd:'',
hidden:false,
page: 1,
size: 20,
hasmore:true,
hasrefesh:false },
onload:function(options){ var that = this; var url = 'http://v.juhe.cn/weixin/query?key=f16af393a63364b729fd81ed9fdd4b7d&pno=1&ps=10';
network_util._get(url, function(res){
that.setdata({
list:res.data.result.list,
hidden: true, }); },function(res){
console.log(res); }); },
onready:function(){ // 页面渲染完成 },
onshow:function(){ // 页面显示 },
onhide:function(){ // 页面隐藏 },
onunload:function(){ // 页面关闭 }, //点击事件处理
bindviewtap: function(e) {
console.log(e.currenttarget.dataset.title); }, //加载更多
loadmore: function(e) { var that = this;
that.setdata({
hasrefesh:true,}); if (!this.data.hasmore) return var url = 'http://v.juhe.cn/weixin/query?key=f16af393a63364b729fd81ed9fdd4b7d&pno='+(++that.data.page)+'&ps=10';
network_util._get(url, function(res){
that.setdata({
list: that.data.list.concat(res.data.result.list),
hidden: true,
hasrefesh:false, }); },function(res){
console.log(res); })},//刷新处理refesh: function(e) { var that = this;
that.setdata({
hasrefesh:true, }); var url = 'http://v.juhe.cn/weixin/query?key=f16af393a63364b729fd81ed9fdd4b7d&pno=1&ps=10';
network_util._get(url, function(res){
that.setdata({
list:res.data.result.list,
hidden: true,
page:1,
hasrefesh:false, }); },function(res){
console.log(res); })},})
最后的效果:
图片:5.jpg
以上就是小程序开发之列表的上拉加载和下拉刷新效果实现教程 的详细内容。