Android ListView的上拉刷新,下拉加载功能(pull - to -refesh)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010046908/article/details/47749503

        最近研究了一下Android ListView的上拉刷新,下拉加载功能,采用了目前比较火的PullToRefresh,他是目前实现比较好的下拉刷新的类库。目前他支持的控件有:ListView, ExpandableListView,GridView,WebView等。把自己的学习过程记录一下:

步骤:

1.从github下载:Android-PullToRefresh的库文件,导入到项目中(下载地址:https://github.com/chrisbanes/Android-PullToRefresh)。

2、要实现下拉刷新的功能很简单,


下面是个例子采用的是ListView,

1、布局文件activity_main.xml

01 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
02     xmlns:tools="http://schemas.android.com/tools"
03     android:layout_width="match_parent"
04     android:layout_height="match_parent"
05     tools:context=".MainActivity" >
06  
07       <com.handmark.pulltorefresh.library.PullToRefreshListView
08         android:id="@+id/pull_refresh_list"
09         android:layout_width="fill_parent"
10         android:layout_height="fill_parent" />
11 </RelativeLayout>
2.Activity中的设置:

package com.handmark.pulltorefresh.samples;


import java.util.Arrays;
import java.util.LinkedList;


import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;


import android.app.ListActivity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;


public class MainActivityLoadMore extends ListActivity {


private PullToRefreshListView mPullToRefreshListView;

private LinkedList<String> mItemList;
private ArrayAdapter<String> adapter;

private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
initData();//初始化数据

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

//初始化控件
mPullToRefreshListView = (PullToRefreshListView)findViewById(R.id.pull_refresh_list);
ListView mListView = mPullToRefreshListView.getRefreshableView();
mListView.setAdapter(adapter);

//设置pull-to-refresh模式为Mode.Both
mPullToRefreshListView.setMode(Mode.BOTH);//设置为双向的

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


@Override
public void onPullDownToRefresh(//下拉时回调的方法
PullToRefreshBase<ListView> refreshView) {

Toast.makeText(MainActivityLoadMore.this, "Pull Down!", Toast.LENGTH_SHORT).show();
new GetDataTask().execute();
}


@Override
public void onPullUpToRefresh(//上拉时回调的方法
PullToRefreshBase<ListView> refreshView) {
Toast.makeText(MainActivityLoadMore.this, "Pull Up!", Toast.LENGTH_SHORT).show();
new GetDataTask().execute();
}



});

mPullToRefreshListView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {


@Override
public void onLastItemVisible() {
}
});
}




private void initData(){
//初始化数据
mItemList = new LinkedList<String>();
mItemList.addAll(Arrays.asList(data));

}

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


@Override
protected String[] doInBackground(Void... params) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
String[] data  = getData();
return data;
}


@Override
protected void onPostExecute(String[] result) {
mItemList.addAll(Arrays.asList(result));
adapter.notifyDataSetChanged();


// Call onRefreshComplete when the list has been refreshed.
//当list已经完成刷新,调用该方法
mPullToRefreshListView.onRefreshComplete();


super.onPostExecute(result);
}
}


private String[] data  = new String[]{"data1","data2","data3","data4","data5","data6",
"data1","data2","data3","data4","data5","data6"};


    public String[]  getData() {
    String[]  mItem = new String[10];
    for (int i = 0; i < 10; i++) {
    mItem[i]="object"+i+"";
}
return mItem;
}
 }


效果图:Android ListView的上拉刷新,下拉加载功能(pull - to -refesh)

上一篇:Android中实现短信验证码自动填入


下一篇:关于com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536的解决方法