1. 程式人生 > >android最新上拉載入,下拉重新整理

android最新上拉載入,下拉重新整理

     開發專案過程中基本都會用到listView的下拉重新整理和上滑載入更多,之前大家最常用的應該是pull to refresh或它的變種版吧,google官方在最新的Android.support.v4包中增加了一個新類SwipeRefreshLayout,地址 這個類的作用就是提供官方的下拉重新整理,並且效果相當不錯,而上拉載入更多則用我們自定義的listview,也是相當簡單。

下拉重新整理


簡單的介紹下:

  首先它是一個viewgroup,但是它只允許有一個子控制元件,子控制元件能是任何view,使用的時候,所在類實現OnRefreshListener介面,在onRefresh()方法中實現所要完成的任務,

findviewbyid得到SwipeRefreshLayout後,顯示重新整理動畫用SwipeRefreshLayout.setRefreshing(true);取消重新整理動畫用SwipeRefreshLayout.setRefreshing(false);相當簡單。

另外有一些其他的常用方法比如判斷時候正在顯示重新整理動畫,用SwipeRefreshLayout.isShown()。

佈局:

  1. <android.support.v4.widget.SwipeRefreshLayout
  2.         android:id="@+id/swip_index"
  3.         android:layout_width="match_parent"
  4.         android:layout_height="match_parent">
  5.         <com.example.listviewloadmore.LoadMoreListView
  6.             android:id="@+id/listView"
  7.             android:layout_width="wrap_content"
  8.             android:layout_height="wrap_content"
  9.             android:text
    ="@string/hello_world">
  10.         </com.example.listviewloadmore.LoadMoreListView>
  11.     </android.support.v4.widget.SwipeRefreshLayout>


上拉載入更多

思路:監聽滑動事件並判斷是否到達底部,到達底部的時候,回撥我們定義好的一個方法,從而達到上拉載入更多的目的。 但是具體怎麼監聽和回撥呢,我們不妨看看Button控制元件的點選事件的執行流程,Button繼承textView,textView繼承View 1.實現OnClickListener介面,重寫onClick方法 2.設定Button.setOnClickListener(this)或者new一個介面也一樣的。 檢視View原始碼,我們可得知 View類中有OnClickOistener介面,setOnClickListener方法 那麼我們在我們自己的listview中要實現的步驟狠清楚了 1.myListView繼承listView 2.實現onLoadMore介面 3.在類中宣告onLoadMore介面物件 4.設定點選方法setOnLoadMore 5.在需要的地方呼叫onLoadMore.loadMore方法。 下面附上MyListView原始碼:
  1. package com.example.listviewloadmore;  
  2. import android.annotation.SuppressLint;  
  3. import android.content.Context;  
  4. import android.util.AttributeSet;  
  5. import android.util.Log;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.widget.AbsListView;  
  9. import android.widget.AbsListView.OnScrollListener;  
  10. import android.widget.ListView;  
  11. publicclass LoadMoreListView extends ListView implements OnScrollListener{  
  12.     private View footer;  
  13.     privateint totalItem;  
  14.     privateint lastItem;  
  15.     privateboolean isLoading;  
  16.     private OnLoadMore onLoadMore;  
  17.     private LayoutInflater inflater;  
  18.     public LoadMoreListView(Context context) {  
  19.         super(context);  
  20.         init(context);  
  21.     }  
  22.     public LoadMoreListView(Context context, AttributeSet attrs) {  
  23.         super(context, attrs);  
  24.         init(context);  
  25.     }  
  26.     public LoadMoreListView(Context context, AttributeSet attrs, int defStyle) {  
  27.         super(context, attrs, defStyle);  
  28.         init(context);  
  29.     }  
  30.     @SuppressLint("InflateParams")  
  31.     privatevoid init(Context context) {  
  32.         inflater = LayoutInflater.from(context);  
  33.         footer = inflater.inflate(R.layout.load_more_footer,null ,false);  
  34.         footer.setVisibility(View.GONE);  
  35.         this.addFooterView(footer);  
  36.         this.setOnScrollListener(this);  
  37.     }  
  38.     @Override
  39.     publicvoid onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {  
  40.         this.lastItem = firstVisibleItem+visibleItemCount;  
  41.         this.totalItem = totalItemCount;  
  42.     }  
  43.     @Override
  44.     publicvoid onScrollStateChanged(AbsListView view, int scrollState) {  
  45.         if(this.totalItem == lastItem&&scrollState == SCROLL_STATE_IDLE){  
  46.             Log.v("isLoading""yes");  
  47.             if(!isLoading){  
  48.                 isLoading=true;  
  49.                 footer.setVisibility(View.VISIBLE);  
  50.                 onLoadMore.loadMore();  
  51.             }  
  52.         }  
  53.     }  
  54.     publicvoid setLoadMoreListen(OnLoadMore onLoadMore){  
  55.         this.onLoadMore = onLoadMore;  
  56.     }  
  57.     /** 
  58.      * 載入完成呼叫此方法 
  59.      */
  60.     publicvoid onLoadComplete(){  
  61.         footer.setVisibility(View.GONE);  
  62.         isLoading = false;  
  63.     }  
  64.     publicinterface OnLoadMore{  
  65.         publicvoid loadMore();  
  66.     }  
  67. }  


