去github上面下载pulltorefresh这个library或者去我的网盘链接:http://pan.baidu.com/s/1brSfBw 密码:ob9t 下载这个zip包,然后解压,在你的项目中添加这个包里面的library,PulltorefreshListFragment,PullToRefreshViewPager 这样三个工程,当然你如果想看到示例代码也可以添加LauncherActivity这个工程,
2.示例代码在示例代码中我们可以看到PullToRefreshListActivity中只有下拉刷新的效果,而PullToRefreshGridActivity中且有上拉刷新的效果,这样对比代码来看,在List里面我们需要在布局文件的添加 ptr:ptrMode="both"
这样的一个属性,就是支持上拉和下拉刷星的属性,当然添加这个属性还要加入xmlns:ptr="http://schemas.android.com/apk/res-auto"
添加这样的一个属性,这样就可以实现上拉和下拉刷新的效果了,不过如果你在List里面不对setOnRefreshListener做改变的话,上拉和下拉刷新的效果是一样的,那么,我们就来研究一下setOnRefreshListener,setOnRefreshListener参数里面有一个实现接口的方法,这个接口有两种,一种是new OnRefreshListener<ListView>()
另一种是new OnRefreshListener2<ListView>()
。要想实现 上拉和下拉刷新效果不一样就要使用第二种。并且要实现里面的
@Override
public void onPullDownToRefresh(
PullToRefreshBase<ListView> refreshView) {
new GetDataTask().execute();
}
@Override
public void onPullUpToRefresh(
PullToRefreshBase<ListView> refreshView) {
new GetDataTask2().execute();
}
这两个方法。那么也要实现GetDataTask()和GetDataTask2()这两个类,表示上拉刷新更新的方式 和下拉刷新更新的方式。那么我就把整个代码附上来。小伙伴们看看吧。
package com.example.pulltorefreshtest;
import java.util.ArrayList;
import java.util.LinkedList;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import android.os.AsyncTask;
import android.os.Bundle;
import android.R.integer;
import android.app.Activity;
import android.text.format.DateUtils;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
private PullToRefreshListView mpullToRefreshListView;
private ArrayAdapter<String>mArrayAdapter;
private LinkedList mArrayList = new LinkedList<String>();;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mpullToRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
mArrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1, mArrayList);
mpullToRefreshListView.setAdapter(mArrayAdapter);
mpullToRefreshListView.setOnRefreshListener(new OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(
PullToRefreshBase<ListView> refreshView) {
new GetDataTask().execute();
}
@Override
public void onPullUpToRefresh(
PullToRefreshBase<ListView> refreshView) {
new GetDataTask2().execute();
}
/*@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
refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
// Do work to refresh the list here.
new GetDataTask().execute();
}*/
});
}
private class GetDataTask extends AsyncTask<Void, Void, String []> {
@Override
protected String[] doInBackground(Void... params) {
// Simulates a background job.
return mStrings;
}
@Override
protected void onPostExecute(String[] result) {
mArrayList.addFirst(result[0]);
mArrayAdapter.notifyDataSetChanged();
// Call onRefreshComplete when the list has been refreshed.
mpullToRefreshListView.onRefreshComplete();
super.onPostExecute(result);
}
}
private String[] mStrings = { "Abbaye de Belloc"};
private class GetDataTask2 extends AsyncTask<Void, Void, String []> {
@Override
protected String[] doInBackground(Void... params) {
// Simulates a background job.
return mStrings2;
}
@Override
protected void onPostExecute(String[] result) {
mArrayList.addFirst(result[0]);
mArrayAdapter.notifyDataSetChanged();
// Call onRefreshComplete when the list has been refreshed.
mpullToRefreshListView.onRefreshComplete();
super.onPostExecute(result);
}
}
private String[] mStrings2 = { "hahahha"};
}
布局文件为
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- The PullToRefreshListView replaces a standard ListView widget. -->
<com.handmark.pulltorefresh.library.PullToRefreshListView
xmlns:ptr="http://schemas.android.com/apk/res-auto"
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"
ptr:ptrMode="both"
ptr:ptrDrawable="@drawable/lottery"
/>
</LinearLayout>
共同学习,写下你的评论
评论加载中...
作者其他优质文章