gpt4 book ai didi

android - 在 Android 中从 ListView 切换到子 ListView

转载 作者:行者123 更新时间:2023-11-30 04:06:08 26 4
gpt4 key购买 nike

我有一个应用程序,其中 MainActivity 创建了一个带有 tabsActionBar(为此使用了 ActionBarSherlock) .

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

当一个项目被点击时,应用程序会从网络服务器获取数据。我想在新的 ListView 中显示该数据。但这就是我被困的地方。

如何从我的第一个主 ListView 切换到“sub-ListView”?

请记住,我当前的 View 是一个 fragment ,它加载在一个选项卡内。当我显示新的“子”ListView 时,我想保持在同一个选项卡上。

此外,当我在 Android 设备上按下后退 按钮 时,我希望它再次显示主 ListView

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

最佳答案

您必须使用另一个 Fragment 才能做到这一点。您需要创建该 Fragment,然后用 FragmentTransaction 替换旧的,如果您想使用“后退”按钮,您需要将新 fragment 添加到 返回堆栈。如果这些 ListFragment 在数据方面有很大不同,我将有 2 个 fragment 类(例如: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 布局中定义(除非您以编程方式创建布局)。它只是您的默认 fragment 的 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);

关于android - 在 Android 中从 ListView 切换到子 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11629985/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com