Android下拉刷新-SwipeRefreshLayout

十度 Android 2015年12月01日 收藏

现在市面上新闻类的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