gpt4 book ai didi

android - 选项卡和 fragment ,单独的返回堆栈

转载 作者:行者123 更新时间:2023-11-29 00:41:07 24 4
gpt4 key购买 nike

我正在尝试将选项卡添加到现有应用程序以添加更多功能我已经能够实现选项卡并将所有内容移动到 Fragments。但是,我目前设置它的方式不会保留每个选项卡的堆栈。所以基本上我有一个处理选项卡并将 fragment 附加到每个选项卡的主 FrameActivity。

在我的研究过程中,我发现了这个线程:https://stackoverflow.com/a/7480080/792407

他给出的例子很有道理,但我似乎无法显示 fragment 。因此,让我解释一下我正在做什么,以确保我理解正确:

我有一个扩展 FragmentActivity 并处理选项卡的主选项卡 Activity 。布局看起来像:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TabWidget
android:id="@android:id/tabs"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>

<FrameLayout
android:id="@+android:id/realtabcontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</TabHost>
</LinearLayout>

在此 Activity 中,我初始化了我的选项卡:

mTabHost = getTabHost();        
Resources res = getResources();
Intent intent;
TabHost.TabSpec spec;

//search tab
intent = new Intent().setClass(this, searchFragmentStack.class);
spec = mTabHost.newTabSpec("search").setIndicator("Search",res.getDrawable(R.drawable.ic_tab_search)).setContent(intent);
mTabHost.addTab(spec);

//home tab
intent = new Intent().setClass(this, homeFragmentStack.class);
spec = mTabHost.newTabSpec("home").setIndicator("Home",res.getDrawable(R.drawable.ic_tab_home)).setContent(intent);
mTabHost.addTab(spec);

我正在使用的堆栈类如下所示:

public class searchFragmentStack extends ActivityInTab {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
navigateTo(new search());
}
}

ActivityInTab 抽象类是他在线程中使用的相同代码:

abstract class ActivityInTab extends FragmentActivity { // FragmentActivity is just Activity for the support library.

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs_layout);
}

/**
* Navigates to a new fragment, which is added in the fragment container
* view.
*
* @param newFragment
*/
protected void navigateTo(Fragment newFragment) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.add(R.id.content, newFragment);
ft.addToBackStack(null);
ft.commit();
}

@Override
public void onBackPressed() {
FragmentManager manager = getSupportFragmentManager();
if (manager.getBackStackEntryCount() > 0) {
// If there are back-stack entries, leave the FragmentActivity
// implementation take care of them.
super.onBackPressed();
} else {
// Otherwise, ask user if he wants to leave :)
//showExitDialog();
}
}

}

堆栈的布局也是基于他的示例:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:isScrollContainer="true">
</RelativeLayout>

差不多就这些了。我得到的只是选项卡中的黑屏,这让我认为这是一个布局问题,或者我只是做错了。这有意义吗?有没有更好的办法?我错过了什么吗?任何帮助将不胜感激。

最佳答案

我在开始一个虚拟项目并从头开始做所有事情后想通了:

我用来处理选项卡的 Activity 需要是 TabActivity 并且像我使用 Activity 创建普通选项卡一样实现,除了我将使用单独的 FragmentActivity“堆栈”来管理每个选项卡中的内容。此外,我用于 Tab Activity 的布局对于我正在做的事情来说是错误的,我现在使用的是:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout>
</TabHost>

这是两个主要的变化,我已经上传了测试项目的源代码,以防有人想在他们自己的项目中引用它:

Seperate Fragment Stacks in Tabs

编辑

忘记提及 TabActivity 已被弃用,因此 future 的 Android 版本不应依赖此方法。我还没有想出如何使用 FragmentActivity 来实现它。也就是说,它可以满足我当前的需求,并让我有时间弄清楚如何以新的方式做到这一点。

关于android - 选项卡和 fragment ,单独的返回堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9287591/

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