android標準機能の引っ張って更新

2014年6月10日火曜日

4.1 android 開発

t f B! P L
外部のライブラリを使用せずに、リストを引っ張って更新する処理です。
使用する条件として、
・Android SDKExtras->android Support Libraryが19.1.0以上
・android-support-v4.jarが、android Support Libraryが19.1.0以上になった後
 に作成されたもの。
 ※自分は、jarが古くてハマってました。
  なお、更新する場合は新規にプロジェクトを作成などして、そこのandroid-support-v4.jarと
  入れ替えればOK

で、コピペできそうなソース
まずはxmlから
#########################################################################
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_refresh_widget"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            />
    </android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
#########################################################################


ソース
#########################################################################
public class test extends ListActivity implements OnRefreshListener
{
private SwipeRefreshLayout mSwipeRefreshLayout;

@Override
public void onCreate(Bundle savedInstanceState)
{
// 更新処理の初期化
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_widget);
// 更新中のアニメーション色設定
mSwipeRefreshLayout.setColorScheme(
android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
mSwipeRefreshLayout.setOnRefreshListener(this);

// アニメーション実施
mSwipeRefreshLayout.setRefreshing(true);
}

// 引っ張って離すとonRefreshが実施される
@Override
public void onRefresh()
{
// 更新処理を記載
xxxxxxxxxxxxxxxxxxx


// 更新アニメーションを停止
mSwipeRefreshLayout.setRefreshing(false);
}
}
#########################################################################

QooQ