#Android学习#下拉刷新SwipeRefreshLayout

下拉刷新是我们经常会用到的一个功能,网上也能找到很多的开源下拉刷新的资源,如ActionBar-PullToRefresh。但这些都需要引入第三方的库,而事实上,Google官方也推出了它的下拉刷新组件SwipeRefreshLayout,可以让我们很方便的实现下拉刷新的效果。
SwipeRefreshLayout的使用非常简单,主要分为两步:
1)在布局文件里,添加SwipeRefreshLayout控件,然后再这个控件里添加另外一个可滚动的组件,如ListView、RecyclerView、 ScrollView等。
2)在Activity里找到对应的组件,然后调用setOnRefreshListener,之后可通过setColorSchemeResources来设置刷新进度条的颜色。
结合前面已经学习过的RecydlerView,我们就可以很方便的做出下拉刷新的效果了。
通过Android Studio新建project,选择Blank Activity。修改content_main.xml 增加刷新控件

// content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/swipe_refresh_layout_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.cyfloel.learnswiperefreshlayout.MainActivity"
    tools:showIn="@layout/activity_main">

    <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>

注意app:layout_behavior="@string/appbar_scrolling_view_behavior" 这句可以使得刷新控件在toolbar下显示,如果缺少这个属性,SwipeRefreshLayout根据我们设置的match_parent 属性就会覆盖整个屏幕

    // MainActivity
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

    // 找到下拉组件
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout_container);
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    // 设置listener
        swipeRefreshLayout.setOnRefreshListener(this);
        // 设置刷新进度条的颜色
        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_purple,
                android.R.color.holo_blue_bright,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(new MyRecyclerViewAdapter());
    }
    @Override
    public void onRefresh() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(5000);
                    Message msg = mHandler.obtainMessage(0x1);
                    mHandler.sendMessage(msg);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x1){
                Snackbar.make(findViewById(R.id.mycoordinatorLayout),"刷新成功",Snackbar.LENGTH_SHORT).show();
                swipeRefreshLayout.setRefreshing(false);
            }
        }
    };

我们在刷新的时候通过线程停留5秒的时间,以体现效果。RecyclerView的使用可参考RecyclerView基础这篇文章。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值