关于SwipeRefreshLayout的刷新事件

版权声明:欢迎转载,但请声明出处http://blog.csdn.net/dazhaodai https://blog.csdn.net/dazhaoDai/article/details/73730059

--关于SwipeRefreshLayout使用的小记录
我们常见的SwipeRefreshLayout包裹Recyclerview,实现下拉加载-更新数据,常见布局例如:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

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

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/transparent"
            android:fadeScrollbars="true"
            android:scrollbars="vertical" />
    </android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>

超级简单的布局,在Activity或者Fragment中,我们想在打开当前View时候,使SwipeRefreshLayout处于加载状态,例如在Activity的onCreate方法中,初始化SwipeRefreshLayout操作后调用setRefreshing方法:

SwipeRefreshLayout  refreshlayout = (SwipeRefreshLayout) findviewbyId(R.id.refreshlayout );
refreshlayout .setRefreshing(true);

编译运行,没有效果;
真正调用的方法:

  refreshlayout.post(new Runnable() {
            @Override
            public void run() {
           refreshlayout.setRefreshing(true);     
            }
        });

取消加载效果则

refreshlayout.post(() -> refreshlayout.setRefreshing(false));
refreshlayout.setRefreshing(false);

都可以

展开阅读全文

没有更多推荐了,返回首页