pulltorefreshの使用
Androidではlistviewのドロップダウン・リフレッシュが必要な場合があり、自分で書くのは面倒で時間がかかる場合があるので、オープンソースのpulltorefreshを直接利用してコンポーネントをリフレッシュすることができます
ダウンロードリンク:https://github.com/chrisbanes/Android-PullToRefresh
解凍後、libraryフォルダをeclipseに導入し、新しいプロジェクトで右クリック---->properties---->android------>add追加前に導入したlibrary
カスタムレイアウトは以下の通りです.list_item.xml
Activity_main.xmlレイアウトにlibraryのリフレッシュコンポーネントを導入し、activity_main.xmlは次のとおりです.
MainActivity.JAvaは以下の通りです.
ダウンロードリンク:https://github.com/chrisbanes/Android-PullToRefresh
解凍後、libraryフォルダをeclipseに導入し、新しいプロジェクトで右クリック---->properties---->android------>add追加前に導入したlibrary
カスタムレイアウトは以下の通りです.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="horizontal" >
<ImageView
android:id="@+id/imageid"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/textid"
android:layout_weight="4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
/>
</LinearLayout>
Activity_main.xmlレイアウトにlibraryのリフレッシュコンポーネントを導入し、activity_main.xmlは次のとおりです.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- The PullToRefreshListView replaces a standard ListView widget. -->
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/pull_refresh_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:divider="#19000000"
android:dividerHeight="4dp"
android:fadingEdge="none"
android:fastScrollEnabled="false"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:smoothScrollbar="true" />
</LinearLayout>
MainActivity.JAvaは以下の通りです.
package com.example.pulltorefresh;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.database.DataSetObserver;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class MainActivity extends Activity {
private List<Map<String,String>>list = new ArrayList<Map<String,String>>();
private PullToRefreshListView pullView;
private String[]texts = new String[]{"aaaa","bbbb","cccc","dddd","eeee","ffff","gggg"};
private int[] images = new int[]{R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher
,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};
SimpleAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initList();
pullView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
// set mode to BOTH
pullView.setMode(Mode.PULL_UP_TO_REFRESH);
pullView.getLoadingLayoutProxy(false, true).setPullLabel(getString(R.string.pull_to_load));
pullView.getLoadingLayoutProxy(false, true).setRefreshingLabel(getString(R.string.loading));
pullView.getLoadingLayoutProxy(false, true).setReleaseLabel(getString(R.string.release_to_load));
//
adapter = new SimpleAdapter(this, list,R.layout.list_item,new String[]{"imageid","textid"},new int[]{R.id.imageid,R.id.textid});
ListView actualListView = pullView.getRefreshableView();
actualListView.setAdapter(adapter);
pullView.setOnRefreshListener(new OnRefreshListener<ListView>() {
@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
// TODO Auto-generated method stub
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);
new GetDataTask().execute(new Void[]{});
}
});
}
// list
private void initList() {
// TODO Auto-generated method stub
for (int i = 0; i < texts.length; i++) {
Map<String,String>map = new HashMap<String, String>();
map.put("imageid",images[i]+"");
map.put("textid",texts[i]);
list.add(map);
}
}
public class GetDataTask extends AsyncTask<Void,Void,Void> {
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int i = 0; i < 2; i++) {//
Map<String,String>map = new HashMap<String, String>();
map.put("imageid",R.drawable.ic_launcher+"");
map.put("textid","fresh data"+i);
list.add(map);
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
adapter.notifyDataSetChanged();
pullView.onRefreshComplete();
super.onPostExecute(result);
}
}
}