转自:http://blog.csdn.net/zouzhigang96/article/details/50476402

版权声明:本文为博主原创文章,未经博主允许不得转载。

前言: 
如今谷歌推出了更官方的下拉刷新控件, 这无疑是对安卓开发人员来说是个好消息,很方便的使用这个SwipeRefreshLayout控件实现下拉刷新功能。Android4.0以下的版本需要用到 Android-support-v4.jar包才能用到 
android-support-v4.jar 包下载地址:http://download.csdn.net/detail/h7870181/7784247

注:记得一定要把Support library的版本升级到19.1或最新

谷歌SwipeRefreshLayout官方API

那下面就来做一个简单的DEMO,来看看SwipeRefreshLayout这个控件到底如何

注:楼主使用的是Android Studio 作为IDE。

1,首先来看看SwipeRefreshLayout的布局文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swiperereshlayout"
android:layout_marginTop="55dp"
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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

2,在Activity中是如何使用SwipeRefreshLayout的

public class MainActivity extends AppCompatActivity {
private SwipeRefreshLayout swiperereshlayout ;
private ListView listview ;
private ArrayAdapter<String> adapter;
private List<String> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
}); initView(); }
private void initView (){ swiperereshlayout = (SwipeRefreshLayout) findViewById(R.id.swiperereshlayout);
listview = (ListView) findViewById(R.id.listview);
data = new ArrayList<String>();
for (int i = 0; i < 20; i++) {
data.add("当前的item为 " + i);
}
adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, data);
listview.setAdapter(adapter); swiperereshlayout.setColorSchemeResources(android.R.color.holo_blue_bright,
android.R.color.holo_green_light, android.R.color.holo_orange_light);
//给swipeRefreshLayout绑定刷新监听
swiperereshlayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
//设置2秒的时间来执行以下事件
new Handler().postDelayed(new Runnable() {
public void run() {
data.add(0, "刷新后新增的item");
adapter.notifyDataSetChanged();
swiperereshlayout.setRefreshing(false);
}
}, 2000);
}
}); } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60

注:主要代码就是在initView这个方法里面,里面简单的实现的如何下拉加载新数据,只是做了一个简单的演示 ,具体深入的开发,大家也可以尝试一下自定义。

主要方法

(1)setOnRefreshListener(OnRefreshListener): 为布局添加一个Listener

(2)setRefreshing(boolean): 显示或隐藏刷新进度条

(3)isRefreshing(): 检查是否处于刷新状态

(4)setColorSchemeResources(): 设置进度条的颜色主题,最多能设置四种

目前 swiperereshlayout.setColorScheme()->已弃用 
可以使用swiperereshlayout.setColorSchemeResources()来设置颜色。

总结:

谷歌官方目前正在不断完善自己的SDK,推出越来越多的组件,其目的是让开发更简单,设计上更统一,这可能是Google未来的方向,不管怎样,这对开发者来说无疑是非常好的消息。

源码下载

