简体   繁体   English

Android:带有标签的片段

[英]Android: Fragment with tabs

How can I put tabs inside of my fragment? 如何将标签放在片段中? I have been struggling with this and would greatly appreciate some help. 我一直在为此苦苦挣扎,将不胜感激一些帮助。

Currently I am getting this error: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'? 当前,我收到此错误: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?

However, even when I add a call to this I still get a fatal crash with an error: Must call setup() that takes a Context and FragmentManager 但是,即使我添加了对此的调用,也仍然会发生致命的错误并崩溃: Must call setup() that takes a Context and FragmentManager

This seems to be contradictory.... 这似乎是矛盾的。

Below is the relevant Java and XML: 以下是相关的Java和XML:

  @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    super.onCreateView(inflater, container, savedInstanceState);

    View v = inflater.inflate(R.layout.fragment_profile, container, false);

    mTabHost = new FragmentTabHost(getActivity());
    mTabHost.setup(getActivity().getApplicationContext(), getChildFragmentManager(), R.layout.testtabhost1);

    //LocalActivityManager mLocalActivityManager = new LocalActivityManager(getActivity(), false);
    //mLocalActivityManager.dispatchCreate(savedInstanceState); 
    //mTabHost.setup(mLocalActivityManager);

    //LocalActivityManager mLocalActivityManager = new LocalActivityManager(getActivity(), false);
    //mTabHost.setup(mLocalActivityManager);

    Intent q_intent = new Intent (getActivity(), QuestionTabActivity.class);
    Intent a_intent = new Intent (getActivity(), AnswerTabActivity.class);

    View questionIndicator = createTabView(mTabHost.getContext(), "Questions");
    View answerIndicator = createTabView(mTabHost.getContext(), "Answers");

    mTabHost.addTab(mTabHost.newTabSpec("questions").setIndicator(questionIndicator).setContent(q_intent));
    mTabHost.addTab(mTabHost.newTabSpec("answers").setIndicator(answerIndicator).setContent(a_intent));

    mTabHost.setCurrentTab(0);

    // Inflate the layout for this fragment
    return v;
}

And the XML: 和XML:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.juryroom.qollege_android_v1.ProfileFragment">

    .........
    ...ETC...
    .........

    <android.support.v4.app.FragmentTabHost
        android:id="@+id/testtabhost1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/edit_profile_picture_fragment_circView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="5dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:showDividers="none"></TabWidget>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent">


            </FrameLayout>
        </LinearLayout>
    </android.support.v4.app.FragmentTabHost>

.........
...ETC...
.........

</FrameLayout>

Here is the design look I'm making: 这是我正在设计的外观: 我的设计

mTabHost = (FragmentTabHost) v.findViewById(android.R.id.tabhost);
    mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
    mTabHost.addTab(mTabHost.newTabSpec(Constant.BuildingPicture.FRONT_PHOTOS).setIndicator(Constant.BuildingPicture.FRONT_PHOTOS),SiteDataFrontPhotoTab.class, null);
    mTabHost.addTab(mTabHost.newTabSpec(Constant.BuildingPicture.SATELLITE_PHOTOS).setIndicator(Constant.BuildingPicture.SATELLITE_PHOTOS),SiteDataSatellitePhotoTab.class, null);

In order to display tabs we don't have to use any other UI element like TabHost. 为了显示选项卡,我们不必使用任何其他UI元素(例如TabHost)。 Action bar has the inbuilt capability of adding tabs. 操作栏具有添加标签的内置功能。 All we have to do is enable it using setNavigationMode(ActionBar.NAVIGATION_MODE_TABS). 我们要做的就是使用setNavigationMode(ActionBar.NAVIGATION_MODE_TABS)启用它。

Here is a good description of what you are looking for. 是您要寻找的东西的良好描述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM