官方链接: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 implements SwipeRefreshLayout.OnRefreshListener { private SwipeRefreshLayout swipeLayout; private ListView listView; private ListViewAdapter adapter; private ArrayList<SoftwareClassificationInfo> list; private boolean isRefresh = false;//是否刷新中 @Override protected 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>color4 swipeLayout.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,那么我们还有什么理由不跟进时代学习新的设计呢,快清明了
祝大家节日愉快,最好不加班哦!