12Material Design-下拉刷新
2018-04-08  本文已影响62人 
何惧l
SwipeRefreshLayout就是用于实现下拉刷新功能的核心类,是由support-V4提供的,把要实现下来刷新功能的控件放入到这个里面,就可以迅速让这个控件支持下拉刷新
- 在这个项目中,应该支持下拉刷新的功能就是RecyclerView了,修改activity_main.xml中的代码
 
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
    ...
        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swipe_refresh"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            >
            <android.support.v7.widget.RecyclerView
                android:id="@+id/recycler_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                >
            </android.support.v7.widget.RecyclerView>
        </android.support.v4.widget.SwipeRefreshLayout>
    ...
    </android.support.design.widget.CoordinatorLayout>
    ...
</android.support.v4.widget.DrawerLayout>
- 这里在RecyclerView的外面又嵌套一层SwipeRefreshLayout,这样RecyclerView就具有了下拉刷新的功能
 - 注意:这里由于RecyclerView变成了SwipeRefreshLayout的子控件,之前使用的 app:layout_behavior声明的布局行为也要移动到SwipeRefreshLayout中了
 
- 这只是添加了下拉刷新的功能,但是还要在代码中处理具体的刷新逻辑才行,修改MainActivity中的代码
 
package com.example.tool;
public class MainActivity extends AppCompatActivity {
    private SwipeRefreshLayout swipeRefreshLayout;
    ...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
   
        ...
        swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swipe_refresh);
        // 设置颜色
        swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshFruits();
            }
        });
    }
    private void refreshFruits(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        initFruits();
                        adapter.notifyDataSetChanged();
                        swipeRefreshLayout.setRefreshing(false);
                    }
                });
            }
        }).start();
    }
       // 添加信息
    private void initFruits(){
        fruitList.clear();
        for (int i = 0; i<50; i++){
            Random random = new Random();
            int index = random.nextInt(fruits.length);
            fruitList.add(fruits[index]);
        }
    }
}
- 首先获取实例,然后调用setColorSchemeResources()方法设置下拉刷新进度条的颜色,
 - 调用setOnRefreshListener()方法进行下拉刷新监听器,当触发的时候就会回调这个onRefresh()函数
 - 通常情况下onRefresh()方法是去网络中请求一个最新的数据,这里为了简单就调用 refreshFruits()方法实现本地刷新
 - 在这个方法中,首先开启了一个线程,然后将线程睡两秒,然后使用runOnUiThread()方法将线程切回主线程,然后调用 initFruits()方法重新生成数据,然后调用FruitAdapter的notifyDataSetChanged()方法通知数据发生了变化
 - 最后调用swipeRefreshLayout.setRefreshing()方法并传入一个false,用于表示刷新事件结束,并隐藏刷新进度条
 - 
运行程序就可以看到
实现下拉刷新.png
下拉进度条只会停留两秒,之后就自动消失,界面上的水果也会发生变化