下拉刷新控件SwipeRefreshLayout用法简介

来源:互联网 发布:鳄鱼毒品知乎 编辑:程序博客网 时间:2024/05/01 11:47
以前需要下拉刷新效果时,一般都自定义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() {@Overridepublic void onRefresh() {// TODO Auto-generated method stubnew 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
原创粉丝点击