布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ptr="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
ptr:ptrAnimationStyle="rotate"/>
</LinearLayout>
================================================
逻辑代码:
package com.qianfeng.pulltoreresh_demo;
import java.util.ArrayList;
import java.util.List;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Typeface;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
//定义对象
private PullToRefreshListView pullList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pullList=(PullToRefreshListView) findViewById(R.id.list);
//给PullToRefreshListView添加headView
//获取PullToRefreshListView中的ListView
ListView list=pullList.getRefreshableView();
ImageView img=new ImageView(this);
img.setImageResource(R.drawable.ic_launcher);
//添加头
list.addHeaderView(img);
//配置数据源
List<String>dataList=new ArrayList<String>();
for (int i = 0; i <20; i++) {
dataList.add("item:"+i);
}
//配置适配器
ArrayAdapter<String>adapter=new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1, dataList);
//添加适配器
pullList.setAdapter(adapter);
//控制PullToRefreshListView添加上拉和下拉的方式
//方式有3种(仅上拉 仅下拉 上下都有)
pullList.setMode(Mode.BOTH);
//在布局文件中通过属性(ptr:ptrAnimationStyle="rotate")控制加载图标的动画效果(旋转[rotate] 翻转[flip])
//修改刷新布局的参数
ILoadingLayout startLayout=pullList.getLoadingLayoutProxy(true, false);//获取上啦刷新布局对象
startLayout.setLastUpdatedLabel("上次更新的时间");//获得上次更新的时间
startLayout.setLoadingDrawable(getResources().getDrawable(R.drawable.ic_launcher));//设置加载图标
startLayout.setPullLabel("下拉刷新");//设置在下拉过程中显示的文字
startLayout.setReleaseLabel("松开刷新");//设置下拉到底部时的提示文字
startLayout.setRefreshingLabel("玩命加载中...");//下拉到底部时松开后的文字
startLayout.setTextTypeface(Typeface.MONOSPACE);//设置字体格式
ILoadingLayout endLayout = pullList.getLoadingLayoutProxy(false, true);//获取上拉刷新布局的对象
endLayout.setLastUpdatedLabel("");
endLayout.setPullLabel("上拉加载");
endLayout.setRefreshingLabel("正在加载数据。。。");
endLayout.setReleaseLabel("松开刷新");
//分别监听上啦和下拉的操作
pullList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {
//下拉刷新
@Override
public void onPullDownToRefresh(PullToRefreshBase refreshView) {
// TODO Auto-generated method stub
//下拉刷新的逻辑代码
/*
* 1、清空数据集合中的原有的所有的数据
*
* 2、重新请求第一页数据,存入到数据集合中
*
* 3、通知UI展示新数据(notifyDataSetChanged)
*/
}
//上啦加载
@Override
public void onPullUpToRefresh(PullToRefreshBase refreshView) {
// TODO Auto-generated method stub
//上拉加载的逻辑代码
/*
* 1、请求下一页数据
*
* 2、添加到数据集合中
*
* 3、通知UI展示新数据(notifyDataSetChanged)
*/
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class MyTask extends AsyncTask<String, Void, String>{
//执行网络请求,加载数据
@Override
protected String doInBackground(String... arg0) {
//模拟网络请求
try {
Thread.sleep(3 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return arg0[0];
}
//数据加载完成,刷新UI
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Toast.makeText(MainActivity.this, result, 0).show();
//通知刷新布局,数据加载完成
pullList.onRefreshComplete();
}
}
}