我尝试在 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.." 。为什么列表没有更新?