package com.example.mypulltorefreshdemo;
import java.util.ArrayList;
import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
ArrayList<String> dataList = new ArrayList<String>();
private PullToRefreshListView pull_refresh_list;
private ArrayAdapter<String> arrayAdapter;
private int index = 0;
private int TIME_COUNT = 20;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pull_refresh_list = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
// Set a listener to be invoked when the list should be refreshed.
pull_refresh_list
.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
refreshView.getLoadingLayoutProxy()
.setLastUpdatedLabel(label);
// Do work to refresh the list here.
new GetDataTask().execute();
}
});
// Add an end-of-list listener
pull_refresh_list
.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
@Override
public void onLastItemVisible() {
if (index < 100) {
index = index + TIME_COUNT;
initData();
arrayAdapter.notifyDataSetChanged();
} else {
// 做上拉加载的操作
Toast.makeText(MainActivity.this, "End of List!",
Toast.LENGTH_SHORT).show();
}
}
});
initData();
ListView actualListView = pull_refresh_list.getRefreshableView();
arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1,
dataList);
actualListView.setAdapter(arrayAdapter);
}
class GetDataTask extends AsyncTask<Void, Void, ArrayList<String>> {
@Override
protected ArrayList<String> doInBackground(Void... arg0) {
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return dataList;
}
@Override
protected void onPostExecute(ArrayList<String> result) {
// mListItems.addFirst("Added after refresh...");
index = 0;
dataList.clear();
initData();
arrayAdapter.notifyDataSetChanged();
// Call onRefreshComplete when the list has been refreshed.
pull_refresh_list.onRefreshComplete();
super.onPostExecute(result);
}
}
private void initData() {
for (int i = index; i < index + TIME_COUNT; i++) {
dataList.add("我是第" + i + "条目");
}
}
}
import java.util.ArrayList;
import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
//需要第三方lib包,
ArrayList<String> dataList = new ArrayList<String>();
private PullToRefreshListView pull_refresh_list;
private ArrayAdapter<String> arrayAdapter;
private int index = 0;
private int TIME_COUNT = 20;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pull_refresh_list = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
// Set a listener to be invoked when the list should be refreshed.
pull_refresh_list
.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
refreshView.getLoadingLayoutProxy()
.setLastUpdatedLabel(label);
// Do work to refresh the list here.
new GetDataTask().execute();
}
});
// Add an end-of-list listener
pull_refresh_list
.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
@Override
public void onLastItemVisible() {
if (index < 100) {
index = index + TIME_COUNT;
initData();
arrayAdapter.notifyDataSetChanged();
} else {
// 做上拉加载的操作
Toast.makeText(MainActivity.this, "End of List!",
Toast.LENGTH_SHORT).show();
}
}
});
initData();
ListView actualListView = pull_refresh_list.getRefreshableView();
arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1,
dataList);
actualListView.setAdapter(arrayAdapter);
}
class GetDataTask extends AsyncTask<Void, Void, ArrayList<String>> {
@Override
protected ArrayList<String> doInBackground(Void... arg0) {
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return dataList;
}
@Override
protected void onPostExecute(ArrayList<String> result) {
// mListItems.addFirst("Added after refresh...");
index = 0;
dataList.clear();
initData();
arrayAdapter.notifyDataSetChanged();
// Call onRefreshComplete when the list has been refreshed.
pull_refresh_list.onRefreshComplete();
super.onPostExecute(result);
}
}
private void initData() {
for (int i = index; i < index + TIME_COUNT; i++) {
dataList.add("我是第" + i + "条目");
}
}
}