私はあなたがこのライブラリを使用していると思います...
https://github.com/chrisbanes/Android-PullToRefresh
それをダウンロードして、以下をインポートし、プロジェクトの依存関係として追加する必要があります。
PullToRefreshListFragment ライブラリ
コメントのリンクからダウンロードしたプロジェクトを変更します。ここからダウンロードできます。
https://dl.dropboxusercontent.com/u/33565803/StackOverFlowExamples/chrisbanes%20-%20Android-PullToRefresh-master-plusHeader.zip
私もコードを書くつもりなので、この回答にこのリンクを依存しないでください;)。そのため、FragmentActivity から拡張された MainActivity を呼び出すクラスは 1 つだけです。
public class MainActivity extends FragmentActivity implements PullToRefreshBase.OnRefreshListener<ListView> {
FragmentManager fm = getSupportFragmentManager();
private PullToRefreshListFragment mPullRefreshListFragment;
private PullToRefreshListView mPullRefreshListView;
private ListView mActualListView;
private String[] mStrings = {"Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
"Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",
"Allgauer Emmentaler", "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
"Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",
"Allgauer Emmentaler"};
private LinkedList<String> mListItems;
private ArrayAdapter<String> mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// sliding menu implementation
/*final SlidingMenu menu = new SlidingMenu(this);
menu.setMode(SlidingMenu.LEFT);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menu.setShadowWidthRes(R.dimen.shadow_width);
menu.setShadowDrawable(R.drawable.shadow);
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
menu.setFadeDegree(0.35f);
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
menu.setMenu(R.layout.menu);*/
// event handlers come after slidingmenu
mPullRefreshListFragment = (PullToRefreshListFragment) getSupportFragmentManager().findFragmentById(R.id.frag_ptr_list);
//aq = new AQuery(getActivity(),mPullToRefreshListFragment.getView());
/*mPullRefreshListView.setOnScrollListener(new EndlessScrollListener();*/
mPullRefreshListView = mPullRefreshListFragment.getPullToRefreshListView();
// Set a listener to be invoked when the list should be refreshed.
mPullRefreshListView.setOnRefreshListener(this);
// You can also just use mPullRefreshListFragment.getListView()
mActualListView = mPullRefreshListView.getRefreshableView();
mListItems = new LinkedList<String>();
mListItems.addAll(Arrays.asList(mStrings));
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListItems);
View headerView = getLayoutInflater().inflate(R.layout.list_header, null);
mActualListView.addHeaderView(headerView);
mActualListView.setAdapter(mAdapter);
mPullRefreshListFragment.setListShown(true);
/*asyncJson(1,expire);*/
}
public void asyncJson(int page, final long expire) {
// GET JSON FROM THE WEB AND PARSE IT
// ALSO STORE DATA IN ARRAY LIST VARIABLES
if (expire == -1) {
mPullRefreshListView.onRefreshComplete();
}
//if(pg == 1 && expire !=-1){
//If you wanna add a header at this point, do it like this
View headerView = getLayoutInflater().inflate(R.layout.list_header, null);
mActualListView.addHeaderView(headerView);
// DO SOMETHING
//}
//Just notify the changes because the adapter do exist
mAdapter.notifyDataSetChanged();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
// Do work to refresh the list here.
new GetDataTask().execute();
}
private class GetDataTask extends AsyncTask<Void, Void, String[]> {
@Override
protected String[] doInBackground(Void... params) {
// Simulates a background job.
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
}
return mStrings;
}
@Override
protected void onPostExecute(String[] result) {
mListItems.addFirst("Added after refresh...");
mAdapter.notifyDataSetChanged();
// Call onRefreshComplete when the list has been refreshed.
mPullRefreshListView.onRefreshComplete();
super.onPostExecute(result);
}
}
}
これは私たちが持っているレイアウトです:
activity_main.xml
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<fragment
android:id="@+id/frag_ptr_list"
android:name="com.handmark.pulltorefresh.extras.listfragment.PullToRefreshListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
list_row.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
list_header.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#4a2">
<TextView
android:id="@+id/text"
android:gravity="center"
android:text="My Header"
android:textColor="#fff"
android:layout_width="match_parent"
android:layout_height="40dp"/>
</RelativeLayout>