Android官方的SwipeRefreshLayout

來源:互聯網
上載者:User

標籤:swiperefreshlayout   android官方推出   下拉重新整理外掛程式   

這個是基於最新v4包實現的一個下拉重新整理的東東~~~

先給大家透露一下,整體很簡單,畢竟不是自訂,還請大家放寬心對待!!!廢話不多說,直接貼代碼

package com.bob.swiperefresh;import android.app.Activity;import android.os.Message;import android.support.v4.widget.*;import android.os.Bundle;import android.widget.ArrayAdapter;import android.widget.ListView;import java.util.ArrayList;import java.util.List;import android.os.Handler;/*這是一個下拉重新整理的demo  1、先用官方的SwipeRefreshLayout來實現吧___好醜的說 */public class MainActivity extends Activity implements SwipeRefreshLayout.OnRefreshListener {    private int index = 0;    private static final int REFRESH_FINISH = 1; //設定一個重新整理完成的標記量    private SwipeRefreshLayout mSwipe;    private ListView listView;    private String[] arrays = new String[]{"Hello", "I am a student", "I love android", "love open Resource"};    private ArrayAdapter<String> adapter;    private List<String> data = new ArrayList<>();//asList是數組和集合之間的橋樑,不能直接用於初始化List介面變數,否則在加資料的時候會崩掉    private Handler handler = new Handler() {        @Override        public void handleMessage(Message msg) {            switch (msg.what) {                case REFRESH_FINISH:                    data.add(getData());                    adapter.notifyDataSetChanged();//提示資料配接器資料發生改變從而更新適配器裡的資料                    mSwipe.setRefreshing(false);//隱藏重新整理進度條                    break;            }        }    };    public String getData() {//迴圈擷取數組中的資訊        String re = arrays[index];        index = (index + 1) % arrays.length;        return re;    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mSwipe = (SwipeRefreshLayout) findViewById(R.id.swipe_ly);        listView = (ListView) findViewById(R.id.lv);        adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, data);        listView.setAdapter(adapter);        mSwipe.setOnRefreshListener(this);        mSwipe.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light,                android.R.color.holo_orange_light, android.R.color.holo_red_light);        //這裡設定載入動畫的顏色可選項,最多設定4種    }    @Override    public void onRefresh() {//應該這麼使用:網路訪問或者其他的費時操作在這裡邊,最後發送message就ok        //說明一下,這個demo裡更新ui的東東不一定非要用handler,也可以直接在這裡更新ui,只是沒有了延遲的效果,但在        //項目裡肯定還是需要子線程來實現的        handler.sendEmptyMessageDelayed(REFRESH_FINISH, 4000);//發送一個空訊息並添加延遲時間    } /*   public void onRefresh(){//或者採用這種方式也行,        new Handler().postDelayed(new Runnable() {            @Override            public void run() {                mSwipe.setRefreshing(false);            }        },4000);    }*/}


布局檔案:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity">    <android.support.v4.widget.SwipeRefreshLayout        android:id="@+id/swipe_ly"        android:layout_width="match_parent"        android:layout_height="match_parent"        >        <ListView            android:id="@+id/lv"            android:layout_width="match_parent"            android:layout_height="match_parent"></ListView>    </android.support.v4.widget.SwipeRefreshLayout></RelativeLayout>


資料是我一條一條重新整理出來的,具體實現邏輯可以在源碼中看到~~~

據我所知,如果v4版本較低的話效果是“知乎用戶端”的那個效果,現在最新的這個目前用在了“coursera”用戶端上,有興趣的盆友可以自行看看

(ps:渣渣用的是Linux,暫時還沒有找到比較稱心的gift錄製工具,如博友有推薦,還請留言告知!!!跪謝!!!)

github地址

Android官方的SwipeRefreshLayout

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.