加载第一个列表项时,在第二个片段中加载项 [英] Load item in second fragment when first list item loaded

查看:81
本文介绍了加载第一个列表项时,在第二个片段中加载项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建与 Android Fragments教程类似的内容:左侧有一个ListView片段,右侧有一个可以显示项目的片段.

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.

现在,我正在尝试通过SimpleCursorAdapter(我通过LoaderManager使用)加载列表中的第一项(或之后不久)自动选择列表中的第一项-全部使用Android支持库.

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.

无论如何我都需要实现一个onLoadFinished,但这对我来说太晚了,因为我正在加载一个很长的列表.因此,我一直在考虑onItemLoadedListener的用法,我只会在第一次调用它时使用它.但是似乎没有那样的东西存在,所以我很困惑.

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.

感谢任何指针!

推荐答案

您反对什么太简单了?

 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.

这篇关于加载第一个列表项时,在第二个片段中加载项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