官方链接:https://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html#inhfields


情景:前天小海告诉我说谷歌除了自己的刷新控件,没错就是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。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/container"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:ignore="MergeRootFrame" >    <android.support.v4.widget.SwipeRefreshLayout        android:id="@+id/swipe_container"        android:layout_width="match_parent"        android:layout_height="match_parent" >        <ListView            android:id="@+id/list"            android:layout_width="match_parent"            android:layout_height="match_parent" >        </ListView>    </android.support.v4.widget.SwipeRefreshLayout></FrameLayout>

Activity代码:

package com.jabony.swiperefreshlayou;import java.util.ArrayList;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.support.v4.widget.SwipeRefreshLayout;import android.widget.ListView;import com.jabony.swiperefreshlayout.R;public class SwipRefreshLayoutActivity extends Activity implementsSwipeRefreshLayout.OnRefreshListener {private SwipeRefreshLayout swipeLayout;private ListView listView;private ListViewAdapter adapter;private ArrayList<SoftwareClassificationInfo> list; private boolean isRefresh = false;//是否刷新中@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.swipe_refresh_layout);swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);swipeLayout.setOnRefreshListener(this);//加载颜色是循环播放的,只要没有完成刷新就会一直循环,color1>color2>color3>color4swipeLayout.setColorScheme(android.R.color.white,android.R.color.holo_green_light,android.R.color.holo_orange_light, android.R.color.holo_red_light);list = new ArrayList<SoftwareClassificationInfo>();list.add(new SoftwareClassificationInfo(1, "asdas"));listView = (ListView) findViewById(R.id.list);adapter = new ListViewAdapter(this, list);listView.setAdapter(adapter);}public void onRefresh() { if(!isRefresh){ isRefresh = true;new Handler().postDelayed(new Runnable() {public void run() {swipeLayout.setRefreshing(false);list.add(new SoftwareClassificationInfo(2, "ass"));adapter.notifyDataSetChanged(); isRefresh= false;}}, 3000); }}}

主要方法:

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

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

附件:稍后附上下载地址(免积分

更多相关文章

  1. 说说 Android(安卓)的 Material Design 设计(四)——卡片式布局
  2. Android(安卓)ViewPager引导页
  3. Android(安卓)View.MeasureSpec
  4. Android(安卓)RecyclerView控件
  5. 个人经验 - Android的RelativeLayout布局的layout_height属性设
  6. Android(安卓)App启动图启动界面(Splash)的简单实现
  7. android常见手动和自动轮播图效果
  8. LayoutInflater那些事儿
  9. Android(安卓)GridView 方格中图标与文字如何同时存在

随机推荐

  1. Android点名系统
  2. Android上的ril库
  3. Android的真机调试,INSTALL_FAILED_NO_MAT
  4. android 移送控件
  5. android GMS认证之testGoogleDuoPreloade
  6. Android中登录布局展示Activity
  7. what is already installed?
  8. 【Android】广播大全(二)
  9. 关于Android中是否可以使用全局变量的问
  10. android注解框架ButterKnife详细使用文档