Android自带的下拉刷新组件SwipeRefreshLayout

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

 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 print ? 在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代码:

package com.wyl.test.example;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity2 extends Activity implements
		SwipeRefreshLayout.OnRefreshListener {
	private SwipeRefreshLayout swipeLayout;
	private ListView listView;
	private ArrayList<String> list;
	private ArrayAdapter adapter;
	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<String>();
		list.add("测试数据1");
		list.add("测试数据2");
		list.add("测试数据3");
		list.add("测试数据4");
		listView = (ListView) findViewById(R.id.list);
		adapter = new ArrayAdapter(this,
				android.R.layout.simple_expandable_list_item_1, list);
		listView = (ListView) findViewById(R.id.list);

		listView.setAdapter(adapter);
	}

	@Override
	public void onRefresh() {
		if (!isRefresh) {
			isRefresh = true;
			new Handler().postDelayed(new Runnable() {
				public void run() {
					swipeLayout.setRefreshing(false);
					list.add("测试数据5");
					adapter.notifyDataSetChanged();
					isRefresh = false;
				}
			}, 3000);
		}
	}

}


主要方法:

  • setOnRefreshListener(OnRefreshListener): 为布局添加一个Listener
  • setRefreshing(boolean): 显示或隐藏刷新进度条
  • isRefreshing(): 检查是否处于刷新状态
  • setColorScheme(): 设置进度条的颜色主题,最多能设置四种
  • 在上面的onRefresh()函数中实现获取数据功能以及更新数据,当更新完数据后,调用swipeRefreshLayout.setRefreshing(false);来关闭刷新。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值