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

 <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" /> </android.support.v4.widget.SwipeRefreshLayout>

java代码

 import java.util.ArrayList;

 import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ListView; import com.example.pullrefreshview.R; public class SwipeRefreshListViewFrgmt extends Fragment implements OnRefreshListener{
SwipeRefreshLayout refreshLayout;
ListView listview;
ArrayList<String> datas;
ArrayAdapter<String> adapter; void initAdapter(){
datas = new ArrayList<String>();
for (int i = ; i < ; i++) {
datas.add("item " + i);
}
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,android.R.id.text1,datas);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
initAdapter();
refreshLayout = (SwipeRefreshLayout) inflater.inflate(R.layout.frgmt_srl_listview,container,false);
refreshLayout.setEnabled(false); //默认先关闭 下拉刷新功能
refreshLayout.setOnRefreshListener(this);
listview = (ListView) refreshLayout.findViewById(android.R.id.list);
listview.setAdapter(adapter);
listview.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (firstVisibleItem == )
refreshLayout.setEnabled(true);
else
refreshLayout.setEnabled(false);
}
});
return refreshLayout;
}
@Override
public void onRefresh() {
//layout正在刷新时回调,
( new Handler()).postDelayed(new Runnable() {//3秒后关闭刷新。
@Override
public void run() {
refreshLayout.setRefreshing(false);
}
}, );
}
}

基本流程:  

  • 在xml中 添加
  • 在代码中找到它
  • 设置它的 OnRefreshListener
  • 在onRefresh中写更新代码
  • 关闭刷新

注意第36行,先把刷新功能关闭。当满足某条件时再打开它(第48行)。

3.scrollview示例

 <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe">
<ScrollView
android:id="@+id/scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rndNum"
android:hint="random number "
/>
</LinearLayout>
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>

java代码

 import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView; import com.example.pullrefreshview.R; public class SwipeRefreshScrollViewFrgmt extends Fragment implements OnRefreshListener {
SwipeRefreshLayout refreshView;
TextView rndNum ;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
refreshView = (SwipeRefreshLayout) inflater.inflate(R.layout.frgmt_srl_scrollview, container,false);
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);
refreshView.setOnRefreshListener(this);
View v = refreshView.findViewById(R.id.scrollview);
rndNum = (TextView) v.findViewById(R.id.rndNum);
return refreshView;
}
@Override
public void onRefresh() {
refreshView.setRefreshing(true);
( new Handler()).postDelayed(new Runnable() {
@Override
public void run() {
refreshView.setRefreshing(false);
double f = Math.random();
rndNum.setText(String.valueOf(f));
}
}, );
}
}

4.常用方法

  • public void setEnabled(boolean enabled)  是否关闭下拉刷新功能(关闭下拉手势和刷新界面)。
  • public void setRefreshing(boolean refreshing) ; true显示刷新界面,false关闭刷新界面。
  • public void setColorSchemeResources(@ColorRes int... colorResIds) 设置刷新条的颜色。
  • ...

