前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android 自带的下拉刷新SwipeRefreshLayout

Android 自带的下拉刷新SwipeRefreshLayout

作者头像
码客说
发布2019-10-22 16:54:13
4K0
发布2019-10-22 16:54:13
举报
文章被收录于专栏:码客码客

概要

试了很多第三方的下拉刷新不是效果不好看 就是有bug,最后还是决定用官方的下拉刷新,但是官方的默认不支持进入页面立即刷新,所以我们可以用官方的并对其扩展

官方原版的用法

XML

代码语言:javascript
复制
<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/id_swipe_ly"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:id="@+id/file_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@color/zj_qianhuise"
        android:dividerHeight="1px">
    </ListView>
</android.support.v4.widget.SwipeRefreshLayout>

代码中

代码语言:javascript
复制
//定义变量
private SwipeRefreshLayout mSwipeLayout;

实现SwipeRefreshLayout.OnRefreshListener接口 添加回调方法

代码语言:javascript
复制
@Override
public void onRefresh() {
    loadData();
}

初始化

代码语言:javascript
复制
//下拉刷新
mSwipeLayout = (SwipeRefreshLayout) parentView.findViewById(R.id.id_swipe_ly);
mSwipeLayout.setOnRefreshListener(this);
//设置加载动画背景颜色
mSwipeLayout.setProgressBackgroundColorSchemeColor(getResources().getColor(android.R.color.background_light));
//设置进度动画的颜色
mSwipeLayout.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light, android.R.color.holo_orange_light, android.R.color.holo_red_light);

如上loadData()来加载数据,加载结束后记得调用下面的方法停止刷新动画

代码语言:javascript
复制
mSwipeLayout.setRefreshing(false);

进入页面立即刷新

但是我们想做到一进页面就立刻刷新,并有刷新动画怎么办

首先添加一个类

代码语言:javascript
复制
public class AutoSwipeRefreshLayout extends SwipeRefreshLayout {

    public AutoSwipeRefreshLayout(Context context) {
        super(context);
    }

    public AutoSwipeRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * 自动刷新
     */
    public void autoRefresh() {
        try {
            Field mCircleView = SwipeRefreshLayout.class.getDeclaredField("mCircleView");
            mCircleView.setAccessible(true);
            View progress = (View) mCircleView.get(this);
            progress.setVisibility(VISIBLE);

            Method setRefreshing = SwipeRefreshLayout.class.getDeclaredMethod("setRefreshing", boolean.class, boolean.class);
            setRefreshing.setAccessible(true);
            setRefreshing.invoke(this, true, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

然后XML改为

代码语言:javascript
复制
<cn.psvmc.swiperefreshlayout.AutoSwipeRefreshLayout
    android:id="@+id/id_swipe_ly"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:id="@+id/file_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@color/zj_qianhuise"
        android:dividerHeight="1px">
    </ListView>
</cn.psvmc.swiperefreshlayout.AutoSwipeRefreshLayout>

对应的java代码中的SwipeRefreshLayout 都换成 AutoSwipeRefreshLayout

页面加载后调用mSwipeLayout.autoRefresh()就可以了

但是立即执行mSwipeLayout.autoRefresh()在效果上不是很流畅 所以我用了消息机制 延迟发送消息 就好了

代码语言:javascript
复制
//定义消息常量
interface ZJHandlerStatus {
    int endrefresh = 0;
    int endDialog = 1;
    int reloadData = 1;
    int autoRefresh = 2;
}

//消息处理
Handler myHandler = new Handler() {
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case ZJHandlerStatus.endrefresh:
                mSwipeLayout.setRefreshing(false);
                break;
            case ZJHandlerStatus.reloadData:
                fileAdapter.notifyDataSetChanged();
                break;
            case ZJHandlerStatus.autoRefresh:
                mSwipeLayout.autoRefresh();
                break;
        }
        super.handleMessage(msg);
    }
};

//发送消息
Message message = new Message();
message.what = ZJHandlerStatus.autoRefresh;
myHandler.sendMessageAtTime(message, SystemClock.uptimeMillis() + 600);
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-06-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 概要
  • 官方原版的用法
  • 进入页面立即刷新
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档