Android——谷歌官方下拉刷新控件SwipeRefreshLayout(转)的更多相关文章

  1. Android之官方下拉刷新控件SwipeRefreshLayout

    SwipeRefreshLayout 是在V4包里面,首先要先导入V4包,最新的V4包里面才有这控件 首先是布局 <android.support.v4.widget.SwipeRefreshL ...

  2. android官方下拉刷新控件SwipeRefreshLayout的使用

    可能开发安卓的人大多数都用过很多下拉刷新的开源组件,但是今天用了官方v4支持包的SwipeRefreshLayout觉得效果也蛮不错的,特拿出来分享. 简介:SwipeRefreshLayout组件只 ...

  3. 官方下拉刷新控件SwipeRefreshLayout的使用

    今天看博客,发现有了这个下拉刷新的控件,效果看上去还蛮好的,于是我也想研究的是使用一下,写个demo.其实使用很简单的,但就是为了能使用这个新组建我下了好久的更新,后来还是直接去官网下载最新的ADT得 ...

  4. 【转】Android官方下拉刷新控件 SwipeRefreshLayout

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

  5. 第一个开源控件:Google 官方下拉刷新控件 SwipeRefreshLayout 强化版,支持上拉刷新

    最近比较闲,所以趁着这时间撸了个SwipeRefreshLayout的加强版,Github地址. 原版只支持下拉刷新,强化之后支持上拉刷新和一进入页面就加载刷新,整个控件的加载动画是一致的,毫无违和感 ...

  6. Android SwipeRefreshLayout 官方下拉刷新控件介绍

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24521483 下面App基本都有下拉刷新的功能,以前基本都使用XListView ...

  7. android SwipeRefreshLayout google官方下拉刷新控件

    下拉刷新功能之前一直使用的是XlistView很方便我前面的博客有介绍 SwipeRefreshLayout是google官方推出的下拉刷新控件使用方法也比较简单 今天就来使用下SwipeRefres ...

  8. Google SwipeRefreshLayout(Goolge官方下拉刷新控件)尝鲜

    前天Google官方终于出了Android刷新控件——SwipeRefreshLayout. 使用前先需要将android.support.v4.jar升级到19.1.升级后,可能会出现SDK版本与A ...

  9. google官方提供的下拉刷新控件SwipeRefreshLayout

    摘自:http://www.stormzhang.com/android/2014/03/29/android-swiperefreshlayout/ SwipeRefreshLayout Swipe ...

随机推荐

  1. 作业(二)—python实现wc命令

    Gitee地址:https://gitee.com/c1e4r/word-count(为什么老师不让我们用github) 0x00 前言 好久没发博客了,感觉自己的学习是有点偷懒了.这篇博客也是应专业 ...

  2. dotNET面试(一)

    1.列举ASP.NET 页面之间传递值的几种方式. 1).使用QueryString, 如....?id=1; response. Redirect().... 2).使用Session变量 3).使 ...

  3. 转载:对比Angular/jQueryUI/Extjs:没有一个框架是万能的

    Angular不能做什么?对比Angular/jQueryUI/Extjs 框架就好比兵器,你得明白你手里拿的是屠龙刀还是倚天剑,刀法主要是砍,剑法主要是刺.对于那些职业喷子和脑残粉,小僧送你们两个字 ...

  4. 在eclipse中添加svn插件

    1.点击菜单栏中的help选项,然后选择Install New Software,然后点击ADD,输入: name:subclipse     url:http://subclipse.tigris. ...

  5. NOIP2016D1T3 换教室 (概率DP)

    NOIP2016D1T3 换教室 题目大意:有n个时间段,每个时间段i有两个教室a[i],b[i]可以上课,如果不申请换教室就在教室a[i]上课,如果换教室就在b[i]上课.你最多只能换m次教室.教室 ...

  6. UOJ197 线性规划

    传送门 由于这道题标程GG了所以必不可能AC嘛2333 单纯形法是一个很玄学的东西qwq 就是 非标准型 -> 标准型 -> 规范型 -> 松弛型 一个玄学操作——转轴操作(priv ...

  7. 关于Linux_系统资源监控_dmesg_free_uptime_uname

    (系统资源查看命令-dmesg[查看系统内核资源信息])->判断服务器的硬件状态 Comment:dmesg | grep CPU,指定查看cpu资源信息 (系统资源查看命令-free[查看内存 ...

  8. spring需要表

    DROP TABLE IF EXISTS `user`;CREATE TABLE `user` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `username` ...

  9. CDN技术之--全局负载均衡(GSLB)

    负载均衡就是智能调度全局负载均衡(GSLB)的负载均衡主要是在多个节点之间进行均衡,其结果可能直接终结负载均衡过程,也可能将用户访问交付下一层次的(区域或本地)负载均衡系统进行处理.GSLB最通用的是 ...

  10. ArcGIS Runtime SDK for .NET (Quartz Beta)之连接ArcGIS Portal

    1. 介绍 ArcGIS Portal作为ArcGIS平台的中枢,在ArcGIS体系中起着至关重要的地位.在ArcGIS Runtime的新架构Quartz中添加了连接ArcGIS Portal(或A ...