下拉刷新控件(3)系统自带的下拉刷新控件SwipeRefreshLayout(推荐*)的更多相关文章

  1. [Swift通天遁地]二、表格表单-(4)使用系统自带的下拉刷新控件,制作表格的下拉刷新效果

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  2. Android仿苹果版QQ下拉刷新实现(一) ——打造简单平滑的通用下拉刷新控件

    前言: 忙完了结婚乐APP的开发,终于可以花一定的时间放在博客上了.好了,废话不多说,今天我们要带来的效果是苹果版本的QQ下拉刷新.首先看一下目标效果以及demo效果:      因为此效果实现的步骤 ...

  3. Asp.net绑定带层次下拉框(select控件)

    1.效果图 2.数据库中表数据结构 3.前台页面 <select id="pid" runat="server" style="width:16 ...

  4. 自定义SWT控件二之自定义多选下拉框

    2.自定义下拉多选框 package com.view.control.select; import java.util.ArrayList; import java.util.HashMap; im ...

  5. IP编辑控件(因为封装的是系统自带控件,所以也使用了CreateSubClass,不过为啥要封装CN_COMMAND和CN_NOTIFY不是很明白)

    最近需要用一个IP输入控件,网上找了几个,都不符合效果,有些还有一些奇怪的Bug.后来发现原来系统已经提供了IP地址编辑控件,只是系统提供的控件不能设置只读效果.网上找了下资料,封装了一下,自己迂回一 ...

  6. C# 如何定义让PropertyGrid控件显示[...]按钮,并且点击后以下拉框形式显示自定义控件编辑属性值

    关于PropertyGrid控件的详细用法请参考文献: 1.C# PropertyGrid控件应用心得 2.C#自定义PropertyGrid属性 首先定义一个要在下拉框显示的控件: using Sy ...

  7. Android自定义日历控件(继承系统控件实现)

    Android自定义日历控件(继承系统控件实现) 主要步骤 编写布局 继承LinearLayout设置子控件 设置数据 继承TextView实现有圆圈背景的TextView 添加Attribute 添 ...

  8. 使用OC和swift创建系统自带的刷新界面

    使用OC和swift创建系统自带的刷新界面 一:swift刷新界面代码: import UIKit class ViewController: UITableViewController { // 用 ...

  9. Android系统在超级终端下必会的命令大全(adb shell命令大全)

    . 显示系统中全部Android平台: android list targets . 显示系统中全部AVD(模拟器): android list avd . 创建AVD(模拟器): android c ...

随机推荐

  1. A trip through the graphics pipeline 2011 Part 10(翻译)

    之前的几篇翻译都烂尾了,这篇希望....能好些,恩,还有往昔呢. ------------------------------------------------------------- primi ...

  2. 编写一函数用来实现左右循环移位。函数原型为move(value,n);n>0时右移n位,n<0时左移|n|位。

    #include<stdio.h> #include<stdlib.h> int main(){ setbuf(stdout,NULL); int move(int,int); ...

  3. 微信电脑版也能用公众号自定义菜单 微信1.2 for Windows发布

    昨日,微信电脑版发布更新,版本为微信1.2 for Windows,最大的特色就是加入了保存聊天记录功能,可以使用公账号菜单,手机上收藏的表情也能在电脑版上发送,可以接收转账消息. 本次微信pc版更新 ...

  4. logback日志项目使用方法 - 150205交易模块添加日志信息logback,orderNo订单号为log主键便于跟踪,数字常量化,解决取消支付BUG,弱网络环境原因

    1.项目里面的日志,便于跟踪数据的变更和异常错误信息产生.生产环境的日志级别是INFO,测试环境日志级别DEBUG,如果生产环境的日志级别是DEBUG,虽然方便查询问题,可以看到SQL语句等信息,但是 ...

  5. C#&Java重学笔记(集合比较和转换)

    C#部分: 1.C#中集合有三种,数组类,ArrayList,和字典键值对类,一般也可以自定义集合,但是自定义集合的类型也只有这三类. 2.自定义集合实现三类集合的方法:前两者需要继承Collecti ...

  6. B树、B-树、B+树、B*树---转载

    B树 即二叉搜索树: 1.所有非叶子结点至多拥有两个儿子(Left和Right): 2.所有结点存储一个关键字: 3.非叶子结点的左指针指向小于其关键字的子树,右指针指向大于其关键字的子树: 如: B ...

  7. cf 403 D

    D. Beautiful Pairs of Numbers time limit per test 3 seconds memory limit per test 256 megabytes inpu ...

  8. POJ 2092

    #include <iostream> #include <algorithm> #define MAXN 10005 using namespace std; int _m[ ...

  9. MetInfo安装

    安装MetInfo企业网站管理系统需要经历三个步骤:安装准备.上传文件.安装系统. 第一步:安装前的准备 环境要求:需要支持PHP并提供Mysql数据库的空间(虚拟主机),详细要求: 下载MetInf ...

  10. CAS登录后回传除了ticket参数以外的其他自定义参数

    在一次项目的技术选型中,选择了easyui+cas+shiro+spring的组合,cas实现了单点登录,这使得在一个应用中嵌入另一个应用的页面来展示数据所涉及到的授权方面变得简单. 由于shiro在 ...