ListFragment に終わりのないリスト (アクティビティ用に実装された既存のコードに基づく) を実装しようとしています。
次のコードを含む ListFragment があります。
public class MainFragment extends ListFragment {
//how many to load on reaching the bottom
int itemsPerPage = 15;
boolean loadingMore = false;
ArrayList<String> myListItems;
ArrayAdapter<String> adapter;
ListView listview;
//For test data :-)
Calendar d = Calendar.getInstance();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("Test", "Create Stories Fragment");
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.listplaceholder, container, false);
listview = (ListView) view.findViewById(android.R.id.list);
//This will hold the new items
myListItems = new ArrayList<String>();
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, myListItems);
//add the footer before adding the adapter, else the footer will nod load!
View footerView = ((LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.listfooter, null, false);
listview.addFooterView(footerView);
//Set the adapter
listview.setAdapter(adapter);
//Here is where the magic happens
listview.setOnScrollListener(new OnScrollListener(){
//useless here, skip!
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {}
//dumdumdum
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
//what is the bottom iten that is visible
int lastInScreen = firstVisibleItem + visibleItemCount;
//is the bottom item visible & not loading more already ? Load more !
if((lastInScreen == totalItemCount) && !(loadingMore)){
Thread thread = new Thread(null, loadMoreListItems);
thread.start();
}
}
});
//Load the first 15 items
Log.e("Test", "Load first items");
Thread thread = new Thread(null, loadMoreListItems);
thread.start();
return view;
}
//Runnable to load the items
private Runnable loadMoreListItems = new Runnable() {
@Override
public void run() {
//Set flag so we cant load new items 2 at the same time
loadingMore = true;
//Reset the array that holds the new items
myListItems = new ArrayList<String>();
Log.e("Test", "simulate delay");
//Simulate a delay, delete this on a production environment!
try { Thread.sleep(1000);
} catch (InterruptedException e) {}
//Get 15 new listitems
for (int i = 0; i < itemsPerPage; i++) {
//Fill the item with some bogus information
myListItems.add("Date: " + (d.get(Calendar.MONTH)+ 1) + "/" + d.get(Calendar.DATE) + "/" + d.get(Calendar.YEAR) );
Log.e("Test", "Date: " + (d.get(Calendar.MONTH)+ 1) + "/" + d.get(Calendar.DATE) + "/" + d.get(Calendar.YEAR));
// +1 day :-D
d.add(Calendar.DATE, 1);
}
//Done! now continue on the UI thread
getActivity().runOnUiThread(returnRes);
}
};
//Since we cant update our UI from a thread this Runnable takes care of that!
private Runnable returnRes = new Runnable() {
@Override
public void run() {
Log.e("Test", "returnRes");
//Loop thru the new items and add them to the adapter
if(myListItems != null && myListItems.size() > 0){
for(int i=0;i<myListItems.size();i++)
{
Log.e("Test", "adding " + myListItems.get(i));
adapter.add(myListItems.get(i));
}
}
Log.e("Test", "notifyDataSetChanged");
//Tell to the adapter that changes have been made, this will cause the list to refresh
adapter.notifyDataSetChanged();
//Done loading more.
loadingMore = false;
}
};
}
さて、listplaceholder.xml はシンプルで、元のコードと同じです。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fastScrollEnabled="true"
android:drawSelectorOnTop="false" />
<TextView
android:id="@id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="Loading data.."/>
</LinearLayout>
問題は、元のコードでは、下にスクロールすると日付が更新されているのを確認できますが、私のコードでは、「Loading Data ..」というフッター メッセージしか表示されないことです。リストが更新されないのはなぜですか?