问题
I am trying to build something similar to the Android Fragments tutorial: I have a ListView fragment on the left and a fragment that can display an item on the right.
Now I am trying to auto-select the first item in the list as soon (or soon after) it has been loaded through the SimpleCursorAdapter (which I use through the LoaderManager)-- all using the Android support library.
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mAdapter = new SimpleCursorAdapter(/* options */);
setListAdapter(mAdapter);
}
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
CursorLoader cl = new CursorLoader(getActivity());
/* setProjection, etc */
return cl;
}
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mAdapter.swapCursor(data);
}
public void onLoaderReset(Loader<Cursor> loader) {
mAdapter.swapCursor(null);
}
I have checked various sample implementations and they all seem to be much too simple or simply don't populate the fragment on the right on load but just upon a click.
I needed to implement a onLoadFinished anyway but that is too late for me as I am loading a long list. So I was thinking along the lines of an onItemLoadedListener which I would only use the first time it is called. But nothing like that seems to exist so I am quite stuck.
Thanks for any pointers!
回答1:
what you have against much too simple?
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
data.moveToFirst();
Bundle b = new Bundle();
// get info from that first item and put into the bundle
...
ContentFrag f = new ContentFrag();
f.setArguments(b);
getSupportFragmentManager().beginTransaction().replace(R.id.contentLayout, f).commit();
// instantiate and set your adapter
...
}
simple! Just like any other fragment transaction.
来源:https://stackoverflow.com/questions/13294666/load-item-in-second-fragment-when-first-list-item-loaded