下拉刷新控件(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
<?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(推荐*)的更多相关文章
- [Swift通天遁地]二、表格表单-(4)使用系统自带的下拉刷新控件,制作表格的下拉刷新效果
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- Android仿苹果版QQ下拉刷新实现(一) ——打造简单平滑的通用下拉刷新控件
前言: 忙完了结婚乐APP的开发,终于可以花一定的时间放在博客上了.好了,废话不多说,今天我们要带来的效果是苹果版本的QQ下拉刷新.首先看一下目标效果以及demo效果: 因为此效果实现的步骤 ...
- Asp.net绑定带层次下拉框(select控件)
1.效果图 2.数据库中表数据结构 3.前台页面 <select id="pid" runat="server" style="width:16 ...
- 自定义SWT控件二之自定义多选下拉框
2.自定义下拉多选框 package com.view.control.select; import java.util.ArrayList; import java.util.HashMap; im ...
- IP编辑控件(因为封装的是系统自带控件,所以也使用了CreateSubClass,不过为啥要封装CN_COMMAND和CN_NOTIFY不是很明白)
最近需要用一个IP输入控件,网上找了几个,都不符合效果,有些还有一些奇怪的Bug.后来发现原来系统已经提供了IP地址编辑控件,只是系统提供的控件不能设置只读效果.网上找了下资料,封装了一下,自己迂回一 ...
- C# 如何定义让PropertyGrid控件显示[...]按钮,并且点击后以下拉框形式显示自定义控件编辑属性值
关于PropertyGrid控件的详细用法请参考文献: 1.C# PropertyGrid控件应用心得 2.C#自定义PropertyGrid属性 首先定义一个要在下拉框显示的控件: using Sy ...
- Android自定义日历控件(继承系统控件实现)
Android自定义日历控件(继承系统控件实现) 主要步骤 编写布局 继承LinearLayout设置子控件 设置数据 继承TextView实现有圆圈背景的TextView 添加Attribute 添 ...
- 使用OC和swift创建系统自带的刷新界面
使用OC和swift创建系统自带的刷新界面 一:swift刷新界面代码: import UIKit class ViewController: UITableViewController { // 用 ...
- Android系统在超级终端下必会的命令大全(adb shell命令大全)
. 显示系统中全部Android平台: android list targets . 显示系统中全部AVD(模拟器): android list avd . 创建AVD(模拟器): android c ...
随机推荐
- 【BZOJ】【1052】【HAOI2007】覆盖问题
二分+贪心 首先二分L,转化成判定问题…… 但是判定不会判啊QAQ orz hzwer,用一个最小的矩形框住所有点后,直接往矩形的角上摆正方形……第二个用同样的方法摆,最后判一下剩下的能否被完全覆盖 ...
- 【BZOJ】【1874】取石子游戏
SG函数 嗯博弈论入门题,关于SG函数这个东西可以去看VFK神犇的博客,讲的非常清楚Orz. 传送门:vfleaking.blog.163.com/blog/static/17480763420123 ...
- Hdu 1429 胜利大逃亡(续) 分类: Brush Mode 2014-08-07 17:01 92人阅读 评论(0) 收藏
胜利大逃亡(续) Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Subm ...
- [百度空间] [原]跨平台编程注意事项(二): windows下 x86到x64的移植
之前转的: 将程序移植到64位Windows 还有自己乱写的一篇: 跨平台编程注意事项(一) 之前对于x64平台的移植都是纸上谈兵,算是前期准备工作, 但起码在写代码时,已经非常注意了.所以现在移植起 ...
- css hack一览
浏览器对css hack的支持情况
- CSS 类名的单词连字符:下划线还是连接符?
本文的部分内容整理自我对此问题的解答: 命名 CSS 的类或 ID 时单词间如何连接? - 知乎 问题 CSS 类或 ID 命名时单词间连接通常有这几种写法: 驼峰式: solutionTitle.s ...
- Sqli-labs less 40
Less-40 本关的sql语句为SELECT * FROM users WHERE id=('$id') LIMIT 0,1 我们根据sql语句构造以下的payload: http://127.0. ...
- 转:jxl导出excel(合并单元格)
Demo 代码如下: import java.io.*; import jxl.*; import jxl.format.UnderlineStyle; import jxl.write.*; pub ...
- ios网站,博客
中文 网站系列 objcio.cncocoachina.comcode4app.com泰然网 博客系列唐巧地球人都知道哈.http://blog.devtang.com/巧哥新出书了,速度入手吧. 虾 ...
- 使用angular.js开发的一个简易todo demo
前沿 在CVTE实习考察的一周里,接触到了angular,并在最后的一天任务里要求使用angular做一个功能主要包括创建.编辑.恢复.删除以及留言的todo demo,并支持响应式布局.因为之前没怎 ...