android之官方下拉重新整理組件SwipeRefreshLayout

來源:互聯網
上載者:User

標籤:

一、問題描述

  在android開發中,使用最多的資料重新整理方式就是下拉重新整理了,而完成此功能我們使用最多的就是第三方的開源庫PullToRefresh。現如今,google也忍不住推出了自己的下拉組件SwipeRefreshLayout,下面我們通過api文檔和源碼來分析學習如何使用SwipeRefreshLayout。

  先看:

二、SwipeRefreshLayout的具體用法

  下面我們來看SwipeRefreshLayout的具體用法,顧名思義此組件就是一個布局,只不過要注意的是此布局內只能有一個直接子View。其實通過文檔我們可以知道SwipeRefreshLayout只不過是繼承了ViewGroup。

  查看文檔,我們可以知道,在SwipRefreshLayout中存在一個介面,通過此介面我們可以監聽滑動手勢,其實使用此組件最重要的步驟就是實現此介面的onRefresh方法,在此方法中實現資料的更新操作。如下:

  介面中的方法:

 

  除了OnRefreshListener介面外,SwipRefreshLayout中還有一些其他重要的方法,具體如下:

         1、setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener listener):設定手勢滑動監聽器。

         2、setProgressBackgroundColor(int colorRes):設定進度圈的背景色。

         3、setColorSchemeResources(int… colorResIds):設定進度動畫的顏色。

         4、setRefreshing(Boolean refreshing):設定組件的刷洗狀態。

         5、setSize(int size):設定進度圈的大小,只有兩個值:DEFAULT、LARGE

  弄清楚API後,我們下面進行實際編碼,首先先做布局,具體內容如下:

<?xml version="1.0" encoding="utf-8"?><android.support.v4.widget.SwipeRefreshLayout     xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:id="@+id/swipeLayout" >         <ListView         android:id="@+id/mylist"        android:layout_width="match_parent"        android:layout_height="wrap_content"/>    </android.support.v4.widget.SwipeRefreshLayout>

  Activity核心代碼如下:

swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swipeLayout);        swipeRefreshLayout.setColorSchemeResources(R.color.swipe_color_1,                R.color.swipe_color_2,                R.color.swipe_color_3,                R.color.swipe_color_4);        swipeRefreshLayout.setSize(SwipeRefreshLayout.LARGE);;        swipeRefreshLayout.setProgressBackgroundColor(R.color.swipe_background_color);        //swipeRefreshLayout.setPadding(20, 20, 20, 20);        //swipeRefreshLayout.setProgressViewOffset(true, 100, 200);        //swipeRefreshLayout.setDistanceToTriggerSync(50);        swipeRefreshLayout.setProgressViewEndTarget(true, 100);        swipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {            @Override            public void onRefresh() {                new Thread(new Runnable() {                    @Override                    public void run() {                        data.clear();                        for(int i=0;i<20;i++){                            data.add("SwipeRefreshLayout下拉重新整理"+i);                        }                        try {                            Thread.sleep(5000);                        } catch (InterruptedException e) {                            e.printStackTrace();                        }                        mHandler.sendEmptyMessage(1);                    }                }).start();            }        });    //handler    private Handler mHandler = new Handler(){        @Override        public void handleMessage(Message msg) {            super.handleMessage(msg);            switch (msg.what) {            case 1:                                swipeRefreshLayout.setRefreshing(false);                adapter.notifyDataSetChanged();                //swipeRefreshLayout.setEnabled(false);                break;            default:                break;            }        }    };

  通過如上步驟,我們就實現了一個簡單的下拉重新整理操作,在此基礎上,我們可以分析研究一下SwipeRefreshLayout是如何?的。

  通過源碼我們發現SwipeRefreshLayout中的兩個重要的屬性:

private MaterialProgressDrawable mProgress;

private CircleImageView mCircleView;

  這兩個屬性正是用於實現進度動畫效果的,在方法createProgressView中,我們看到mCircleView最終加入到了SwipeRefreshLayout中。

private void createProgressView() {        mCircleView = new CircleImageView(getContext(), CIRCLE_BG_LIGHT, CIRCLE_DIAMETER/2);        mProgress = new MaterialProgressDrawable(getContext(), this);        mProgress.setBackgroundColor(CIRCLE_BG_LIGHT);        mCircleView.setImageDrawable(mProgress);        mCircleView.setVisibility(View.GONE);        addView(mCircleView);    }

  同時我們也可以查看到CirlceImageView繼承了ImageView,MaterialProgressDrawabel繼承了Drawable,至此我們也就明白了下來進度動畫是如何?的了,具體的細節在不做過多贅述,可自行查看源碼。

class CircleImageView extends ImageView

class MaterialProgressDrawable extends Drawable implements Animatable

  具體的下拉功能實現主要是在onInterceptTouchEvent、onTouchEvent方法中完成的,在此我就不在將具體代碼貼出來了,大家可自行查看。

 

  想要進一步瞭解的同學,可以下載完整工程,自行運行查看!有問題也可以跟帖討論哦~

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.