1

我有一个应用程序,MainActivity其中创建了一个ActionBarwith tabs(用于ActionBarSherlock此)。

每个tab加载一个Fragment. 一个片段有一个ListView,我在其中加载项目。每个项目都响应一个OnItemClick事件。

单击项目时,应用程序会从网络服务器获取数据。我想在新的ListView. 但这就是我卡住的地方。

如何从我的第一个主视图切换ListView到“子列表视图”?

请记住,我当前的视图是一个片段,并且它加载在选项卡中。当我显示新的 'sub' 时,我想保持在同一个选项卡上ListView

此外,当我在 Android 设备上按下后部button时,我希望它ListView再次显示主要内容。

所以实际上,有点像 iPhone 用他们的TableView.

4

1 回答 1

1

你必须使用另一个Fragment来做到这一点。您需要创建它Fragment,然后用 替换旧的FragmentTransaction,如果要使用“后退”按钮,则需要将新片段添加到Backstack. 如果这些ListFragment在数据方面完全不同,我将有 2 个片段类(例如:FirstListFragment、SecondListFragment)。

这是我工作过的应用程序的一些代码:

    // Here I get the FragmentTransaction

    FragmentTransaction ft = getFragmentManager().beginTransaction();

    // I replace it with a new Fragment giving it data. Here you need to tell 
    // the FragmentTransaction what (or actually where) and by what you want to replace. 

    ft.replace(R.id.fraglist, new ModuleFragment(lpId, sqId, sqName));

R.id.fraglist 应该在您的 XML 布局中定义(除非您以编程方式创建布局)。它只是您的默认片段的 ID。

    // I set the animation

    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);

    // I add it to the backstack so I can use the back button

    ft.addToBackStack(null);

    // Then I finally commit so it happens !

    ft.commit();

您还可以使用 Bundle 来解析数据,如下所示:

    Bundle moduleBundle = new Bundle();
    moduleBundle.putString("id", id);
    moduleBundle.putString("description", description);
    moduleFragment.setArguments(moduleBundle);
于 2012-07-24T11:58:28.277 回答