点击查看原文

  SwipeRefreshLayout是Google官方更新的一个Widget,可以实现下拉刷新的效果。完整包名:android.support.v4.widget.SwipeRefreshLayout .

一. 布局

复制代码
<android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/srl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

.............

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

复制代码

二. Activity继承SwipeRefreshLayout刷新监听

implements SwipeRefreshLayout.OnRefreshListener

三.添加监听

srl.setOnRefreshListener(this);

 四.改变刷新图标颜色

复制代码
CommonUtils.setColorSchemeResources(srl);

</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> CommonUtils {

    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 统一维护,方便修改
     *
     * @param sr_layout 下拉控件
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setColorSchemeResources(SwipeRefreshLayout sr_layout) {
        </span><span style="color: #0000ff;">if</span> (sr_layout != <span style="color: #0000ff;">null</span><span style="color: #000000;">) {
            sr_layout.setColorSchemeResources(R.color.refresh1, R.color.refresh2, R.color
                    .refresh3, R
                    .color.refresh4);
        }
    }

}</span></pre>
复制代码

五. 重写SwipeRefreshLayout刷新监听事件

复制代码
  //刷新
    @Override
    public void onRefresh() {
        isPullToRefreshRequest = true;
        //刷新数据
    }

</span><span style="color: #008000;">//</span><span style="color: #008000;">加载动画结束</span>

@Override
public void onAllRequestsFinished() {
super.onAllRequestsFinished();
srl.setRefreshing(
false);
}

复制代码

六. 效果