主要用到 Android-PullToRefresh
地址:https://github.com/chrisbanes/Android-PullToRefresh
一、MainActivity
package com.fengshupo.mypackage;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button btnListView;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnListView=(Button)findViewById(R.id.btnListView);
btnListView.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view) {
intent=new Intent(MainActivity.this,PullToRefreshListActivity.class);
startActivity(intent);
}});
}
@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;
}
}
二、PullToRefreshListActivity
package com.fengshupo.mypackage;
import java.util.Arrays;
import java.util.LinkedList;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
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;
public class PullToRefreshListActivity extends ListActivity {
private LinkedList<String> items;
private ArrayAdapter<String> adapter;
private String[] names={"周星星","刘诗诗"};
private PullToRefreshListView mPullRefreshListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ptr_list);
mPullRefreshListView=(PullToRefreshListView)findViewById(R.id.pull_refresh_list);
mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>(){
@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
String msg=DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),
DateUtils.FORMAT_SHOW_YEAR|DateUtils.FORMAT_SHOW_DATE|DateUtils.FORMAT_SHOW_TIME|DateUtils.FORMAT_SHOW_DATE);
refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(msg);
new GetDataTask().execute();
}});
mPullRefreshListView.setOnLastItemVisibleListener(new OnLastItemVisibleListener(){
@Override
public void onLastItemVisible() {
Toast.makeText(PullToRefreshListActivity.this, "已经是末尾", Toast.LENGTH_SHORT).show();
}});
ListView actualListView = mPullRefreshListView.getRefreshableView();
items=new LinkedList<String>();
items.addAll(Arrays.asList(names));
adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,items);
actualListView.setAdapter(adapter);
}
private class GetDataTask extends AsyncTask<Void, Void, String[]> {
@Override
protected String[] doInBackground(Void... params) {
// Simulates a background job.
try {
items.addFirst(System.currentTimeMillis()+"");
Thread.sleep(4000);
} catch (InterruptedException e) {
}
return names;
}
@Override
protected void onPostExecute(String[] result) {
items.addFirst("-------这是更新的内容-------");
adapter.notifyDataSetChanged();
// Call onRefreshComplete when the list has been refreshed.
mPullRefreshListView.onRefreshComplete();
super.onPostExecute(result);
}
}
}
三、效果图