在Android开发中有时我们须要訪问网络实时刷新数据。比方QQ好友在线状态最新信息,QQ空间须要显示很多其它的好友动态信息,EOE论坛client显示很多其它的文章帖子信息等。android-pulltorefresh开源项目提供一个向下滑动即刷新列表的功能,将该项目稍作改动就可以应用到自己的项目中。

1.下载地址

https://github.com/johannilsson/android-pulltorefresh

该项目为 Android 应用提供一个向下滑动即刷新列表的功能。

2.project组成

PullToRefreshListView.java

OnRefreshListener 监听刷新操作的接口 。onRefresh()刷新函数 在列表头部显示正在进行的刷新操作进度条

onRefreshComplete() 刷新操作完毕后。恢复列表常态

上述类和接口的详细实现:

public class PullToRefreshListView extends ListView implements OnScrollListener{

  /**Interface definition for a callback to be invoked when list should be refreshed.
*/
public interface OnRefreshListener {
/**
* Called when the list should be refreshed.
* <p>
* A call to {@link PullToRefreshListView #onRefreshComplete()} is expected to indicate that the refresh has completed.
*/
public void onRefresh();
} /**
* Resets the list to a normal state after a refresh.
* @param lastUpdated Last updated at.
*/
public void onRefreshComplete(CharSequence lastUpdated) {
setLastUpdated(lastUpdated);
onRefreshComplete();
} /**
* Resets the list to a normal state after a refresh.
*/
public void onRefreshComplete() {
Log.d(TAG, "onRefreshComplete");
resetHeader();
// If refresh view is visible when loading completes, scroll down to
// the next item.
if (getFirstVisiblePosition() == 0) {
invalidateViews();
setSelection(1);
}
}
}

pull_to_refresh_header.xml

PullToRefreshListView头部 显示刷新进度条信息 。在 PullToRefreshListView.java调用以下的语句将该子布局加入到列表顶部

private LayoutInflater mInflater;

private RelativeLayout mRefreshView;

mInflater = (LayoutInflater) context.getSystemService(

                Context.LAYOUT_INFLATER_SERVICE);

//header part of PullToRefreshListView

mRefreshView = (RelativeLayout) mInflater.inflate(R.layout.pull_to_refresh_header, this, false);

//add header part to the ListView

addHeaderView(mRefreshView);

PullToRefreshActivity.java   (MainActivity)

pull_to_refresh.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!--
The PullToRefreshListView replaces a standard ListView widget.
-->
<com.markupartist.android.widget.PullToRefreshListView
android:id="@+id/android:list"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
/>
</LinearLayout>

注:

LiveActivity本身继承了关于List操作的众多接口,我们能够方便的重写这些操作中须要的方法来实现自己须要的功能。

假设要用ListActivity。则 Activity的Layout文件里必须包含一个(仅仅能一个)ListView。且ListView的id=
"@id/android:list"。

3.PullToRefreshActivity.java

刷新额外的列表数据String mExtras[],当额外列表数据显示完成时。不再进行刷新操作。

比如以下这个样例刷新三次以后,就没有额外须要显示的数据。拉松列表进行刷新操作将提示“No More Messages”

package com.markupartist.android.example.pulltorefresh;

import java.util.Arrays;
import java.util.LinkedList; import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Toast; import com.markupartist.android.widget.PullToRefreshListView;
import com.markupartist.android.widget.PullToRefreshListView.OnRefreshListener; @SuppressLint("NewApi")
public class PullToRefreshActivity extends ListActivity {
private LinkedList<String> mListItems;
int count = 0; /** Called when the activity is first created. */
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pull_to_refresh); // Set a listener to be invoked when the list should be refreshed.
((PullToRefreshListView) getListView()).setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh() {
// Do work to refresh the list here.
if(count < mExtras.length)
new GetDataTask().execute(count++);
else{
Toast.makeText(getApplicationContext(), "No More Messages",
Toast.LENGTH_LONG).show(); //Resets the list to a normal state after a refresh
((PullToRefreshListView) getListView()).onRefreshComplete();
} }
}); mListItems = new LinkedList<String>();
mListItems.addAll(Arrays.asList(mStrings)); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, mListItems); setListAdapter(adapter);
} private class GetDataTask extends AsyncTask<Integer, Void, String[]> { private int count; @Override
protected String[] doInBackground(Integer... params) {
count = params[0];
// Simulates a background job.
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
;
}
return mStrings;
} @SuppressLint("NewApi")
@Override
protected void onPostExecute(String[] result) {
// mListItems.addFirst("Added after refresh...");
mListItems.addFirst(mExtras[count]); // Call onRefreshComplete when the list has been refreshed.
((PullToRefreshListView) getListView()).onRefreshComplete(); // super.onPostExecute(result);
}
} private String[] mStrings = {
"Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam",
"Abondance", "Ackawi", "Acorn", "Adelost", "Affidelice au Chablis",
"Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",
"Allgauer Emmentaler"}; private String[] mExtras = {"extra1","extra2","extra3"}; }

Android开源项目pulltorefresh分析与简单使用的更多相关文章

  1. 2015-2016最火的Android开源项目--github开源项目集锦(不看你就out了)

    标签: Android开发开源项目最火Android项目github 2015-2016最火的Android开源项目 本文整理与集结了近期github上使用最广泛最火热与最流行的开源项目,想要充电与提 ...

  2. Android开源项目分类汇总

    目前包括: Android开源项目第一篇——个性化控件(View)篇   包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView. ...

  3. 59.Android开源项目及库 (转)

    转载 : https://github.com/Tim9Liu9/TimLiu-Android?hmsr=toutiao.io&utm_medium=toutiao.io&utm_so ...

  4. Android 开源项目分类汇总(转)

    Android 开源项目分类汇总(转) ## 第一部分 个性化控件(View)主要介绍那些不错个性化的 View,包括 ListView.ActionBar.Menu.ViewPager.Galler ...

  5. Android 开源项目分类汇总

    Android 开源项目分类汇总 Android 开源项目第一篇——个性化控件(View)篇  包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView ...

  6. Android开源项目及库搜集

    TimLiu-Android 自己总结的Android开源项目及库. github排名 https://github.com/trending,github搜索:https://github.com/ ...

  7. Android 开源项目及其学习

    Android 系统研究:http://blog.csdn.net/luoshengyang/article/details/8923485 Android 腾讯技术人员博客 http://hukai ...

  8. [转]Android开源项目第二篇——工具库篇

    本文为那些不错的Android开源项目第二篇--开发工具库篇,主要介绍常用的开发库,包括依赖注入框架.图片缓存.网络相关.数据库ORM建模.Android公共库.Android 高版本向低版本兼容.多 ...

  9. GitHub上史上最全的Android开源项目分类汇总 (转)

    GitHub上史上最全的Android开源项目分类汇总 标签: github android 开源 | 发表时间:2014-11-23 23:00 | 作者:u013149325 分享到: 出处:ht ...

随机推荐

  1. Qt录音程序

    源地址:http://www.oschina.net/code/snippet_1243295_48623 [代码] [C/C++]代码 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 ...

  2. 1.0.2-学习Opencv与MFC混合编程之---为播放AVI视频添加滑动条

    源代码地址:http://download.csdn.net/detail/nuptboyzhb/3961642 版本1.0.2新增内容 Ø  全局变量和函数的添加: 在CVMFCview.cpp文件 ...

  3. 阿里巴巴 web前端性能优化进阶路

    Web前端性能优化WPO,相信大多数前端同学都不会陌生,在各自所负责的站点页面中,也都会或多或少的有过一定的技术实践.可以说,这个领域并不缺乏成熟技术理论和技术牛人:例如Yahoo的web站点性能优化 ...

  4. poj1860 解题报告

    题意:这里有N种货币,分别记为1~N,有M种货币交换的方式,每一种方式有A,B两种钱币,有RAB, CAB, RBA and CBA,四个数,表示交换率, Nick手上有其中的一种货币S,货币S的钱数 ...

  5. Java设计模式---外观模式

    外观模式(Facade) 外观模式的意图是:为子系统提供一个接口,便于它的使用.   解释: 简单的说,外观模式就是封装多个上层应用需要的方法,使得上层调用变得简单,为上层提供简单的接口,是设计模式中 ...

  6. Ajax - 在函数中使用Ajax怎么使用返回值 - Ajax赋值给全局变量异常的解决方法

    要使用异步操作:  async : false,//取消异步操作 //添加节点函数 function InsertNode(nodenum, nodename, type) { var returnv ...

  7. IdHttpServer实现webservice(130篇DataSnap文章)

    IdHttpServer实现webservice   朋友有个项目,通信协议使用HTTP,数据序列使用BIN(二进制).他不知道要选用何种技术方案. REST webservice是http+json ...

  8. poj1011Sticks

    传说中的poj必做50题之中的一个-- 这是个传说中的搜索, 一開始以为, 仅仅要棒子加起来等于如果的原始长度, 那么这几根选择的棒子就不用管了, 结果卡在第一个例子-- 看了一下,发现, 代码把1, ...

  9. BCB/Delphi中常用的VCL函数说明(文件函数)

    --------------------文件操作--------------------函数名称:ChangeFileExt函数说明:更改指定文件的扩展名,函数原型如下:AnsiString __fa ...

  10. mysql 创建函数 error Code: 1227. Access denied;

    mysql> show function status; +------+------------------+----------+------------+----------------- ...