下拉刷新控件SwipeRefreshLayout用法简介

以前需要下拉刷新效果时,一般都自定义ListView,添加头部视图,实现下拉效果。SwipeRefreshLayout控件则省去的自定义头部视图的繁琐,其用法非常简单。先看一下界面布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <ListView
            android:id="@+id/listview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>
    </android.support.v4.widget.SwipeRefreshLayout>

</LinearLayout>

通过布局文件可知,使用SwipeRefreshLayout控件需要导入v4包。在布局文件中,只要将列表控件直接包含在SwipeRefreshLayout中即可。

在Java文件中,主要程序如下:

swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
		swipeRefreshLayout.setColorScheme(android.R.color.white,
				android.R.color.holo_green_light,
				android.R.color.holo_orange_light,
				android.R.color.holo_red_light);

		swipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				// TODO Auto-generated method stub
				new Handler().postDelayed(new Runnable() {
					public void run() {
						swipeRefreshLayout.setRefreshing(false);
						arrayList.add(0, "---->" + ++f);
						adapter.notifyDataSetChanged();
					}
				}, 2000);
			}
		});

这里主要需要注意两个地方:一是swipeRefreshLayout.setColorScheme(),该方法是设定刷新效果的颜色,最多同时支持4种颜色。二是使用swipeRefreshLayout控件需为其注册OnRefreshListener()监听,监听刷新操作,并重写onRefresh()方法。在onRefresh()方法中,可以设定刷新的具体操作,例如添加新数据、刷新效果持续时间等等。

以上就是SwipeRefreshLayout控件的用法,很简单。因程序中的其他代码非常简单,就不再贴出来。大家有可以下载Demo看看。链接在最后给出。


Demo:SwipeRefreshLayout




 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值