Android实战简易教程<十九>(SwipeRefreshLayout下拉刷新使用实例)

来源:互联网 发布:淘宝如何改规格分类 编辑:程序博客网 时间:2024/06/11 06:52

我们来看SwipeRefreshLayout的具体用法,顾名思义此组件就是一个布局,只不过要注意的是此布局内只能有一个直接子View。其实通过文档我们可以知道SwipeRefreshLayout只不过是继承了ViewGroup。

 查看文档,我们可以知道,在SwipRefreshLayout中存在一个接口,通过此接口我们可以监听滑动手势,其实使用此组件最重要的步骤就是实现此接口的onRefresh方法,在此方法中实现数据的更新操作。如下:


接口中的方法:


 除了OnRefreshListener接口外,SwipRefreshLayout中还有一些其他重要的方法,具体如下:

 

         1、setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener listener):设置手势滑动监听器。

 

         2、setProgressBackgroundColor(int colorRes):设置进度圈的背景色。

 

         3、setColorSchemeResources(int… colorResIds):设置进度动画的颜色。

 

         4、setRefreshing(Boolean refreshing):设置组件的刷新状态。

 

         5、setSize(int size):设置进度圈的大小,只有两个值:DEFAULT、LARGE。

下面我们通过一个实例来看一下具体怎么用

1.main.xml:

[html] view plaincopy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent" >  
  4.   
  5.     <android.support.v4.widget.SwipeRefreshLayout  
  6.         android:id="@+id/swipe_refresh"  
  7.         android:layout_width="match_parent"  
  8.         android:layout_height="match_parent" >  
  9.   
  10.         <ListView  
  11.             android:id="@+id/listview"  
  12.             android:layout_width="match_parent"  
  13.             android:layout_height="match_parent" >  
  14.         </ListView>  
  15.     </android.support.v4.widget.SwipeRefreshLayout>  
  16.   
  17. </LinearLayout>  
2.list_item.xml:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="wrap_content" >  
  5.   
  6.     <TextView  
  7.         android:id="@+id/item_name"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_margin="15dp"  
  11.         android:gravity="center"  
  12.         android:singleLine="true"  
  13.         android:textSize="16sp"  
  14.         android:textStyle="bold" />  
  15.   
  16. </RelativeLayout>  

3.ItemInfo.java:

[java] view plaincopy
  1. package com.demo.downrefresh;  
  2.   
  3. /** 
  4.  * ListView中item属性 
  5.  *  
  6.  * @author w.w 
  7.  */  
  8. public class ItemInfo {  
  9.   
  10.     /** 
  11.      * id 
  12.      */  
  13.     private int id;  
  14.   
  15.     /** 
  16.      * name 
  17.      */  
  18.     private String name;  
  19.   
  20.     public int getId() {  
  21.         return id;  
  22.     }  
  23.   
  24.     public void setId(int id) {  
  25.         this.id = id;  
  26.     }  
  27.   
  28.     public String getName() {  
  29.         return name;  
  30.     }  
  31.   
  32.     public void setName(String name) {  
  33.         this.name = name;  
  34.     }  
  35.   
  36. }  

4.ListViewAdapter.java:

[java] view plaincopy
  1. package com.demo.downrefresh;  
  2.   
  3. import java.util.List;  
  4.   
  5. import android.content.Context;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.widget.ArrayAdapter;  
  10. import android.widget.TextView;  
  11.   
  12. /** 
  13.  * ListView适配器 
  14.  * @author w.w 
  15.  */  
  16. public class ListViewAdapter extends ArrayAdapter<ItemInfo> {  
  17.       
  18.     private LayoutInflater inflater;  
  19.       
  20.     public ListViewAdapter(Context context, List<ItemInfo> list) {  
  21.         super(context, 0, list);  
  22.         inflater = LayoutInflater.from(context);  
  23.     }  
  24.       
  25.     @Override  
  26.     public View getView(int position, View convertView, ViewGroup parent) {  
  27.         ItemInfo info = getItem(position);  
  28.           
  29.         if (convertView == null) {  
  30.             convertView = inflater.inflate(R.layout.item_listview, null);  
  31.         }  
  32.           
  33.         TextView name = (TextView) convertView.findViewById(R.id.item_name);  
  34.         name.setText(info.getName());  
  35.           
  36.         return convertView;  
  37.     }  
  38.   
  39. }  
5.MainActivity.java:

[java] view plaincopy
  1. package com.demo.downrefresh;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.R.integer;  
  7. import android.app.Activity;  
  8. import android.os.Bundle;  
  9. import android.os.Handler;  
  10. import android.support.v4.widget.SwipeRefreshLayout;  
  11. import android.widget.ListView;  
  12.   
  13. /** 
  14.  * 主页 
  15.  * @author w.w 
  16.  */  
  17. public class MainActivity extends Activity implements SwipeRefreshLayout.OnRefreshListener {  
  18.   
  19.     /** 
  20.      * 给ListView添加下拉刷新 
  21.      */  
  22.     private SwipeRefreshLayout swipeLayout;  
  23.       
  24.     /** 
  25.      * ListView 
  26.      */  
  27.     private ListView listView;  
  28.       
  29.     /** 
  30.      * ListView适配器 
  31.      */  
  32.     private ListViewAdapter adapter;  
  33.       
  34.     private List<ItemInfo> infoList;  
  35.     private int i=0;  
  36.       
  37.     @Override  
  38.     protected void onCreate(Bundle savedInstanceState) {  
  39.         super.onCreate(savedInstanceState);  
  40.         setContentView(R.layout.activity_main);  
  41.   
  42.         swipeLayout = (SwipeRefreshLayout) this.findViewById(R.id.swipe_refresh);  
  43.         swipeLayout.setOnRefreshListener(this);  
  44.           
  45.         // 顶部刷新的样式  
  46.         swipeLayout.setColorScheme(android.R.color.holo_red_light, android.R.color.holo_green_light,  
  47.                 android.R.color.holo_blue_bright, android.R.color.holo_orange_light);  
  48.   
  49.         infoList = new ArrayList<ItemInfo>();  
  50.         ItemInfo info = new ItemInfo();  
  51.         info.setName("coin");  
  52.         infoList.add(info);  
  53.         listView = (ListView) this.findViewById(R.id.listview);  
  54.         adapter = new ListViewAdapter(this, infoList);  
  55.         listView.setAdapter(adapter);  
  56.     }  
  57. /** 
  58.  * 下拉刷新 
  59.  */  
  60.   
  61.     public void onRefresh() {  
  62.         new Handler().postDelayed(new Runnable() {  
  63.             public void run() {  
  64.                 swipeLayout.setRefreshing(false);  
  65.                   
  66.                     ItemInfo info = new ItemInfo();  
  67.                     info.setName("coin-refresh"+i);  
  68.                     infoList.add(info);  
  69.                     i++;  
  70.                   
  71.                   
  72.                 adapter.notifyDataSetChanged();//刷新ListView  
  73.             }  
  74.         }, 500);  
  75.     }  
  76. }  
6.运行实例如下:

总结

1.此布局内只能有一个直接子View;

2.setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener listener):设置手势滑动监听器;

3.覆写public void onRefresh()。


0 0
原创粉丝点击