在做android项目的时候,我们经常要用到上拉刷新列表,下拉刷新列表的功能,这里简单介绍一个PullToRefresh的使用。
一,下载PullToRefresh,地址:https://github.com/chrisbanes/Android-PullToRefresh。这里面,我们只要其中的Library工程。将它引入我们的工程,在android studio1.01中的引用方法,请看http://blog.csdn.net/chenguang79/article/details/43150303
二,应用,这里我使用了一个自定义的Adapter,我感觉这样能更好的和实战结和起来。下面是代码,我在代码里,都进行了注解
1,布局文件(activity_user_defined_list__pulltore_fresh.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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.cg.studiotest.userDefinedList_PulltoreFresh">
<!--下面就是PullToRefresh控件-->
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/userDefinedList_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是一样的,就不多说了。
2,列表布局文件(pulltorefresh_item.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"
android:orientation="horizontal">
<TextView
android:id="@+id/txt_pulltorefresh_item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/img_pulltorefresh_item_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"/>
</LinearLayout>
3,程序(userDefinedList_PulltoreFresh.java)
package com.example.cg.studiotest;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.widget.ListView;
import android.widget.Toast;
import com.example.cg.baseAdapter.pulltorefresh_baseAdapter;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import java.util.ArrayList;
import java.util.List;
public class userDefinedList_PulltoreFresh extends Activity {
//定义下拉刷新列表控件
private PullToRefreshListView userDefinedList_pull_refresh_list;
//定义原始数据源
private String[] mainStr = {"宋元通宝","太平通宝","淳化元宝","至道元宝","咸平元宝","景德元宝","祥福元宝",
"祥福通宝","天禧通宝","天圣元宝","明道元宝","景祐元宝","皇宝通宝","庆历重宝"};
//定义新加的数据源
private String[] strArray = {"至和元宝","至和重宝","嘉祐元宝","喜祐通宝","治平元宝","熙宁元宝","熙宁通宝"};
//定义要从新的数据源中取出哪个值
private int num;
//定义List的Adapter
private pulltorefresh_baseAdapter pullAdapter;
//定义向Adaptger中传值的数据
private List<String> listDate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_defined_list__pulltore_fresh);
//初始化数据
listDate = new ArrayList<String>();
//初始化值
num = 0;
//循环给数据赋值
for(int i=0;i<mainStr.length;i++)
{
listDate.add(mainStr[i]);
}
//初始化下拉列表控件
userDefinedList_pull_refresh_list = (PullToRefreshListView)this.findViewById(R.id.userDefinedList_pull_refresh_list);
userDefinedList_pull_refresh_list.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
String twoInfo = DateUtils.formatDateTime(getApplicationContext(),System.currentTimeMillis(),
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
//下拉时,显示的主要文字,一般的情况下,我们不修改它
refreshView.getLoadingLayoutProxy().setReleaseLabel("放手开始加载数据");
//下拉时,第二行显示的文字
refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(twoInfo);
//数据刷新的时候,显示的文字
refreshView.getLoadingLayoutProxy().setRefreshingLabel("数据加载中......");
//刷新结束后,显示的文字,没有什么意义,一间而过
refreshView.getLoadingLayoutProxy().setPullLabel("数据加载完成!");
//调用异步,开始加载
new GetDataTask().execute();
}
});
//设置底部下拉刷新模式
userDefinedList_pull_refresh_list.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
pullAdapter = new pulltorefresh_baseAdapter(this,listDate);
//设置适配器
ListView actualListView = userDefinedList_pull_refresh_list.getRefreshableView();
actualListView.setAdapter(pullAdapter);
}
private class GetDataTask extends AsyncTask<Void,Void,String>{
/**
* 此处进行异步操作
*/
@Override
protected String doInBackground(Void... params) {
//让程序休眠一秒钟,以达到延时的效果
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Toast.makeText(userDefinedList_PulltoreFresh.this,e.getMessage(),Toast.LENGTH_LONG).show();
}
//定义一个变量,用来存取要添加的值,当num值大于新加数据的个数时,就不再给值,而是返回空
String str = "";
if(num!=strArray.length)
{
str = strArray[num];
num++;
}
return str;
}
/**
* 这里是异步操作完,返回的结果,用来修改UI
* @param backStr 异步返回的结果
*/
@Override
protected void onPostExecute(String backStr) {
//判断一下,传过来的值是否为空,如是不为空则进行添加
if(!backStr.equals("")) {
listDate.add(backStr);
}
pullAdapter.notifyDataSetChanged();
userDefinedList_pull_refresh_list.onRefreshComplete();
super.onPostExecute(backStr);
}
}
}
4,Adapter文件(pulltorefresh_baseAdapter.java)
package com.example.cg.baseAdapter;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.cg.studiotest.R;
import java.util.List;
/**
* Created by cg on 2015/1/27.
*/
public class pulltorefresh_baseAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private List<String> testDate;
public pulltorefresh_baseAdapter(Context context,List<String> testDate)
{
this.mInflater = LayoutInflater.from(context);
this.testDate = testDate;
}
@Override
public int getCount() {
return testDate.size();
}
@Override
public Object getItem(int position) {
return testDate.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
pulltoreItem item;
if(convertView==null)
{
item = new pulltoreItem();
convertView = mInflater.inflate(R.layout.pulltorefresh_item, null);
item.txt_pulltorefresh_item_title = (TextView)convertView.findViewById(R.id.txt_pulltorefresh_item_title);
item.img_pulltorefresh_item_img = (ImageView)convertView.findViewById(R.id.img_pulltorefresh_item_img);
convertView.setTag(item);
}
else
{
item = (pulltoreItem)convertView.getTag();
}
item.txt_pulltorefresh_item_title.setText(testDate.get(position));
return convertView;
}
public class pulltoreItem
{
TextView txt_pulltorefresh_item_title;
ImageView img_pulltorefresh_item_img;
}
}
5,效果图