Layoutの作成
レイアウトはandroid.support.v4.widget.SwipeRefreshLayoutを使用します。コンテンツはSwipeRefreshLayoutの子Viewとして記述します。
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_refresh_widget"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- some full screen pullable view that will be the offsetable content -->
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/content"/>
</android.support.v4.widget.SwipeRefreshLayout>
Swipeによる更新
Swipeによる更新は次のように行います。1. 更新開始タイミングをハンドルするために、setOnRefreshListenerメソッドにリスナーをセット
2. onRefresh()で更新開始をハンドリング
3. 更新処理が終わったら、setRefreshing(false)をコールする
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample_swipe_refresh_widget);
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_widget);
mSwipeRefreshLayout.setOnRefreshListener(this);
}
@Override
public void onRefresh() {
refresh();
}
private void refresh() {
Message msg = mHander.obtainMessage(0, this);
mHander.sendMessageDelayed(msg, 1000);
}
private static Handler mHander = new Handler() {
@Override
public void handleMessage(Message msg) {
SwipeRefreshLayoutActivity activity = (SwipeRefreshLayoutActivity) msg.obj;
activity.mSwipeRefreshLayout.setRefreshing(false);
}
};
0 件のコメント:
コメントを投稿