自分のアプリに組み込んでみる
サンプルはサクっと動いたので、今度は自分のテストアプリに組み込んでみる。
テストアプリは今までリストビューにセルを加えたり削除したりしていた下記のアプリケーション。
今回はゴミ箱アイコンでセルが削除されて、[ADD]メニューで一番上に [Cell No.10000] が加えられる。
そして、引っ張ったら 3秒待ってから各セルの数字を +100するという更新機能を PullToRefresh で加えることにする。
と、言ってもやることは簡単。
Layout の activity_main.xml はこんな感じに。
<FrameLayout 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" tools:context=".MainActivity" > <com.handmark.pulltorefresh.library.PullToRefreshListView android:id="@+id/chainListView" android:layout_width="match_parent" android:layout_height="match_parent" /> <ImageView android:id="@+id/bitmapImageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ffececec" /> </FrameLayout>
MainActivity はこんな感じのコードを加えた。
mListView = (PullToRefreshListView) findViewById(R.id.chainListView); mListView.setOnRefreshListener(new OnRefreshListener<ListView>() { @Override public void onRefresh(PullToRefreshBase<ListView> refreshView) { // TODO Auto-generated method stub Timer timer = new Timer(true); timer.schedule( new TimerTask() { @Override public void run() { mMainHandler.post( new Runnable() { public void run() { mListView.onRefreshComplete(); for (MyCell cell : mAnimList) { cell.index = cell.index + 100; } mMyAnimListAdapter.notifyDataSetChanged(); } }); } }, REFRESH_DURATION); } });
細かい部分はもう少し手を入れているけど、詳細が気になる方は GitHub で確認してください。
これでサックリ引っ張って更新機能も実装できました!
というわけで、このテストアプリはまた GitHub に上げておきます。