代碼實現Android5.0的下拉刷新效果


如圖所示,實現類似與gmail的下拉刷新。

項目地址:https://github.com/stormzhang/SwipeRefreshLayoutDemo

 

一、在xml文件中定義

這個控件在supportV4就提供了,叫做SwipeRefreshLayout。這個view其實就是一個父控件,我們可以如下定義。

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp" />

</android.support.v4.widget.SwipeRefreshLayout>

有沒有感覺它和listview一毛錢關系都沒有!就是這么方便,比之前的listview下拉刷新要簡單多了。用戶只要在這個控件的范圍里下拉,就會自動出現下拉的圓形小球。在實際使用中,我們還是需要在java代碼中進行簡單處理的。

 

二、通過java代碼進行設置

     mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
        mSwipeLayout.setOnRefreshListener(this);
        // 設置下拉圓圈上的顏色,藍色、綠色、橙色、紅色
        mSwipeLayout.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);
        mSwipeLayout.setDistanceToTriggerSync(400);// 設置手指在屏幕下拉多少距離會觸發下拉刷新
        mSwipeLayout.setProgressBackgroundColor(R.color.red); // 設定下拉圓圈的背景
        mSwipeLayout.setSize(SwipeRefreshLayout.LARGE); // 設置圓圈的大小

設置了背景和圓圈的大小后就變成了下面的樣子:

  

更多的設置方式可以去官網參考文檔:https://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html

設置監聽器:

public class MainActivity extends Activity implements SwipeRefreshLayout.OnRefreshListener {

//……

}
/* 
     * 監聽器SwipeRefreshLayout.OnRefreshListener中的方法,當下拉刷新后觸發
     */
    public void onRefresh() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                // 停止刷新
                mSwipeLayout.setRefreshing(false);
            }
        }, 5000); // 5秒后發送消息,停止刷新
    }

 

全部代碼:

/*
 * Created by Storm Zhang, Mar 31, 2014.
 */

package com.storm.swiperefreshlayout;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * @author:
 * @description  :stormzhang
 * @web: http://stormzhang.com/android/2014/10/27/android-swiperefreshlayout/
 * @date  :2015年1月19日
 */
public class MainActivity extends Activity implements SwipeRefreshLayout.OnRefreshListener {

    private SwipeRefreshLayout mSwipeLayout;
    private ListView mListView;
    private ArrayList<String> list = new ArrayList<String>();

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mListView = (ListView) findViewById(R.id.listview);
        mListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getData()));

        mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
        mSwipeLayout.setOnRefreshListener(this);
        // 設置下拉圓圈上的顏色,藍色、綠色、橙色、紅色
        mSwipeLayout.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);
        mSwipeLayout.setDistanceToTriggerSync(400);// 設置手指在屏幕下拉多少距離會觸發下拉刷新
        mSwipeLayout.setProgressBackgroundColor(R.color.red);
        mSwipeLayout.setSize(SwipeRefreshLayout.LARGE);
    }

    private ArrayList<String> getData() {
        list.add("Hello");
        list.add("This is stormzhang");
        list.add("An Android Developer");
        list.add("Love Open Source");
        list.add("My GitHub: stormzhang");
        list.add("weibo: googdev");
        return list;
    }

    /* 
     * 監聽器SwipeRefreshLayout.OnRefreshListener中的方法,當下拉刷新后觸發
     */
    public void onRefresh() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                // 停止刷新
                mSwipeLayout.setRefreshing(false);
            }
        }, 5000); // 5秒后發送消息,停止刷新
    }
}

 

源碼下載:http://download.csdn.net/detail/shark0017/8375685

 

參考的項目:https://github.com/stormzhang/SwipeRefreshLayoutDemo

參考博文:http://stormzhang.com/android/2014/10/27/android-swiperefreshlayout/

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM