今天在codepath 上看到一个开源项目 [点击查看]使用到了 SwipeRefreshLayout 实现了下拉刷新,但演示样例并不完整,于是自己就动手写了下.之前看到郭霖的博客上也有介绍下拉刷新,只是他是纯手动实现的,代码量大,较为繁琐.[点击查看]而使用Android
提供的SwipeRefreshLayout 则大大降低了我们的工作量,当然,学会了使用SwipeRefreshLayout之后还是建议去看看如何不借助SwipeRefreshLayout从零開始实现"下拉刷新".

SwipeRefreshLayout is a ViewGroup that can hold only one scrollable
view as a child. This can be either a ScrollView or anAdapterView such
as a ListView.

Note: This layout only exists within more recent versions of support-v4 as explained in this
post
. Edit your app/build.gradlefile to include a support library later than version 19:

要注意的是 SwipeRefreshLayout 在 Android 4.4.2(API 19) 的版本号才得到支持,因此在建project的时候最低版本号要选 19 之后的.

先看效果:

接下来我就直接上代码了. [当中用到的图片能够下载源代码,图片均包括在里面]

布局文件(XML)[activity_main.xml]:

里面就一个ListView,也不要有其它多余的东西

<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"> <ListView
android:layout_marginTop="30dp"
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView> </android.support.v4.widget.SwipeRefreshLayout>

MainActivity 代码:

package com.demo.mummyding.learnswiperefreshlayout;

import android.app.Activity;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast; import org.json.JSONArray; import java.util.ArrayList;
import java.util.List; /**
* 这里要实现 OnRefreshListener 接口
*/
public class MainActivity extends Activity implements SwipeRefreshLayout.OnRefreshListener{
private SwipeRefreshLayout swipeContainer;
ListView listView;
List<viewItem> list;
itemAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
} /**
* 下来刷新就会触发运行此方法
*/
@Override
public void onRefresh() {
/**
* 用Handler().postDelayed 延迟运行
* 当然,不用延迟也能够,我这里是为了看效果,由于这里刷新哗的一下就没了~
*/
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
list.clear();
addItems();
adapter.notifyDataSetChanged();
swipeContainer.setRefreshing(false);
}
}, 1000); /*
不用延迟能够直接像以下这样写
*/
/*
* list.clear();
addItems();
adapter.notifyDataSetChanged();
swipeContainer.setRefreshing(false);
*/
} /**
* 初始化变量&加入事件监听
*/
void init(){
listView = (ListView) findViewById(R.id.list_view);
swipeContainer = (SwipeRefreshLayout) findViewById(R.id.swipeContainer);
swipeContainer.setOnRefreshListener(this);
list = new ArrayList<viewItem>();
adapter = new itemAdapter(this,R.layout.view_layout,list);
listView.setAdapter(adapter);
} /**
* 向ListView加入Item 以下的Item 能够多复制几遍~_~
*/
void addItems(){
viewItem addItem = new viewItem("Aaron");
list.add(addItem);
addItem = new viewItem("Barton");
list.add(addItem);
addItem = new viewItem("Beacher");
list.add(addItem);
addItem = new viewItem("Colbert");
list.add(addItem);
addItem = new viewItem("Dick");
list.add(addItem);
addItem = new viewItem("Gregary");
list.add(addItem);
addItem = new viewItem("Francis");
list.add(addItem);
addItem = new viewItem("Fitch");
list.add(addItem);
addItem = new viewItem("Gordon");
list.add(addItem);
addItem = new viewItem("Eugene");
list.add(addItem);
addItem = new viewItem("Gregary");
list.add(addItem);
addItem = new viewItem("Francis");
list.add(addItem);
addItem = new viewItem("Fitch");
list.add(addItem);
addItem = new viewItem("Gordon");
list.add(addItem);
addItem = new viewItem("Eugene");
list.add(addItem);
addItem = new viewItem("Gregary");
list.add(addItem);
addItem = new viewItem("Francis");
list.add(addItem);
addItem = new viewItem("Fitch");
list.add(addItem);
addItem = new viewItem("Gordon");
list.add(addItem);
addItem = new viewItem("Eugene");
list.add(addItem);
addItem = new viewItem("Gregary");
list.add(addItem);
addItem = new viewItem("Francis");
list.add(addItem);
addItem = new viewItem("Fitch");
list.add(addItem);
addItem = new viewItem("Gordon");
list.add(addItem);
addItem = new viewItem("Eugene");
list.add(addItem);
addItem = new viewItem("Gregary");
list.add(addItem);
addItem = new viewItem("Francis");
list.add(addItem);
addItem = new viewItem("Fitch");
list.add(addItem);
addItem = new viewItem("Gordon");
list.add(addItem);
addItem = new viewItem("Eugene");
list.add(addItem);
addItem = new viewItem("Gregary");
list.add(addItem);
addItem = new viewItem("Francis");
list.add(addItem);
addItem = new viewItem("Fitch");
list.add(addItem);
addItem = new viewItem("Gordon");
list.add(addItem);
addItem = new viewItem("Eugene");
list.add(addItem); } }

接下来是两个类 Item类 和Adapter类[这就属于ListView的的基本使用方法了] 里面做了优化

package com.demo.mummyding.learnswiperefreshlayout;

/**
* Created by mummyding on 15-7-20.
*/
public class viewItem {
private String itemName; public String getItemName() {
return itemName;
} public viewItem(String itemName) {
this.itemName = itemName;
}
}
package com.demo.mummyding.learnswiperefreshlayout;

import android.content.Context;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView; import java.util.List; /**
* Created by mummyding on 15-7-20.
*/
public class itemAdapter extends ArrayAdapter<viewItem> { public itemAdapter(Context context, int resource, List<viewItem> objects) {
super(context, resource, objects);
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
ViewHolder viewHolder;
viewItem item = getItem(position);
if(convertView == null){
view = LayoutInflater.from(getContext()).inflate(R.layout.view_layout, null);
viewHolder = new ViewHolder();
viewHolder.name = (TextView) view.findViewById(R.id.tv_Name);
viewHolder.name.setText(item.getItemName());
view.setTag(viewHolder);
}else{
view = convertView;
viewHolder = (ViewHolder) convertView.getTag();
viewHolder.name.setText(item.getItemName());
} return view;
} /**
* 为了降低getView方法中 findViewById 调用次数 而加入的一个辅助类.
*/
class ViewHolder{
TextView name;
}
}

最后另一个Item View的局部文件[view_layout.xml]

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv_Uers"
android:layout_width="60dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:background="@drawable/user"
/>
<TextView
android:id="@+id/tv_Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/iv_Uers"
android:gravity="center"
android:layout_centerInParent="true"
/>
</RelativeLayout> </LinearLayout>

完整代码:https://github.com/MummyDing/SwipeRefreshLayout

【转载请注明出处】

Author: MummyDing

出处:http://blog.csdn.net/mummyding/article/category/5651761

【Android】使用 SwipeRefreshLayout 实现下拉刷新的更多相关文章

  1. Android 编程下如何调整 SwipeRefreshLayout 的下拉刷新距离

    SwipeRefreshLayout 的下拉刷新距离比较短,并且也没有提供设置下拉距离的 API,但是看 SwipeRefreshLayout 的源码,会发现有一个内部变量 mDistanceToTr ...

  2. RecyclerViewLoadMoreDemo【封装上拉加载功能的RecyclerView,搭配SwipeRefreshLayout实现下拉刷新】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 封装含有上拉加载功能的RecyclerView,然后搭配SwipeRefreshLayout实现下拉刷新.上拉加载功能. 在项目中将 ...

  3. 利用Swiperefreshlayout实现下拉刷新功能的技术探讨

    在常见的APP中通常有着下拉页面从而达到刷新页面的功能,这种看似简单的功能有着花样繁多的实现方式.而利用Swiperefreshlayout实现下拉刷新功能则是其中比较简明扼要的一种. 一般来说,在竖 ...

  4. Android仿苹果版QQ下拉刷新实现(二) ——贝塞尔曲线开发"鼻涕"下拉粘连效果

    前言 接着上一期Android仿苹果版QQ下拉刷新实现(一) ——打造简单平滑的通用下拉刷新控件 的博客开始,同样,在开始前我们先来看一下目标效果: 下面上一下本章需要实现的效果图: 大家看到这个效果 ...

  5. Android SwipeRefreshLayout 官方下拉刷新控件介绍

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24521483 下面App基本都有下拉刷新的功能,以前基本都使用XListView ...

  6. Android开发之使用SwipeRefreshLayout完成下拉刷新

    SwipeRefreshLayout是V4包下的一个组件,老版本的V4包不支持这个组件功能.因此,如果发现自己的项目中无法导入SwipeRefreshLayout的包,那么说明项目中的V4包是老版本的 ...

  7. Android如何定制一个下拉刷新,上滑加载更多的容器

    前言 下拉刷新和上滑加载更多,是一种比较常用的列表数据交互方式. android提供了原生的下拉刷新容器 SwipeRefreshLayout,可惜样式不能定制. 于是打算自己实现一个专用的.但是下拉 ...

  8. RecycleView + SwipeRefreshLayout 实现下拉刷新和底部自动加载

    前段时间项目里面使用了RecycleView 但是里面的刷新和加载都是框架里面封装好的,直接使用 这几天比较闲就自己来实现以下. 因为SwipeRefreshLayout是一个下拉刷新控件所有直接和R ...

  9. Android实现RecyclerView的下拉刷新和上拉载入很多其它

    需求 先上效果图, Material Design风格的下拉刷新和上拉载入很多其它. 源代码地址(欢迎star) https://github.com/studychen/SeeNewsV2 假设对于 ...

随机推荐

  1. 借助百度地图API制作企业百度地图

    做网站需要插入地图,可以借助百度地图API,具体步骤如下: 1.打开百度地图API的网址:   http://api.map.baidu.com/lbsapi/creatmap/ 2.设置中心点 3. ...

  2. PC端样式重置

    html{font-family:"Microsoft YaHei UI","Microsoft YaHei",sans-serif;-ms-text-size ...

  3. 零基础入门学习Python(5)--闲聊之Python的数据类型

    前言 本次主要闲聊一下python的一些数值类型,整型(int),浮点型(float),布尔类型(bool),还有e记法(科学计数法),也是属于浮点型. 数值类型介绍 整型 整型就是我们平时所说的整数 ...

  4. tomcat idea 报权限错误

    出现的错误提示如下: 下午9:11:27 All files are up-to-date下午9:11:27 All files are up-to-date下午9:11:27 Error runni ...

  5. 集训第五周动态规划 C题 编辑距离

    Description Let x and y be two strings over some finite alphabet A. We would like to transform x int ...

  6. Vue如何使用vee-validate表单验证

    Vue项目遇到要表单验证了吧,对我来说表单验证是个很纠(dan)结(teng)的内容,各种判断凌乱到飞起.往常使用jquery的validate插件做表单验证方便吧,你也可以在Vue里引入jquery ...

  7. 原生JS版和jQuery 版实现文件上传功能

    <!doctype html> <html lang="zh"> <head> <meta charset="utf-8&quo ...

  8. 基本dos

    文件夹的操作:   进入指定盘符:盘符名+:     dir:列出当前控制台下的所有文件以及文件夹  . cd +文件夹名称:进入指定文件夹     cd.. 返回上一级 cd \返回到当前目录的根目 ...

  9. MTK平台释疑android M 配置中断相关问题

    1.使用老方法(android L)配置中断,调用request_irq函数时出错,错误代码 -22  Dear Customer: 您好! 如电话沟通,贵司可以在发过来的code基础上做下面的修改再 ...

  10. 权限管理组件:rbac

    rbac: Role_Based Access Control,基于角色的权限控制 权限:一个包含正则表达式 的url就是一个权限 目录结构: rbac这个app中的文件代码如下: rbac/mode ...