现在市面上新闻类的App基本上都有下拉刷新,算是一个标配吧,网上关于下拉刷新的博客也有很多,实现方式可以使用开源的PullToRefresh,自定义ListView,或者可以直接使用LineLayOut直接搞定的。不过Google在今年在support v4 19.1版本的library推出了SwipeRefreshLayout,字面上的意思就是下拉刷新,继承自ViewGroup,而如今google推出了更官方的下拉刷新组件,对于开发者而言无疑是一个好事情,比较少的代码实现需要的功能。

基本布局

先来看下简单的布局,在最外层加上SwipeRefreshLayout,但是子的View需要时可滚动的(ScrollView或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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefreshLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"> </ListView>
</android.support.v4.widget.SwipeRefreshLayout> </RelativeLayout>

布局效果如下:

Demo实现

MainActivity中onCreate中的初始化一下SwipeLayOut,需要注意的方法是setColorScheme(), 设置进度条的颜色主题,最多能设置四种;

  myListView = (ListView) findViewById(R.id.listView);
mySwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout); mySwipeRefreshLayout.setOnRefreshListener(this);
mySwipeRefreshLayout.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);
listAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,listIDE);
myListView.setAdapter(listAdapter);

MainActivity中需要实现一下 SwipeRefreshLayout.OnRefreshListener

  @Override
public void onRefresh() { refreshHandler.sendEmptyMessageDelayed(REFRESH_STATUS, 1500);
}

 最后初始化一下数据setRefreshing(boolean):,显示或隐藏刷新进度条

  private static final int REFRESH_STATUS =0;
private ListView myListView;
private SwipeRefreshLayout mySwipeRefreshLayout;
private ArrayAdapter<String> listAdapter;
private List<String> listIDE = new ArrayList<String>(Arrays.asList("Visual Studio", "Android Studio", "Eclipse", "Xcode"));
private Handler refreshHandler = new Handler()
{
public void handleMessage(android.os.Message msg)
{
switch (msg.what)
{
case REFRESH_STATUS:
listIDE.removeAll(listIDE);
listIDE.addAll(Arrays.asList("C#", "Java", "C++","Object-C"));
listAdapter.notifyDataSetChanged();
mySwipeRefreshLayout.setRefreshing(false);
break; }
};
};

 最后的效果如下:

参考资料:https://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html

Android下拉刷新-SwipeRefreshLayout的更多相关文章

  1. Android下拉刷新-SwipeRefreshLayout,RecyclerView完全解析之下拉刷新与上拉加载SwipeRefreshLayout)

    SwipeRefrshLayout是Google官方更新的一个Widget,可以实现下拉刷新的效果.该控件集成自ViewGroup在support-v4兼容包下,不过我们需要升级supportlibr ...

  2. Android下拉刷新SwipeRefreshLayout简单用法

    之前一直都想用下拉刷新,感觉上是庞大的工程,所以搁置了.现在学习了一下其实真的超级简单. 看了<第一行代码>以及 https://www.jianshu.com/p/3c402a9e4b7 ...

  3. Android Material Design控件使用(四)——下拉刷新 SwipeRefreshLayout

    使用下拉刷新SwipeRefreshLayout 说明 SwipeRefreshLayout是Android官方的一个下拉刷新控件,一般我们使用此布局和一个RecyclerView嵌套使用 使用 xm ...

  4. Android下拉刷新效果实现

    本文主要包括以下内容 自定义实现pulltorefreshView 使用google官方SwipeRefreshLayout 下拉刷新大致原理 判断当前是否在最上面而且是向下滑的,如果是的话,则加载数 ...

  5. Android 下拉刷新

    以前旧版用的是开源的PullToRefresh第三方库,该库现在已经不再维护了: chrisbanes/Android-PullToRefreshhttps://github.com/chrisban ...

  6. 【原创】窥视懒人的秘密---android下拉刷新开启手势的新纪元

    小飒的成长史原创作品:窥视懒人的秘密---android下拉刷新开启手势的新纪元转载请注明出处 **************************************************** ...

  7. Android 下拉刷新上拉载入 多种应用场景 超级大放送(上)

    转载请标明原文地址:http://blog.csdn.net/yalinfendou/article/details/47707017 关于Android下拉刷新上拉载入,网上的Demo太多太多了,这 ...

  8. Android listview下拉刷新 SwipeRefreshLayout

    今天在Google+上看到了SwipeRefreshLayout这个名词,遂搜索了下,发现竟然是刚刚google更新sdk新增加的一个widget,于是赶紧抢先体验学习下. SwipeRefreshL ...

  9. android开发(49) Android 下拉刷新的实现。使用 SwipeRefreshLayout 代替 pull-to-refesh

    概述 谷歌官方推出了SwipeRefreshLayout 来实现下拉刷新的效果.对比以前我们常用的 pull-to-refesh ,这个方案显得更加的简单方便. 关联项目引用(管理依赖) 在你的 应用 ...

随机推荐

  1. "characterEncoding" must end with the ';' delimiter.

    17/04/20 17:27:10 FATAL conf.Configuration: error parsing conf file:/usr/local/apache-hive-1.2.2-bin ...

  2. 让ORM支持多结果集

    在现有的ORM框架中,都支持查询单个结果集.比如查询用户表,传入语句,返回一个用户对象的集合.虽然一次性查询多个结果集的情况不多,而且也可以通过多次查询得到,但是从写框架的角度来说,我们并不清楚客户( ...

  3. 基于TensorFlow Serving的深度学习在线预估

    一.前言 随着深度学习在图像.语言.广告点击率预估等各个领域不断发展,很多团队开始探索深度学习技术在业务层面的实践与应用.而在广告CTR预估方面,新模型也是层出不穷: Wide and Deep[1] ...

  4. hdu 2275 Kiki & Little Kiki 1 水题

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2275 这个题比较简单,所以就没有测试样例提供给大家,基本把题目的样例过了就可以了 题目大意 给你一串操作, ...

  5. [代码审计]eml企业通讯录管理系统v5.0 存在sql注入

    0x00 前言 上周五的时候想练练手,随便找了个系统下载下来看看. 然后发现还有VIP版本,但是VIP要钱,看了一下演示站,貌似也没有什么改变,多了个导入功能?没细看. 搜了一下发现这个系统,压根就没 ...

  6. Cpp下匿名对象探究

    先来看看一段代码: #include "iostream" using namespace std; class ABCD { public: ABCD(int a, int b, ...

  7. java 不通过第三个字符串,实现一个字符串倒序

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha String s="abcde"; String  s2 = new ...

  8. 20172333 2017-2018-2 《Java程序设计》第6周学习总结

    20172333 2017-2018-2 <Java程序设计>第6周学习总结 教材学习内容 1.数组的基本用法,如数组的定义:int[该数组类型] name = new int[X]X为数 ...

  9. Python文件类型

    Python的文件类型分为三种:源代码.字节代码.优化代码. 1. 源代码    Python源代码文件,即py脚本文件,由 python.exe 解释,可在控制台下运行.pyw脚本文件是图形用户接口 ...

  10. Codeforces Round #293 (Div. 2) A. Vitaly and Strings

    A. Vitaly and Strings time limit per test 1 second memory limit per test 256 megabytes input standar ...