关于SwipeRefreshLayout的刷新事件

作者: T9的第三个三角 | 来源:发表于2016-12-05 18:23 被阅读503次

--关于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);```
都可以

相关文章

网友评论

    本文标题:关于SwipeRefreshLayout的刷新事件

    本文链接:https://www.haomeiwen.com/subject/xoqlmttx.html