我正在尝试将选项卡添加到现有应用程序以添加更多功能,我已经能够实现选项卡并将所有内容移动到 fragment 。但是,我目前的设置方式并没有保留每个选项卡的堆栈。所以基本上我有一个主 FrameActivity 来处理选项卡并将 fragment 附加到每个选项卡。
在我的研究过程中,我发现了这个帖子:https://stackoverflow.com/a/7480080/792407
他给出的例子很有意义,但我似乎无法显示 fragment 。因此,让我解释一下我正在做的事情,以确保我正确理解它:
我有一个主选项卡 Activity ,它扩展 FragmentActivity 并处理选项卡。布局如下:
<?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/