首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android开发之SwipeRefreshLayout实现下拉刷新

Android开发之SwipeRefreshLayout实现下拉刷新

作者头像
YungFan
发布2018-04-24 15:11:39
1.1K0
发布2018-04-24 15:11:39
举报
文章被收录于专栏:学海无涯学海无涯
简介

SwipeRefreshLayout是Google官方推出的一款下拉刷新组件,位于v4兼容包下,android.support.v4.widget.SwipeRefreshLayout,Support Library 必须19.1以上。使用起来很简单,只要在需要刷新的控件最外层加上SwipeRefreshLayout,其child必须是可滚动的view,如ScrollView、GridView或者ListView,这里就测试最常用的ListView。

布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

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

        <ListView
            android:id="@+id/listview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"></ListView>

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

</RelativeLayout>
Activity
public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {

    private SwipeRefreshLayout mSwipeLayout;
    private ListView mListView;
    private ArrayAdapter<String> mAdapter;
    private ArrayList<String> data;
    private boolean isRefresh = false;//是否刷新中

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();


        //设置SwipeRefreshLayout
        mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipeLayout);

        //设置进度条的颜色主题,最多能设置四种 加载颜色是循环播放的,只要没有完成刷新就会一直循环,holo_blue_bright>holo_green_light>holo_orange_light>holo_red_light
       // mSwipeLayout.setColorScheme(android.R.color.holo_blue_bright,
       //         android.R.color.holo_green_light,
       //         android.R.color.holo_orange_light,
       //         android.R.color.holo_red_light);

        //上面的方法已经废弃
        mSwipeLayout.setColorSchemeColors(Color.BLUE,
                Color.GREEN,
                Color.YELLOW,
                Color.RED);


        // 设置手指在屏幕下拉多少距离会触发下拉刷新
        mSwipeLayout.setDistanceToTriggerSync(300);
        // 设定下拉圆圈的背景
        mSwipeLayout.setProgressBackgroundColorSchemeColor(Color.WHITE);
        // 设置圆圈的大小
        mSwipeLayout.setSize(SwipeRefreshLayout.LARGE); 

        //设置下拉刷新的监听
        mSwipeLayout.setOnRefreshListener(this);
    }

    /*
     * 初始化  设置ListView
     */
    private void init() {
        mListView = (ListView) findViewById(R.id.listview);

        data = new ArrayList<String>();

        for (int i = 1; i < 10; i++) {
            data.add("这是第" + i + "个数据");
        }

        //初始化adapter
        mAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, data);

        mListView.setAdapter(mAdapter);
    }

    /*
     * 监听器SwipeRefreshLayout.OnRefreshListener中的方法,当下拉刷新后触发
     */
    public void onRefresh() {
        //检查是否处于刷新状态
        if (!isRefresh) {
            isRefresh = true;
            //模拟加载网络数据,这里设置4秒,正好能看到4色进度条
            new Handler().postDelayed(new Runnable() {
                public void run() {

                    //显示或隐藏刷新进度条
                    mSwipeLayout.setRefreshing(false);
                    //修改adapter的数据
                    data.add("这是新添加的数据");
                    mAdapter.notifyDataSetChanged();
                    isRefresh = false;
                }
            }, 4000);
        }
    }
}
测试

SwipeRefreshLayout.gif

问题

细心的读者肯定发现,代码中 setColorSchemeColors 设置颜色时候并未按如下设置

mSwipeLayout.setColorSchemeColors(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);

那是因为,经过测试,如果按照如上设置颜色,进度条不会有颜色循环的效果,不知道为何?难道是bug吗?知道的告知,感谢~~~

代码地址

https://github.com/yungfan/SwipeRefreshLayout

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016.04.23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 简介
  • 布局
  • Activity
  • 测试
  • 问题
  • 代码地址
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档