Google自己的下拉刷新组件SwipeRefreshLayout

àì夳堔傛蜴生んèń 2022-06-18 09:24 315阅读 0赞

原著:http://stormzhang.com/android/2014/03/29/android-swiperefreshlayout/

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. @Override public void run() {
  14. swipeLayout.setRefreshing(false);
  15. }
  16. }, 5000);
  17. }

上面的代码很简单,只需要给SwipeRefreshLayout添加一个listener,值得说明的是setColorScheme方法是设置刷新进度条的颜色,最多只能设置4种循环显示,默认第一个是随用户手势加载的颜色进度条。

发表评论

表情:
评论列表 (有 0 条评论,315人围观)

还没有评论,来说两句吧...

相关阅读