下拉刷新控件(3)系統自帶的下拉刷新控件SwipeRefreshLayout(推薦*)

1.簡介

  The SwipeRefreshLayout should be used whenever the user can refresh the contents of a view via a vertical swipe gesture. The activity that instantiates this view should add an OnRefreshListener to be notified whenever the swipe to refresh gesture is completed. The SwipeRefreshLayout will notify the listener each and every time the gesture is completed again; the listener is responsible for correctly determining when to actually initiate a refresh of its content. If the listener determines there should not be a refresh, it must call setRefreshing(false) to cancel any visual indication of a refresh. If an activity wishes to show just the progress animation, it should call setRefreshing(true). To disable the gesture and progress animation, call setEnabled(false) on the view.html

  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. This view will also be made the target of the gesture and will be forced to match both the width and the height supplied in this layout. The SwipeRefreshLayout does not provide accessibility events; instead, a menu item must be provided to allow refresh of the content wherever this gesture is used.java

SwipeRefreshLayout 是支援包裏的一個佈局類。經過識別垂直下拉手勢顯示刷新動畫,它要做爲要刷新內容的父控件,而且只能有一個直接子view。推薦使用它,不要自寫了。刷新效果以下圖中的圓圈:

 

 

它是一個layout,裏面只能放:ListView,GridView.但經測試,也可放ScrollView。官方原文以下:android

  To add the swipe to refresh widget to an existing app, add SwipeRefreshLayout as the parent of a single ListView or GridView. Remember that SwipeRefreshLayout only supports a single ListView or GridView child.app

2.listview示例

xmldom

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <android.support.v4.widget.SwipeRefreshLayout
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     xmlns:android="http://schemas.android.com/apk/res/android"
 6     android:id="@+id/swipe">
 7     <ListView
 8         android:id="@android:id/list"
 9         android:layout_width="match_parent"
10         android:layout_height="match_parent" />
11     
12 </android.support.v4.widget.SwipeRefreshLayout>

java代碼ide

 1 import java.util.ArrayList;
 2 
 3 import android.app.Fragment;
 4 import android.os.Bundle;
 5 import android.os.Handler;
 6 import android.support.v4.widget.SwipeRefreshLayout;
 7 import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
 8 import android.view.LayoutInflater;
 9 import android.view.View;
10 import android.view.ViewGroup;
11 import android.widget.AbsListView.OnScrollListener;
12 import android.widget.AbsListView;
13 import android.widget.ArrayAdapter;
14 import android.widget.ListView;
15 
16 import com.example.pullrefreshview.R;
17 
18 public class SwipeRefreshListViewFrgmt extends Fragment implements OnRefreshListener{
19     SwipeRefreshLayout refreshLayout;
20     ListView listview;
21     ArrayList<String> datas;
22     ArrayAdapter<String> adapter;
23     
24     void initAdapter(){
25         datas = new ArrayList<String>();
26         for (int i = 0; i < 30; i++) {
27             datas.add("item " + i);
28         }
29         adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,android.R.id.text1,datas);
30     }
31     @Override
32     public View onCreateView(LayoutInflater inflater, ViewGroup container,
33             Bundle savedInstanceState) {
34         initAdapter();
35         refreshLayout  = (SwipeRefreshLayout) inflater.inflate(R.layout.frgmt_srl_listview,container,false);
36         refreshLayout.setEnabled(false); //默認先關閉 下拉刷新功能
37         refreshLayout.setOnRefreshListener(this);
38         listview = (ListView) refreshLayout.findViewById(android.R.id.list);
39         listview.setAdapter(adapter);
40         listview.setOnScrollListener(new OnScrollListener() {
41             @Override
42             public void onScrollStateChanged(AbsListView view, int scrollState) {
43             }
44             @Override
45             public void onScroll(AbsListView view, int firstVisibleItem,
46                     int visibleItemCount, int totalItemCount) {
47                 if (firstVisibleItem == 0)
48                     refreshLayout.setEnabled(true);
49                 else
50                     refreshLayout.setEnabled(false);
51             }
52         });
53         return refreshLayout;
54     }
55     @Override
56     public void onRefresh() {
57         //layout正在刷新時回調,
58         ( new Handler()).postDelayed(new Runnable() {//3秒後關閉刷新。
59             @Override
60             public void run() {
61                 refreshLayout.setRefreshing(false);
62             }
63         }, 3000);
64     }
65 }

基本流程:  佈局

  • 在xml中 添加
  • 在代碼中找到它
  • 設置它的 OnRefreshListener
  • 在onRefresh中寫更新代碼
  • 關閉刷新

注意第36行,先把刷新功能關閉。當知足某條件時再打開它(第48行)。post

3.scrollview示例

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <android.support.v4.widget.SwipeRefreshLayout
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     xmlns:android="http://schemas.android.com/apk/res/android"
 6     android:id="@+id/swipe">
 7     <ScrollView
 8         android:id="@+id/scrollview"
 9         android:layout_width="match_parent"
10         android:layout_height="match_parent">
11         <LinearLayout
12             android:layout_width="match_parent"
13             android:layout_height="wrap_content"
14             android:orientation="vertical" >
15             <TextView
16                 android:layout_width="match_parent"
17                 android:layout_height="wrap_content"
18                 android:id="@+id/rndNum"
19                 android:hint="random number "
20                 />
21         </LinearLayout>
22     </ScrollView>
23 </android.support.v4.widget.SwipeRefreshLayout>

 java代碼測試

 1 import android.app.Fragment;
 2 import android.os.Bundle;
 3 import android.os.Handler;
 4 import android.support.v4.widget.SwipeRefreshLayout;
 5 import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
 6 import android.view.LayoutInflater;
 7 import android.view.View;
 8 import android.view.ViewGroup;
 9 import android.widget.TextView;
10 
11 import com.example.pullrefreshview.R;
12 
13 public class SwipeRefreshScrollViewFrgmt extends Fragment implements OnRefreshListener {
14     SwipeRefreshLayout refreshView;
15     TextView rndNum ;
16     @Override
17     public View onCreateView(LayoutInflater inflater, ViewGroup container,
18             Bundle savedInstanceState) {
19         refreshView = (SwipeRefreshLayout) inflater.inflate(R.layout.frgmt_srl_scrollview, container,false);
20  refreshView.setColorSchemeResources(android.R.color.holo_blue_dark, android.R.color.holo_blue_light, android.R.color.holo_green_light, android.R.color.holo_green_light);
21         refreshView.setOnRefreshListener(this);
22         View v = refreshView.findViewById(R.id.scrollview);
23         rndNum = (TextView) v.findViewById(R.id.rndNum);
24         return refreshView;
25     }
26     @Override
27     public void onRefresh() {
28         refreshView.setRefreshing(true);
29         ( new Handler()).postDelayed(new Runnable() {
30             @Override
31             public void run() {
32                 refreshView.setRefreshing(false);
33                 double f = Math.random();
34                 rndNum.setText(String.valueOf(f));
35             }
36         }, 3000);
37     }
38 }

 

4.經常使用方法

  • public void setEnabled(boolean enabled)  是否關閉下拉刷新功能(關閉下拉手勢和刷新界面)。
  • public void setRefreshing(boolean refreshing) ; true顯示刷新界面,false關閉刷新界面。
  • public void setColorSchemeResources(@ColorRes int... colorResIds) 設置刷新條的顏色。
  • ...
相關文章
相關標籤/搜索