我正在尝试添加Tabs到一个现有的应用程序,以添加更多的功能,我已经能够实现选项卡,也移动到片段。然而,我目前设置它的方式并不能保留每个标签的堆栈。所以基本上我有一个主FrameActivity来处理选项卡,并将片段附加到每个选项卡上。
在我的研究过程中,我发现了这个帖子:https://stackoverflow.com/a/7480080/792407
他给出的例子很有道理,但我似乎无法让片段显示出来。因此,让我解释一下我在做什么,以确保我正确理解它:
我有一个主选项卡活动,它扩展了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>在此活动中,我初始化我的选项卡:
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>差不多就是这样了。我看到的都是标签中的黑屏,这让我认为这是布局问题,或者我只是做错了。这有意义吗?有没有更好的方法?我是不是遗漏了什么?任何帮助都将不胜感激。
发布于 2012-02-17 04:25:36
在开始一个虚拟项目并从头开始做所有事情后,我已经弄明白了:
我用来处理选项卡的activity需要是一个TabActivity,并且实现起来就像我使用activity创建普通选项卡一样,只是我将使用单独的FragmentActivity "stacks“来管理每个选项卡中的内容。另外,我用于Tab活动的布局与我正在做的事情是错误的,我现在使用的是:
<?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已经被弃用了,所以人们不应该在未来的安卓版本中依赖这种方法。我还没有想出如何使用FragmentActivity来实现这一点。也就是说,它适用于我当前的需求,并让我有时间弄清楚如何以新的方式来做它。
发布于 2014-10-21 10:10:05
我在片段活动中对选项卡进行了实验。所以,也许值得一看
https://stackoverflow.com/questions/9287591
复制相似问题