微信小程序 實現(xiàn)列表刷新的實例詳解
微信小程序 列表刷新:
微信小程序,最近自己學(xué)習(xí)微信小程序的知識,就想實現(xiàn)現(xiàn)在APP 那種列表刷新,下拉刷新,上拉加載等功能。
先開看一下界面


1.wx.request (獲取遠程服務(wù)器的數(shù)據(jù),可以理解成$.ajax)
2. scroll-view的兩個事件
2.1 bindscrolltolower(滑到頁面底部時)
2.2 bindscroll (頁面滑動時)
2.3 bindscrolltoupper (滑倒頁面頂部時)
然后我們看代碼,詳細描述。
index.js
var url = "http://www.imooc.com/course/ajaxlist";
var page =0;
var page_size = 20;
var sort = "last";
var is_easy = 0;
var lange_id = 0;
var pos_id = 0;
var unlearn = 0;
// 獲取數(shù)據(jù)的方法,具體怎么獲取列表數(shù)據(jù)大家自行發(fā)揮
var GetList = function(that){
that.setData({
hidden:false
});
wx.request({
url:url,
data:{
page : page,
page_size : page_size,
sort : sort,
is_easy : is_easy,
lange_id : lange_id,
pos_id : pos_id,
unlearn : unlearn
},
success:function(res){
//console.info(that.data.list);
var list = that.data.list;
for(var i = 0; i < res.data.list.length; i++){
list.push(res.data.list[i]);
}
that.setData({
list : list
});
page ++;
that.setData({
hidden:true
});
}
});
}
Page({
data:{
hidden:true,
list:[],
scrollTop : 0,
scrollHeight:0
},
onLoad:function(){
// 這里要非常注意,微信的scroll-view必須要設(shè)置高度才能監(jiān)聽滾動事件,所以,需要在頁面的onLoad事件中給scroll-view的高度賦值
var that = this;
wx.getSystemInfo({
success:function(res){
console.info(res.windowHeight);
that.setData({
scrollHeight:res.windowHeight
});
}
});
},
onShow:function(){
// 在頁面展示之后先獲取一次數(shù)據(jù)
var that = this;
GetList(that);
},
bindDownLoad:function(){
// 該方法綁定了頁面滑動到底部的事件
var that = this;
GetList(that);
},
scroll:function(event){
// 該方法綁定了頁面滾動時的事件,我這里記錄了當(dāng)前的position.y的值,為了請求數(shù)據(jù)之后把頁面定位到這里來。
this.setData({
scrollTop : event.detail.scrollTop
});
},
refresh:function(event){
// 該方法綁定了頁面滑動到頂部的事件,然后做上拉刷新
page = 0;
this.setData({
list : [],
scrollTop : 0
});
GetList(this)
}
})
index.wxml
<view class="container">
<scroll-view scroll-top="{{scrollTop}}" scroll-y="true" style="height:{{scrollHeight}}px;"
class="list" bindscrolltolower="bindDownLoad" bindscroll="scroll" bindscrolltoupper="refresh">
<view class="item" wx:for="{{list}}">
<image class="img" src="{{item.pic_url}}"></image>
<view class="text">
<text class="title">{{item.name}}</text>
<text class="description">{{item.short_description}}</text>
</view>
</view>
</scroll-view>
<view class="body-view">
<loading hidden="{{hidden}}" bindchange="loadingChange">
加載中...
</loading>
</view>
</view>
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
Android判斷App前臺運行還是后臺運行(運行狀態(tài))
這篇文章主要介紹了Android判斷App前臺運行還是后臺運行的相關(guān)資料,需要的朋友可以參考下2016-04-04
Android table布局開發(fā)實現(xiàn)簡單計算器
這篇文章主要為大家詳細介紹了Android table布局開發(fā)實現(xiàn)簡單計算器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-05-05
Android Studio preview 不固定及常見問題的解決辦法
preview 可以幫助您預(yù)覽您的布局文件將如何在用戶的設(shè)備上呈現(xiàn)。這篇文章主要介紹了Android Studio preview 不固定及常見問題的解決辦法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05
Andriod Studio實現(xiàn)保存QQ密碼功能(案例代碼詳解)
這篇文章主要介紹了Andriod Studio實現(xiàn)保存QQ密碼功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03
android之BroadcastReceiver應(yīng)用詳解
這篇文章主要介紹了android之BroadcastReceiver應(yīng)用詳解,BroadcastReceiver也就是“廣播接收者”的意思,顧名思義,它就是用來接收來自系統(tǒng)和應(yīng)用中的廣播。有興趣的可以了解一下。2016-12-12
Android Fragment 和 FragmentManager 的代碼分析
這篇文章主要介紹了Android Fragment 和 FragmentManager 的代碼分析,非常不錯,具有參考借鑒價值,需要的的朋友參考下吧2017-01-01
Android Studio debug.keystore位置介紹
這篇文章主要介紹了Android Studio debug.keystore位置,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
詳解Android(共享元素)轉(zhuǎn)場動畫開發(fā)實踐
本篇文章主要介紹了詳解Android(共享元素)轉(zhuǎn)場動畫開發(fā)實踐,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-08-08

