Android的 - 在一个选项卡多个片段 [英] Android - Multiple fragments in ONE tab

查看:107
本文介绍了Android的 - 在一个选项卡多个片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了很多在web即将有一个操作栏选项卡中的多个片段的可能性。

I searched a lot in the web about the possibility to have multiple fragments in one action bar tab.

这个问题最接近我的需要,但在code不是工作。

This question comes closest to my needs but the code is not working.

有另一种可能性,包括多个片段在一个选项卡?

Is there another possibility to include multiple fragments in one tab?

这是 StartActivity

public class StartActivity extends FragmentActivity implements ActionBar.TabListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);

        // Set up the action bar.
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        actionBar.addTab(
              actionBar.newTab()
             .setText("Karte")
              .setTabListener(new MapFragmentListener(this)));

    }

的相应的布局 acticity_start 包含两个帧布局的两个片段,将被放置在标签

The corresponding layout acticity_start contains two frame layouts for the two fragments that will be placed in the tab.

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

    <FrameLayout
        android:id="@+id/fragment_map"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    <FrameLayout
        android:id="@+id/fragment_realtime"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

TabListener 是这样的:

private class TabListener implements ActionBar.TabListener {

        private static final String fragment1_tag = "fragment1_tag";
        private static final String fragment2_tag = "fragment2_tag";

        private FragmentActivity activity;
        private RealtimeFragment fragment1;
        private RealtimeFragment fragment2;

        public TabListener(FragmentActivity activity) {

            this.activity = activity;

            android.support.v4.app.FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();

            fragment1 = (RealtimeFragment) activity.getSupportFragmentManager().findFragmentByTag(fragment1_tag);
            if (fragment1 != null && !fragment1.isDetached()) {
                ft.detach(fragment1);
            }

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

            ft.commit();
        }

        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {

            android.support.v4.app.FragmentTransaction fragmentTransaction = activity.getSupportFragmentManager().beginTransaction();

            if(fragment1 == null) {
                fragment1 = new RealtimeFragment();
                fragmentTransaction.add(R.id.fragment_map, fragment1, fragment1_tag);
            } else {
                fragmentTransaction.attach(fragment1);
            }


            if(fragment2 == null) {
                fragment2 = new RealtimeFragment();
                fragmentTransaction.add(R.id.fragment_realtime, fragment2, fragment2_tag);
            } else {
                fragmentTransaction.attach(fragment2);
            }


            fragmentTransaction.commit();

        }

        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {

            android.support.v4.app.FragmentTransaction fragementTransaction = activity.getSupportFragmentManager().beginTransaction();

            if(fragment1_tag != null)
                fragementTransaction.detach(fragment1);
            if(fragment2 != null)
                fragementTransaction.detach(fragment2);


            fragementTransaction.commit();

        }

    }

RealtimeFragment 和相应的布局 fragment_realtime 是这样的:

The class RealtimeFragment and the corresponding layout fragment_realtime looks like this:

public class RealtimeFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
         View view = inflater.inflate(R.layout.fragment_realtime, container, false);
        return view;
    }
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

在一个选项卡,现在的类的两个片段 RealtimeFragment 应显示。这些片段只显示一个按钮,但不显示任何内容!为什么呢?

In one tab now two fragments of the class RealtimeFragment should be displayed. The fragments just show one button but NOTHING is displayed! Why?

推荐答案

我一直在寻找一个解决方案,以同一主题,并发现了这一点,这进来非常方便。

I was looking for a solution to the same Topic, and found this, which comes in quite handy.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment android:name="com.example.news.ArticleListFragment"
            android:id="@+id/list"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
    <fragment android:name="com.example.news.ArticleReaderFragment"
            android:id="@+id/viewer"
            android:layout_weight="2"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
</LinearLayout>

所以引用您的片段和它充气,就像一个正常的片段

so Reference your Fragments and inflate it, just like a normal Fragment

这篇关于Android的 - 在一个选项卡多个片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