Android下拉刷新SwipeRefreshLayout简单用法

1.布局代码

<android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/mswipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <WebView
            android:id="@+id/mwebview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
        </WebView>
</android.support.v4.widget.SwipeRefreshLayout>

2.Activity代码

mSwipe =  findViewById(R.id.mswipeRefreshLayout);

private void setSwipe() {
        /*
        * 设置下拉刷新球颜色
        */
        mSwipe.setColorSchemeResources(R.color.swipeColor1,R.color.swipeColor2,R.color.swipeColor3,R.color.swipeColor4,R.color.swipeColor5);
        /*
        * 设置下拉刷新的监听
        */
        mSwipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //刷新需执行的操作
            }
        });
}

我们可以在res/values/colors.xml文件中设置我们想要的颜色

<color name="swipeColor1">#3F51B5</color>
<color name="swipeColor2">#000000</color>

加载完毕后需要关闭下拉刷新球

mSwipe.setRefreshing(false);

3.打开页面自动刷新

mSwipe.measure(0,0);
mSwipe.setRefreshing(true);

在onMeasure之前 单独使用setRefreshing(true)是没有效果的

还可以复写onWindowFocusChanged当页面获得焦点的时刷新

 @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        mSwipe.setRefreshing(true);
    }

也可以放到ui线程消息循环中排队

mSwipe.post(new Runnable() {

            @Override
            public void run() {
                mSwipe.setRefreshing(true);
            }
        });

在刷新结束后关闭刷新球

public void SwipeIsFinish(){
        if (mSwipe.isRefreshing()) {
            mSwipe.setRefreshing(false);
        }
   }

 

基于SwipeRefreshLayout下拉刷新、上拉加载。支持所有的AbsListView、RecycleView 特点  在 layout 中使用,支持 AbsListView 所有的xml属性  支持自动下拉刷新,什么用呢?比如进入界面时,只需要调用 autoRefresh() 方法即可,同时下拉刷新回调函数将会被调用。  上拉加载支持自定义 View 或设置加载文字、动画  轻松设置 Adapter 空数据视图,默认为 TextView 支持更文字,也可自定义 View  对于简单的界面,如只有 ListView 可以继承 app 包中 Fragment 轻松搞定 效果图 使用 仔细看 listSelector 属性,效果见 sample <com.mylhyl.prlayout.SwipeRefreshListView xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:id="@ id/swipeRefresh"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:listSelector="@drawable/selector_list"     tools:context=".app.ListViewXmlFragment" /> 设置上拉加载,更多方法见 IFooterLayout         IFooterLayout footerLayout = swipeRefreshListView.getFooterLayout();         footerLayout.setFooterText("set自定义加载");         footerLayout.setIndeterminateDrawable(getResources().getDrawable(R.drawable.footer_progressbar)); 自定义adapter空数据视图         ImageView emptyView = new ImageView(getContext());         emptyView.setImageResource(R.mipmap.empty);         swipeRefreshGridView.setEmptyView(emptyView); 或         swipeRefreshListView.setEmptyText("数据呢?"); 使用Gradle构建时添加一下依赖即可: compile 'com.mylhyl:pullrefreshlayout:1.0.0' QQ交流群:435173211
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值