在上一篇文章中,我們知道了使用 scroll-view 可以實現上拉加載更多,但是由於 scroll-view 的限制,它無法實現下拉加載更多,這篇文章我們使用 view 組件來實現 上拉和下拉加載更多。
下拉加載更多:
1、在響應的 xxx.json 配置文件依次配置如下配置
>> enablePullDownRefresh:true 表示開啟下拉刷新
>>onReachBottomDistance:0
>> backgroundTextStyle 下拉 loading 的樣式,僅支持 dark/light
>> backgroundColor 窗口的背景色
2、上方后面2個沒有設置好,在下拉時,頁面頂部會出現一塊白色的區域。
3、在下拉刷新時,不可使用 wx.showLoading 提示(其余的提示我沒有測試),否則在 ios 下頁面回彈過多,導致看不到頂部的那個提示頁面刷新的區域。
4、頁面下拉會觸發 onPullDownRefresh 事件
5、防止 onPullDownRefresh 多次觸發,導致多次請求
上拉加載更多:
1、在對應的 xxx.json 中配置(不是必須的)
>> onReachBottomDistance 頁面上拉觸底事件觸發時距頁面底部距離,單位為px
2、頁面上拉在小於或等於 onReachBottomDistance 的距離時,會觸發 onReachBottom 事件
實現效果:
代碼實現:
1、頁面布局 loadMore.wxml 文件的編寫
- <view class='view-container'>
- <block wx:for='{{articles}}' wx:key='{{item.id}}'>
- <view class='articles-container'>
- <view class='info'>
- <image class='avatar' src='{{item.avatar}}'></image>{{item.nickname}}
- <text class='created-at'>{{item.created_at}}</text>
- <text class='category'>{{item.category}}</text>
- </view>
- </view>
- </block>
- <view class='data-loading' hidden='{{hidden}}'>
- 數據加載中...
- </view>
- </view>
2、樣式編寫 loadMore.wxss 文件的編寫
- .view-container {
- #fff;
- }
- .data-loading {
- height: 100rpx;
- line-height: 100rpx;
- #eee;
- text-align: center;
- font-size: 14px;
- }
- .articles-container {
- border-bottom: 1px solid #eee;
- margin: 30rpx 10rpx;
- #eee;
- }
- .articles-container .info {
- font-size: 12px;
- margin: 20rpx 0rpx;
- height: 100%;
- display: inline-block;
- }
- .articles-container .info .avatar {
- width: 60rpx;
- height: 60rpx;
- margin-right: 10rpx;
- border-radius: 60rpx;
- display: inline-block;
- vertical-align: middle;
- }
- .articles-container .info .created-at {
- margin: 0px 20rpx;
- display: inline-block;
- vertical-align: middle;
- }
- .articles-container .info .category {
- color: #3399ea;
- display: inline-block;
- vertical-align: middle;
- }
3、js 控制 loadMore.js 文件的編寫
- Page({
- data: {
- /**
- * 需要訪問的url
- */
- urls: [
- 'https://www.csdn.net/api/articles?type=more&category=home&shown_offset=0',
- 'https://www.csdn.net/api/articles?type=new&category=arch',
- 'https://www.csdn.net/api/articles?type=new&category=ai',
- 'https://www.csdn.net/api/articles?type=new&category=newarticles'
- ],
- /**
- * 當前訪問的url索引
- */
- currentUrlIndex: 0,
- /**
- * 獲取到的文章
- */
- articles: [],
- /**
- * 控制上拉到底部時是否出現 "數據加載中..."
- */
- hidden: true,
- /**
- * 數據是否正在加載中,避免數據多次加載
- */
- loadingData: false
- },
- onLoad: function(options) {
- this.loadData(false);
- },
- /**
- * 加載數據
- */
- loadData: function(tail, callback) {
- var that = this,
- urlIndex = that.data.currentUrlIndex;
- wx.request({
- url: that.data.urls[urlIndex],
- success: function(r) {
- var oldArticles = that.data.articles,
- newArticles = tail ? oldArticles.concat(r.data.articles) : r.data.articles;
- that.setData({
- articles: newArticles,
- currentUrlIndex: (urlIndex + 1) >= that.data.urls.length ? 0 : urlIndex + 1
- });
- if (callback) {
- callback();
- }
- },
- error: function(r) {
- console.info('error', r);
- },
- complete: function() {}
- });
- },
- /**
- * 監聽用戶下拉動作
- */
- onPullDownRefresh: function() {
- console.info('onPullDownRefresh');
- var loadingData = this.data.loadingData,
- that = this;
- if (loadingData) {
- return;
- }
- // 顯示導航條加載動畫
- wx.showNavigationBarLoading();
- // 顯示 loading 提示框,在 ios 系統下,會導致頂部的加載的三個點看不見
- // wx.showLoading({
- // title: '數據加載中...',
- // });
- setTimeout(function() {
- that.loadData(false, () => {
- that.setData({
- loadingData: false
- });
- wx.stopPullDownRefresh();
- // wx.hideLoading();
- wx.hideNavigationBarLoading();
- console.info('下拉數據加載完成.');
- });
- }, 1000);
- },
- /**
- * 頁面上拉觸底事件的處理函數
- */
- onReachBottom: function() {
- console.info('onReachBottom');
- var hidden = this.data.hidden,
- loadingData = this.data.loadingData,
- that = this;
- if (hidden) {
- this.setData({
- hidden: false
- });
- console.info(this.data.hidden);
- }
- if (loadingData) {
- return;
- }
- this.setData({
- loadingData: true
- });
- // 加載數據,模擬耗時操作
- wx.showLoading({
- title: '數據加載中...',
- });
- setTimeout(function() {
- that.loadData(true, () => {
- that.setData({
- hidden: true,
- loadingData: false
- });
- wx.hideLoading();
- });
- console.info('上拉數據加載完成.');
- }, 1000);
- }
- })
4、配置文件 loadMore.json 的編寫
- {
- "navigationBarTitleText": "上拉刷新和下拉加載更多",
- "enablePullDownRefresh": true,
- "onReachBottomDistance": 0,
- "backgroundTextStyle": "dark",
- "backgroundColor": "#c0d9ff"
- }
5、下拉刷新 上拉加載
完整代碼:
微信小程序實現上拉和下拉加載更多代碼:https://gitee.com/huan1993/weixin_mini_program_study/tree/master/pages/view-pull-up-load-more