Android下拉刷新-SwipeRefreshLayout


現在市面上新聞類的App基本上都有下拉刷新,算是一個標配吧,網上關於下拉刷新的博客也有很多,實現方式可以使用開源的PullToRefresh,自定義ListView,或者可以直接使用LineLayOut直接搞定的。不過Google在今年在support v4 19.1版本的library推出了SwipeRefreshLayout,字面上的意思就是下拉刷新,繼承自ViewGroup,而如今google推出了更官方的下拉刷新組件,對於開發者而言無疑是一個好事情,比較少的代碼實現需要的功能。

基本布局

先來看下簡單的布局,在最外層加上SwipeRefreshLayout,但是子的View需要時可滾動的(ScrollView或ListView)

<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"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefreshLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >
        <ListView
            android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

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


</RelativeLayout>

 布局效果如下:

Demo實現

MainActivity中onCreate中的初始化一下SwipeLayOut,需要注意的方法是setColorScheme(), 設置進度條的顏色主題,最多能設置四種;

  myListView = (ListView) findViewById(R.id.listView);
        mySwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);

        mySwipeRefreshLayout.setOnRefreshListener(this);
        mySwipeRefreshLayout.setColorScheme(android.R.color.holo_blue_bright, android.R.color.holo_green_light,
                android.R.color.holo_orange_light, android.R.color.holo_red_light);
        listAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,listIDE);
        myListView.setAdapter(listAdapter);

 

MainActivity中需要實現一下 SwipeRefreshLayout.OnRefreshListener 

  @Override
    public void onRefresh() {

        refreshHandler.sendEmptyMessageDelayed(REFRESH_STATUS, 1500);
    }

 最后初始化一下數據setRefreshing(boolean):,顯示或隱藏刷新進度條

  private static final int REFRESH_STATUS =0;
    private ListView myListView;
    private SwipeRefreshLayout mySwipeRefreshLayout;
    private ArrayAdapter<String> listAdapter;
    private List<String> listIDE = new ArrayList<String>(Arrays.asList("Visual Studio", "Android Studio", "Eclipse", "Xcode"));
    private Handler refreshHandler = new Handler()
    {
        public void handleMessage(android.os.Message msg)
        {
            switch (msg.what)
            {
                case REFRESH_STATUS:
                    listIDE.removeAll(listIDE);
                    listIDE.addAll(Arrays.asList("C#", "Java", "C++","Object-C"));
                    listAdapter.notifyDataSetChanged();
                    mySwipeRefreshLayout.setRefreshing(false);
                    break;

            }
        };
    };

 最后的效果如下:

 參考資料:https://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html


免責聲明!

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



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