android – 具有多个片段的ActionBar Tabs

我一直在尝试更新我的应用程序,并与碎片一起去
动作栏,以及我错过的所有其他UI功能.一世
明白我可以在活动中有多个片段,有
不同的布局基于设备和所有的好东西,但是
我正在努力获得一些标签的东西,我想要的方式.一世
了解如何添加选项卡,在它们之间切换,但如何才能
标签中有多个片段?所以例如我基本上有两个
屏幕我想让用户能够来回切换
很容易(为什么我想使用标签).如果我有两个单独的活动我
可以在xml文件中指定这一点,并使用setContentView使用布局
下面

tab1_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <fragment
        android:name="com.example.tabrefactor.Fragment1"
        android:id="@+id/fragment_1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>
    <fragment
        android:name="com.example.tabrefactor.Fragment2"
        android:id="@+id/fragment_2"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>
    <fragment
        android:name="com.example.tabrefactor.Fragment3"
        android:id="@+id/fragment_3"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>
</LinearLayout>

tab2_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <fragment
        android:name="com.example.tabrefactor.Fragment4"
        android:id="@+id/fragment_4"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>
</LinearLayout>

我可以将第二个布局转换为使用选项卡,因为它只包含
一个片段,但我不知道如何将第一个布局变成一个单一的
标签.这是允许的东西吗?提前致谢,

Jason Prenger

我会离开这个开放的人有人有一个简化或更好的想法…

最终有一个解决方法,有一个基本布局与3个框架布局…

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:id="@+id/fragment_sb"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    <FrameLayout
        android:id="@+id/fragment_local"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    <FrameLayout
        android:id="@+id/fragment_rest"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

</LinearLayout>

然后在我的活动与标签我做了一个自定义TabListener处理之间的变化.我使用的代码在下面(我使用actionbarsherlock,所以它看起来与正常的东西略有不同)

public class TabActivity extends FragmentActivity {

    Fragment1 fragment1;
    Fragment2 fragment2;
    Fragment3 fragment3;
    Fragment4 fragment4;

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

        final ActionBar bar = getSupportActionBar();
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        bar.setDisplayShowTitleEnabled(true);
        bar.setDisplayShowHomeEnabled(false);
        bar.setTitle("Title");

        bar.addTab(bar.newTab()
                .setIcon(R.drawable.ic_list_tab_selected)
                .setTabListener(new ListTabListener(this)));
        bar.addTab(bar.newTab()
                .setIcon(R.drawable.ic_map_tab_selected)
                .setTabListener(new MapTabListener(this)));

        if (savedInstanceState != null) {
            bar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("tab", getSupportActionBar().getSelectedNavigationIndex());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.test_menu, menu);

        return super.onCreateOptionsMenu(menu);
    }

    public static class ListTabListener implements ActionBar.TabListener {
        private static final String fragment1Tag = "fragment1_tag";
        private static final String fragment2Tag = "fragment2_tag";
        private static final String fragment3Tag = "fragment3_tag";

        private FragmentActivity activity;
        private Fragment1 fragment1;
        private Fragment2 fragment2;
        private Fragment3 fragment3;

        public ListTabListener(FragmentActivity activity) {
            this.activity = activity;

            FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
            fragment1 = (Fragment1) activity.getSupportFragmentManager().findFragmentByTag(fragment1Tag);
            if (fragment1 != null && !fragment1.isDetached()) {
                ft.detach(fragment1);
            }

            fragment2 = (Fragment2) activity.getSupportFragmentManager().findFragmentByTag(fragment1Tag);
            if (fragment2 != null && !fragment2.isDetached()) {
                ft.detach(fragment2);
            }

            fragment3 = (Fragment3) activity.getSupportFragmentManager().findFragmentByTag(fragment1Tag);
            if (fragment3 != null && !fragment3.isDetached()) {
                ft.detach(fragment3);
            }

            ft.commit();
        }

        @Override
        public void onTabReselected(Tab tab, FragmentTransaction nullFt) {
            //Reselected don't do anything          
        }

        @Override
        public void onTabSelected(Tab tab, FragmentTransaction nullFt) {
            FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();

            if(fragment1 == null) {
                fragment1 = new Fragment1();
                ft.add(R.id.fragment_sb, fragment1, fragment1Tag);
            } else {
                ft.attach(fragment1);
            }

            if(fragment2 == null) {
                fragment2 = new Fragment2();
                ft.add(R.id.fragment_local, fragment2, fragment2Tag);
            } else {
                ft.attach(fragment2);
            }

            if(fragment3 == null) {
                fragment3 = new Fragment3();
                ft.add(R.id.fragment_rest, fragment3, fragment3Tag);
            } else {
                ft.attach(fragment3);
            }

            ft.commit();
        }

        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction nullFt) {
            FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();

            if(fragment1 != null)
                ft.detach(fragment1);
            if(fragment2 != null)
                ft.detach(fragment2);
            if(fragment3 != null)
                ft.detach(fragment3);

            ft.commit();
        }
    }

    public static class MapTabListener implements ActionBar.TabListener {
        private static final String fragment4Tag = "fragment4_tag";

        private FragmentActivity activity;
        private Fragment4 fragment4;

        public MapTabListener(FragmentActivity activity) {
            this.activity = activity;

            FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
            fragment4 = (Fragment4) activity.getSupportFragmentManager().findFragmentByTag(fragment4Tag);
            if (fragment4 != null && !fragment4.isDetached()) {
                ft.detach(fragment4);
            }
            ft.commit();
        }

        @Override
        public void onTabReselected(Tab tab, FragmentTransaction nullFt) {
            //Reselected don't do anything          
        }

        @Override
        public void onTabSelected(Tab tab, FragmentTransaction nullFt) {
            FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();

            if(fragment4 == null) {
                fragment4 = new Fragment4();
                ft.add(R.id.fragment_rest, fragment4, fragment4Tag);
            } else {
                ft.attach(fragment4);
            }

            ft.commit();
        }

        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction nullFt) {
            FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();

            if(fragment4 != null)
                ft.detach(fragment4);

            ft.commit();
        }
    }
}
翻译自:https://stackoverflow.com/questions/9216108/actionbar-tabs-with-multiple-fragments

转载注明原文:android – 具有多个片段的ActionBar Tabs