zoukankan      html  css  js  c++  java
  • Google官方下拉刷新组件---SwipeRefreshLayout

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

    SwipeRefreshLayout

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

    主要方法

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

    xml布局文件

    布局文件很简单,只需要在最外层加上SwipeRefreshLayout,然后他的child是可滚动的view即可,如ScrollView或者ListView。如:
    1. <android.support.v4.widget.SwipeRefreshLayout
    2.     xmlns:android="http://schemas.android.com/apk/res/android"
    3.     android:id="@+id/swipe_container"
    4.     android:layout_width="match_parent"
    5.     android:layout_height="match_parent">
    6.     <ScrollView
    7.         android:layout_width="match_parent"
    8.         android:layout_height="match_parent">
    9.         <TextView
    10.             android:text="@string/hello_world"
    11.             android:layout_width="match_parent"
    12.             android:layout_height="wrap_content"
    13.             android:layout_marginTop="16dp"
    14.             android:gravity="center"/>
    15.     </ScrollView>
    16. </android.support.v4.widget.SwipeRefreshLayout>
    复制代码

    Activity代码

    1. protected void onCreate(Bundle savedInstanceState) {
    2.     super.onCreate(savedInstanceState);
    3.     setContentView(R.layout.activity_main);
    4.     swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
    5.     swipeLayout.setOnRefreshListener(this);
    6.     swipeLayout.setColorScheme(android.R.color.holo_blue_bright, 
    7.             android.R.color.holo_green_light, 
    8.             android.R.color.holo_orange_light, 
    9.             android.R.color.holo_red_light);
    10. }
    11. public void onRefresh() {
    12.     new Handler().postDelayed(new Runnable() {
    13.         [url=home.php?mod=space&uid=389554]@Override[/url] public void run() {
    14.             swipeLayout.setRefreshing(false);
    15.         }
    16.     }, 5000);
    17. }
    复制代码
    上面的代码很简单,只需要给SwipeRefreshLayout添加一个listener,值得说明的是setColorScheme方法是设置刷新进度条的颜色,最多只能设置4种循环显示,默认第一个是随用户手势加载的颜色进度条。

    源码

    写了的小demo在github上,地址在:SwipeRefreshLayoutDemo

    总结

    google在不断完善自己的sdk,推出越来越多的组件,其目的是让开发更简单,设计上更统一,这可能是google未来的方向,不管怎样,这对开发者来说无疑是非常好的消息。
  • 相关阅读:
    iSCSI又称为IPSAN
    文档类型定义DTD
    HDU 2971 Tower
    HDU 1588 Gauss Fibonacci
    URAL 1005 Stone Pile
    URAL 1003 Parity
    URAL 1002 Phone Numbers
    URAL 1007 Code Words
    HDU 3306 Another kind of Fibonacci
    FZU 1683 纪念SlingShot
  • 原文地址:https://www.cnblogs.com/xiaochao1234/p/3644998.html
Copyright © 2011-2022 走看看