下拉刷新控件(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.

  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.

SwipeRefreshLayout 是支援包里的一个布局类。通过识别垂直下拉手势显示刷新动画,它要作为要刷新内容的父控件,并且只能有一个直接子view。推荐使用它,不要自写了。刷新效果如下图中的圆圈:

 

 

它是一个layout,里面只能放:ListView,GridView.但经测试,也可放ScrollView。官方原文如下:

  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.

2.listview示例

xml

复制代码
 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代码

复制代码
 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行)。

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) 设置刷新条的颜色。
  • ...

 

posted @   f9q  阅读(307)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?
点击右上角即可分享
微信分享提示