Google官方下拉刷新组件---SwipeRefreshLayout

SwipeRefreshLayout字面意思就是下拉刷新的布局,继承自ViewGroup,在support v4兼容包下(android.support.v4.widget.SwipeRefreshLayout),但必须把你的support library的版本升级到19.1。 提到下拉刷新大家一定对ActionBarPullToRefresh比较熟悉,而如今google推出了更官方的下拉刷新组件,这无疑是对开发者来说比较好的消息。利用这个组件可以很方便的实现Google Now的刷新效果,见下图:


xml布局文件

This layout should be made the parent of the view that will be refreshed as a result of the gesture and can only support one direct child.

只要在需要刷新的控件最外层加上SwipeRefreshLayout,然后他的child首先是可滚动的view,如ScrollView或者ListView。
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:id="@+id/container"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     tools:ignore="MergeRootFrame" >  
  7.   
  8.     <android.support.v4.widget.SwipeRefreshLayout  
  9.         android:id="@+id/swipe_container"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="match_parent" >  
  12.   
  13.         <ListView  
  14.             android:id="@+id/list"  
  15.             android:layout_width="match_parent"  
  16.             android:layout_height="match_parent" >  
  17.         </ListView>  
  18.   
  19.     </android.support.v4.widget.SwipeRefreshLayout>  
  20.   
  21. </FrameLayout>  

Activity代码:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.jabony.swiperefreshlayou;  
  2.   
  3. import java.util.ArrayList;  
  4. import android.app.Activity;  
  5. import android.os.Bundle;  
  6. import android.os.Handler;  
  7. import android.support.v4.widget.SwipeRefreshLayout;  
  8. import android.widget.ListView;  
  9. import com.jabony.swiperefreshlayout.R;  
  10.   
  11. public class SwipRefreshLayoutActivity extends Activity implements  
  12.         SwipeRefreshLayout.OnRefreshListener {  
  13.     private SwipeRefreshLayout swipeLayout;  
  14.     private ListView listView;  
  15.     private ListViewAdapter adapter;  
  16.     private ArrayList<SoftwareClassificationInfo> list; private boolean isRefresh = false;//是否刷新中  
  17.   
  18.     @Override  
  19.     protected void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.swipe_refresh_layout);  
  22.         swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);  
  23.         swipeLayout.setOnRefreshListener(this);  
  24.         //加载颜色是循环播放的,只要没有完成刷新就会一直循环,color1>color2>color3>color4  
  25.         swipeLayout.setColorScheme(android.R.color.white,  
  26.                 android.R.color.holo_green_light,  
  27.                 android.R.color.holo_orange_light, android.R.color.holo_red_light);  
  28.   
  29.         list = new ArrayList<SoftwareClassificationInfo>();  
  30.         list.add(new SoftwareClassificationInfo(1"asdas"));  
  31.         listView = (ListView) findViewById(R.id.list);  
  32.         adapter = new ListViewAdapter(this, list);  
  33.         listView.setAdapter(adapter);  
  34.     }  
  35.   
  36.     public void onRefresh() { if(!isRefresh){ isRefresh = true;  
  37.         new Handler().postDelayed(new Runnable() {  
  38.             public void run() {  
  39.                 swipeLayout.setRefreshing(false);  
  40.                 list.add(new SoftwareClassificationInfo(2"ass"));  
  41.                 adapter.notifyDataSetChanged(); isRefresh= false;  
  42.             }  
  43.         }, 3000); }  
  44.     }  
  45. }  

主要方法:

  • setOnRefreshListener(OnRefreshListener): 为布局添加一个Listener
  • setRefreshing(boolean): 显示或隐藏刷新进度条
  • isRefreshing(): 检查是否处于刷新状态
  • setColorScheme(): 设置进度条的颜色主题,最多能设置四种

注意:
    该类的父类为ViewGroup
    下来刷新的空间,仅仅只能包含一个控件子类,所以在设计XML的时候,需要保证设计的正确性;

总结:每次当有朋友对我说有新的事物时,都非常开心,因为这又是一个进步的机会,希望大家与我一同进步。Google也在不断完善自己的SDK,那么我们还有什么理由不跟进时代学习新的设计呢,快清明了 祝大家节日愉快,最好不加班哦!

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值