Load item in second fragment when first list item loaded

不想你离开。 提交于 2019-12-13 02:25:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!