設置Listview
layout文件中定義PullToRefreshListView
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
設置Mode
PullToRefreshListView mListView = (PullToRefreshListView) findViewById(R.id.list_view);
mListView.setMode(Mode.BOTH);
- Mode.BOTH:同時支持上拉下拉
- Mode.PULL_FROM_START:只支持下拉Pulling Down
- Mode.PULL_FROM_END:只支持上拉Pulling Up
關於模式的說明和解釋
- 如果Mode設置成Mode.BOTH,需要設置刷新Listener爲OnRefreshListener2,並實現onPullDownToRefresh()、onPullUpToRefresh()兩個方法。
- 如果Mode設置成Mode.PULL_FROM_START或Mode.PULL_FROM_END,需要設置刷新Listener爲OnRefreshListener,同時實現onRefresh()方法。當然也可以設置爲OnRefreshListener2,但是Mode.PULL_FROM_START的時候只調用onPullDownToRefresh()方法,Mode.PULL_FROM的時候只調用onPullUpToRefresh()方法.
MainActivity .java
public class MainActivity extends Activity {
private ArrayList<String> mListItems;
private PullToRefreshListView mListView;
private SampleListAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample);
// 設置初期數據
mListItems = new ArrayList<String>();
for (int i = 1; i <= 10; i++) {
mListItems.add("Item " + Integer.toString(i));
}
// 設置ListView
mListView = (PullToRefreshListView) findViewById(R.id.list_view);
mAdapter = new SampleListAdapter();
mListView.setAdapter(mAdapter);
// 設置PullToRefresh
mListView.setMode(Mode.BOTH);
mListView.setOnRefreshListener(new OnRefreshListener2<ListView>(){
// 下拉Pulling Down
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
// 下拉的時候數據重置
mListItems = new ArrayList<String>();
for (int i = 1; i <= 10; i++) {
mListItems.add("Item " + Integer.toString(i));
}
mAdapter.notifyDataSetChanged();
new FinishRefresh().execute();
}
// 上拉Pulling Up
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
// 上拉的時候添加選項
int count = mListItems.size();
mListItems.add("Item " + Integer.toString(++count));
mAdapter.notifyDataSetChanged();
new FinishRefresh().execute();
}
});
}
private class SampleListAdapter extends BaseAdapter {
@Override
public int getCount() {
return mListItems.size();
}
@Override
public Object getItem(int index) {
return mListItems.get(index);
}
@Override
public long getItemId(int index) {
return index;
}
@Override
public View getView(int index, View view, ViewGroup arg2) {
if(view == null){
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.list_item, null);
}
TextView textView = (TextView)view.findViewById(R.id.listItemText);
textView.setText(mListItems.get(index));
return view;
}
}
private class FinishRefresh extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... params) {
return null;
}
@Override
protected void onPostExecute(Void result){
mListView.onRefreshComplete();
}
}
}
sample.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/mylv"
android:layout_height="match_parent"
android:layout_width="match_parent">
</com.handmark.pulltorefresh.library.PullToRefreshListView>
</LinearLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/text_item"
android:layout_width="match_parent"
android:layout_height="match_parent"></TextView>
</LinearLayout>