程式碼不是很複雜,認真看一定能看明白。 底部正在載入佈局程式碼
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:id="@+id/load_more_footer"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="wrap_content">
  6.     <LinearLayout
  7.         android:layout_width="wrap_content"
  8.         android:layout_height="wrap_content"
  9.         android:layout_centerInParent="true">
  10.         <ProgressBar
  11. 相關推薦

    android最新載入重新整理

         開發專案過程中基本都會用到listView的下拉重新整理和上滑載入更多,之前大家最常用的應該是pull to refresh或它的變種版吧,google官方在最新的Android.support.v4包中增加了一個新類SwipeRefreshLayout,地

    XListView載入重新整理

    1.MainActivity頁面 public class MainActivity extends AppCompatActivity { private XListView xListView; private int page; private NewsAdapter adapt

    使用vue-mint-ui 載入重新整理效果

    剛開始進入公司,搞得是vue開發的微信公眾號,花了好幾天做了一個這個效果,自己都笑哭了。做了這麼長時間。 <!-- <div class="dataSet" v-for="(item,index) in bulletinList" :

    iscroll.js 移動端載入重新整理功能實現

    如下圖所示,我需要做一個上拉載入,下拉重新整理的功能:              首先在 html 中引用這個外掛: <script src="/js/common/iscroll.js"></script> 然後插入我們的資料: <d

    功能強大的RecyclerView實現 (單/多子項佈局載入載入重新整理左劃刪除)

           先看效果,可以像普通recyclerView那樣只加載一種列表子項,也可以同時載入不同的列表子項,已經寫好了常用必備的功能:正常的適配渲染,上拉載入,下拉重新整理,左劃刪除。 核心是XRefreshView+MultiTypeAdapter兩者組合使用,搭

    MUI載入重新整理

    前言:這是最近專案中的一個新聞列表 1.DOM容器 這裡只貼主要的程式碼 <div id="refreshId" class="mui-content mui-scroll-wrapper" style="background-col

    載入重新整理列表資料多條目列表,預設圖圓角

    package yxr.com.wekk2_lixie; //viewpager+Fragment滑動 import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import

    微信小程式--刷臉認證(人臉識別載入重新整理

    //獲取應用例項 const app = getApp() var page = 1; var isfinish = false;//載入完畢 function loadmore(that){ if(isfinish) return; wx.showLoading({ title: '正在載入

    移動端載入重新整理效果Demo

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="renderer" content="webkit">

    【前端】dropload 載入重新整理(不推薦使用)

    dropload 上拉載入,下拉重新整理 dropload 指令碼 2018-4-18 測試:不能正常執行 蘋果6版本是8.3(12f70) 低版本安卓 5.1 得 瀏覽器不支援,微信最

    vue mint-ui 載入重新整理

    1.html部分 <div class="detail" ref="wrapper"> <mt-loadmore :top-method="loadTop" :bottom-method="loadBottom" @bottom-status-chan

    XlistView 載入重新整理

    public class MainActivity extends Activity implements IXListViewListener { private XListView xListView; private String uul = "http://www.oschina.net/act

    react專案中通過 iscroll 實現載入重新整理

    基於 iscroll 實現的 react 元件 第一步:引入外掛 npm install reactjs-iscroll --save 可在專案下的package.json檔案內dependencies下看到安裝好的外掛版本; 第二步:在專案中使用 import iS

    PullToRefreshListView載入重新整理 重新整理網路資料 簡單實現ListView顯示網路資料

    1、依賴: compile 'com.google.code.gson:gson:2.6.2' compile 'com.github.userswlwork:pull-to-refresh:1.0.0' 2、許可權: <uses-permission andro

    andbase框架實現載入重新整理和自定義旋轉動畫的方式

    1、今天做列表時,需求上需要做一個下拉重新整理,下拉載入更多的功能,就上網找了一些例子,由於我原來用的就是andbase框架,就還是用它原來寫的了。其中同事給我推薦另一個框架BGARefreshLayout-Android,下載地址https://github.com/bin

    react-native之上載入重新整理元件封裝

    react-native 自定義封裝重新整理元件 幾個月沒寫部落格了,最近一直在寫react 和react-native,前幾天剛發了一版基於react-native混合開發的App,這幾天趕快總結下。 寫過java的同學,再去學習react和rea

    vant 載入重新整理

    1.使用vant中的list和PullRefresh元件 import { PullRefresh,List } from 'vant'; Vue.use(PullRefresh).use(List);   2.程式碼demo <van-pull-ref

    PullToRefresh載入重新整理

    1—新增依賴 implementation ‘com.github.userswlwork:pull-to-refresh:1.0.0’ implementation ‘com.google.code.gson:gson:2.8.5’ 2— 新增許可權 3—添加布局 <

    vue-載入重新整理元件

    vue在移動端開發過程中,上拉載入、下拉重新整理是頁面的基本需求,現在給大家介紹一種基於touch事件封裝的重新整理元件。 元件支援傳參、傳遞事件、請求成功非同步回撥、上拉與觸底觸發載入或重新整理。 父子元件間的通訊 這裡我們有兩個頁面,父元件note.vue與重新整理元件baseScroll.vue。

    微信小程式學習(18) —— 載入重新整理

    在微信小程式上實現下拉重新整理、上拉載入的效果 使用系統提供的onPullDownRefresh、onReachBottom這2個事件, 前提需要在app.json或page.json配置檔案中設定,才能使用。 app.json是全應用的頁面都可以使用該事件