您的位置:首页 > 其它

PullToRefresh使用详解--构建下拉刷新的listView

2016-04-16 16:30 471 查看
转载请指明出处:http://blog.csdn.net/lovemy_baby/article/details/51167663

ListView下拉刷新详解:


一、导入Library

下载源码后(https://github.com/chrisbanes/Android-PullToRefresh),里面有个Library工程,添加工程到Eclipse中;



另外extras文件夹还有两个工程:PullToRefreshListFragment和PullToRefreshViewPager,由于我们的这个用不到他们的库文件,所以不必导入了;


二、实战


1、新建工程,添加Libray库到工程中

新建工程(try_PullToRefresh)后,右键-》Properties-》Android-》Add 选择上面的Library,然后就是这个样子的



以上为转载的一些内容,但是都是大同小异,
先看看效果:





2下面我们看看main.xml代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

>

<com.handmark.pulltorefresh.library.PullToRefreshListView

android:id="@+id/pull_refresh_list"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:cacheColorHint="#00000000"

android:divider="#19000000"

android:dividerHeight="4dp"

android:fadingEdge="none"

android:fastScrollEnabled="false"

android:footerDividersEnabled="false"

android:headerDividersEnabled="false"

android:smoothScrollbar="true" />

</RelativeLayout>

其中间代码就相当于一个ListView控件。

3Java代码详解:

package com.itheima.listviewdemotwo;

import java.util.Arrays;

import java.util.LinkedList;

import com.handmark.pulltorefresh.library.PullToRefreshBase;

import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;

import com.handmark.pulltorefresh.library.PullToRefreshListView;

import android.R.string;

import android.app.Activity;

import android.os.AsyncTask;

import android.os.Bundle;

import android.text.format.DateUtils;

import android.view.Menu;

import android.view.MenuItem;

import android.widget.ArrayAdapter;

import android.widget.ListView;

public class MainActivity extends Activity {

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", "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 LinkedList<String> mListItems;//显示的列表对应原字符串

private PullToRefreshListView mPullToRefreshListView;//PullToRefreshListView实例

private ArrayAdapter<String> mAdapter;//ListView的适配器

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

//获取ID

mPullToRefreshListView=(PullToRefreshListView)findViewById(R.id.pull_refresh_list);

// Set a listener to be invoked when the list should be refreshed.设置一个侦听器被调用时应该刷新列表。

mPullToRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {

@Override

public void onRefresh(PullToRefreshBase<ListView> refreshView) {

String label=DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME|DateUtils.FORMAT_SHOW_DATE|DateUtils.FORMAT_ABBREV_ALL);

// Update the LastUpdatedLabel 更新LastUpdatedLabel

refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);

// Do work to refresh the list here.

new GetDataTask().execute();

}

});

mListItems=new LinkedList<String>();

mListItems.addAll(Arrays.asList(mStrings));

mAdapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,mListItems);

ListView mListView=mPullToRefreshListView.getRefreshableView();

mListView.setAdapter(mAdapter);

}

private class GetDataTask extends AsyncTask<Void, Void, String>{

@Override

protected String doInBackground(Void... params) {

try {

Thread.sleep(1000);

} catch (Exception e) {

}

String str="Added after refresh...I add";

return str;

}

@Override

protected void onPostExecute(String result) {

mListItems.addFirst(result);

mAdapter.notifyDataSetChanged();

mPullToRefreshListView.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", "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 LinkedList<String> mListItems;//显示的列表对应原字符串

private PullToRefreshListView mPullToRefreshListView;//PullToRefreshListView实例

private ArrayAdapter<String> mAdapter;//ListView的适配器

这是变量的定义。

在onCreate()中主要是分为两步,初始化PullToRefreshListView并设置监听器

mPullToRefreshListView=(PullToRefreshListView)findViewById(R.id.pull_refresh_list);

// Set a listener to be invoked when the list should be refreshed.设置一个侦听器被调用时应该刷新列表。

mPullToRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {

@Override

public void onRefresh(PullToRefreshBase<ListView> refreshView) {

String label=DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME|DateUtils.FORMAT_SHOW_DATE|DateUtils.FORMAT_ABBREV_ALL);

// Update the LastUpdatedLabel 更新LastUpdatedLabel

refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);

// Do work to refresh the list here.

new GetDataTask().execute();

}

});

然后设置适配器的内容,并与ListView绑定起来

mListItems=new LinkedList<String>();

mListItems.addAll(Arrays.asList(mStrings));

mAdapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,mListItems);

ListView mListView=mPullToRefreshListView.getRefreshableView();

mListView.setAdapter(mAdapter);

然后是执行刷新的类GetDataTask():

private class GetDataTask extends AsyncTask<Void, Void, String>{

@Override

protected String doInBackground(Void... params) {

try {

Thread.sleep(1000);

} catch (Exception e) {

}

String str="Added after refresh...I add";

return str;

}

@Override

protected void onPostExecute(String result) {

mListItems.addFirst(result);

mAdapter.notifyDataSetChanged();

mPullToRefreshListView.onRefreshComplete();

super.onPostExecute(result);

}

}

(1)派生自AsyncTask

由于派生自AsyncTask,所以下面的那个函数doInBackground和onPostExecute就不难理解了,这两个函数是AsyncTask必须是重写的两个函数

(2)doInBackground函数

doInBackground执行要于后台执行的语句,返回的值可以是任意类型,但要提前在extends AsyncTask<Void, Void, String> 中定义,这个返回值会做为onPostExecute的参数result传到onPostExecute函数中;如果对于网络访问程序,doInBackground就执行访问网络的代码,然后讲返回值存在result中传给onPostExecute函数,以刷新列表;

(3)onPostExecute函数

onPostExecute()是对返回的值进行操作,并添加到ListView的列表中,有两种添加方式添加到头部----mListItems.addFirst(result);和添加在尾部----mListItems.addLast(result);

源码下载:http://download.csdn.net/detail/lovemy_baby/9493129
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: