MaterialDesign--下拉刷新控件--SwipeRefreshLayout

前言:

从今天开始,学习和记录一个学习google自发布MaterialDesign风格而加入的新控件.今天是看的SwipeRefreshLayout,一个下拉刷新控件.现在很多app上都用的是这个,网易,知乎等都用上了,MaterialDesign风格标准化真的是大势所趋.先看一下网易的效果:  

  


需求分析:

今天主要是想实现一下几个功能

1.手势下拉进入刷新状态,出现刷新的圆形箭头,开始刷新数据,数据刷新结束后隐藏

2.进入页面的时候,自动刷新数据,刷新结束后隐藏:


开始使用:

1.布局上声明使用:

这里要注意的地方是swipeRefreshLayout是一个特殊的布局,只允许有一个直接子控件.

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefreshLayout"
        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">

        </ListView>


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

2.手动下拉刷新功能实现:

activity绑定数据,设置刷新监听:

在onRefresh()里面做相应刷新数据的操作,我这里为了测试,就只是做的空倒计时后关闭刷新

        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {//正在刷新
                
                new CountDownTimer(3000, 500) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                        
                        
                    }

                    @Override
                    public void onFinish() {
                        //刷新结束
                        swipeRefreshLayout.setRefreshing(false);
                    }
                }.start();
            }
        });
        

3.自动下拉刷新功能实现:

思路是刚进入页面的时候,让swipeRefreshLayout.setRefreshing(true),然后走OnRefreshListener监听事件的onRefresh()方法加载刷新数据,一切都是那么刚刚好.

然而试了之后并没有什么用,原因在于swipeRefreshLayout.setRefreshing(true)方法不会触发OnRefreshListener.

解决方法:手动调用OnRefreshListener中的onRefresh()方法

(1)定义OnRefreshListener监听事件,为全局变量

    SwipeRefreshLayout.OnRefreshListener  onRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            new CountDownTimer(3000, 500) {
                @Override
                public void onTick(long millisUntilFinished) {


                }

                @Override
                public void onFinish() {
                    //刷新结束
                    swipeRefreshLayout.setRefreshing(false);
                }
            }.start();
        }
    };

(2)根据我们的需要,在onCreate()或者onResum()中手动使swipeRefreshLayout处于刷新状态,并调用OnRefreshListener中的onRefresh()方法

        //设置为正在刷新状态
        swipeRefreshLayout.setRefreshing(true);
        //触发监听的onRefresh()方法
        onRefreshListener.onRefresh();



更多了解:

其实想知道更多关于这个控件的使用,最好的方式就是看源码,而且这个控件的源码才1200多行,看起来不会有太大压力,注释写的都很清楚,例如这个类注释:

/**
 * The SwipeRefreshLayout should be used whenever the user can refresh the
 * contents of a view via a vertical swipe gesture. The activity that
 * instantiates this view should add an OnRefreshListener to be notified
 * whenever the swipe to refresh gesture is completed. The SwipeRefreshLayout
 * will notify the listener each and every time the gesture is completed again;
 * the listener is responsible for correctly determining when to actually
 * initiate a refresh of its content. If the listener determines there should
 * not be a refresh, it must call setRefreshing(false) to cancel any visual
 * indication of a refresh. If an activity wishes to show just the progress
 * animation, it should call setRefreshing(true). To disable the gesture and
 * progress animation, call setEnabled(false) on the view.
 * <p>
 * This layout should be made the parent of the view that will be refreshed as a
 * result of the gesture and can only support one direct child. This view will
 * also be made the target of the gesture and will be forced to match both the
 * width and the height supplied in this layout. The SwipeRefreshLayout does not
 * provide accessibility events; instead, a menu item must be provided to allow
 * refresh of the content wherever this gesture is used.
 * </p>
 */

说明了swipeRefreshLayout的主要用途和使用注意事项,总结一下主要是这几个点:

1.使用OnRefreshListener监听刷新

2.使用setRefreshing(boolean isRegreshing)设置刷新状态

3.使用setEnable(boolean isEnable)设置是否禁用手势下拉刷新

4.只支持一个直接子View,布局要求设置为宽和高都铺满.


还有很多其他的功能,例如设置swipeRefreshLayout的样式等,都可以从源码中找到,从structure窗口查看超级方便,今天太晚了,明天有时间会都测试一下.





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值