首页 > 代码库 > 【转】google推出的SwipeRefreshLayout下拉刷新用法

【转】google推出的SwipeRefreshLayout下拉刷新用法

SwipeRefreshLayout是Google在support v4 19.1版本的library更新的一个下拉刷新组件,实现刷新效果更方便。

使用如下:

1.先下载android-support-v4.jar最新版本,之前的版本是没有SwipeRefreshLayout下拉刷新控件的,如果已经更新,此步骤可省略。

2.在xml文件中引用android.support.v4.widget.SwipeRefreshLayout控件,在里面可以放置任何一个控件,例如ListView,gridview等。

 1     <android.support.v4.widget.SwipeRefreshLayout   2         android:id="@+id/swipe_refresh"   3         android:layout_width="match_parent"   4         android:layout_height="match_parent" >   5        6         <ListView   7             android:id="@+id/listview"   8             android:layout_width="match_parent"   9             android:layout_height="match_parent" >  10         </ListView>  11     </android.support.v4.widget.SwipeRefreshLayout>  

 

3.在java文件中使用。

 1 /**  2 * 主页   3  * @author w.w   4  */   5 public class MainActivity extends Activity implements SwipeRefreshLayout.OnRefreshListener {   6    7     /**   8      * 给ListView添加下拉刷新   9      */  10     private SwipeRefreshLayout swipeLayout;  11       12     /**  13      * ListView  14      */  15     private ListView listView;  16       17     /**  18      * ListView适配器  19      */  20     private ListViewAdapter adapter;  21       22     private List<ItemInfo> infoList;  23       24     @Override  25     protected void onCreate(Bundle savedInstanceState) {  26         super.onCreate(savedInstanceState);  27         setContentView(R.layout.activity_main);  28   29         swipeLayout = (SwipeRefreshLayout) this.findViewById(R.id.swipe_refresh);  30         swipeLayout.setOnRefreshListener(this);  31           32         // 顶部刷新的样式  33         swipeLayout.setColorScheme(android.R.color.holo_red_light, android.R.color.holo_green_light,  34                 android.R.color.holo_blue_bright, android.R.color.holo_orange_light);  35   36         infoList = new ArrayList<ItemInfo>();  37         ItemInfo info = new ItemInfo();  38         info.setName("coin");  39         infoList.add(info);  40         listView = (ListView) this.findViewById(R.id.listview);  41         adapter = new ListViewAdapter(this, infoList);  42         listView.setAdapter(adapter);  43     }  44   45     public void onRefresh() {  46         new Handler().postDelayed(new Runnable() {  47             public void run() {  48                 swipeLayout.setRefreshing(false);  49                 ItemInfo info = new ItemInfo();  50                 info.setName("coin-refresh");  51                 infoList.add(info);  52                 adapter.notifyDataSetChanged();  53             }  54         }, 500);  55     }  56 }  

demo下载地址:http://download.csdn.net/detail/xue_wei_love/7135315