先到githhub找到chrisbanes/Android-pulltorefresh.(支持的组件多,也支持上下刷新。)注意v4包的导入问题。
下载地址:https://github.com/chrisbanes/Android-PullToRefresh
遇到问题1:设置ptr属性,没代码提示。我是到library直接复制。(谁有好点方法?)
2:设置涮新模式为both的setonRefreshListener(new onRefresh2)时没显示方法。
布局代码:
- <com.handmark.pulltorefresh.library.PullToRefreshListView
- android:id="@+id/pull_to_refresh_listview"
- android:layout_height="fill_parent"
- android:layout_width="fill_parent" />
MainActivity源码:
- package com.mpulltorefreshdemo;
- import java.util.Arrays;
- import java.util.LinkedList;
- import android.app.Activity;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.text.format.DateUtils;
- import android.view.Menu;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
- import com.handmark.pulltorefresh.library.PullToRefreshBase;
- import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
- import com.handmark.pulltorefresh.library.PullToRefreshListView;
- public class MainActivity extends Activity {
- private PullToRefreshListView mPullToRefreshListView;
- private LinkedList<String> mListItems;
- private ArrayAdapter<String> mAdapter;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- // Set a listener to be invoked when the list should be refreshed.
- mPullToRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_to_refresh_listview);
- mPullToRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
- //我用onRefreshListener2时没有下面的onRefresf方法。
- @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();
- }
- });
- ListView actualListView = mPullToRefreshListView.getRefreshableView();
- mListItems = new LinkedList<String>();
- mListItems.addAll(Arrays.asList(mStrings));
- mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListItems);
- actualListView.setAdapter(mAdapter);
- }
- private class GetDataTask extends AsyncTask<Void, Void, String[]> {
- @Override
- protected String[] doInBackground(Void... params) {
- // Simulates a background job.
- try {
- Thread.sleep(4000);
- } catch (InterruptedException e) {
- }
- return mStrings;
- }
- @Override
- protected void onPostExecute(String[] result) {
- mListItems.addFirst("Added after refresh...");
- mAdapter.notifyDataSetChanged();
- // Call onRefreshComplete when the list has been refreshed.
- mPullToRefreshListView.onRefreshComplete();
- super.onPostExecute(result);
- }
- }
- @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;
- }
- private String[] mStrings = { "John", "Michelle", "Amy", "Kim", "Mary",
- "David", "Sunny", "James", "Maria", "Michael", "Sarah", "Robert",
- "Lily", "William", "Jessica", "Paul", "Crystal", "Peter",
- "Jennifer", "George", "Rachel", "Thomas", "Lisa", "Daniel", "Elizabeth",
- "Kevin" };
- }
- <com.handmark.pulltorefresh.library.PullToRefreshListView
- android:id="@+id/pull_to_refresh_listview"
- android:layout_height="fill_parent"
- android:layout_width="fill_parent" />