Creating FragmentTabHost causing some trouble

青春壹個敷衍的年華 提交于 2020-01-05 08:55:08

问题


So I'm developing an app and until now, I was using ordinary tab layout with two activities. Now someone told me that it's much better to do it usning FragmentTabHost. So I've been reading some docs and this is what I came up with so far:

MainActivity_Fragment:

public class MainActivity_Fragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.activity_main, container, false);
    }

}

RecordedLibrary_Fragment:

public class RecordedLibrary_Fragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.activity_recorded_library, container, false);
    }
}

TabLayout activity:

public class TabLayout extends FragmentActivity {

//GLOBAL VARIABLES///////////////
private FragmentTabHost mTabHost;

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

    mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
    mTabHost.setup(this, getSupportFragmentManager(), r.id....); // <-- problem here

    mTabHost.addTab(mTabHost.newTabSpec("Recording").setIndicator("Recording"),
            MainActivity_Fragment.class, null);

    mTabHost.addTab(mTabHost.newTabSpec("Library").setIndicator("Library"),
            RecordedLibrary_Fragment.class, null);
    }
}

Now this is where I am stuck. There are a few things that aren't explained in the docs. So I have two questions.

This line in the docs:

mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

Where did they get R.id.realtabcontent ?

And another thing that isn't explained; how should the xml of the tab activity look?

My current tab layout xml:

<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">
    <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"/>
</LinearLayout>

As you can see, the tab layout xml is still from the old tab layout. Do I need to adjust it to FragmentTabLayout or is it good this way?


回答1:


Now someone told me that it's much better to do it usning FragmentTabHost.

Well, I did say "FragmentTabHost is probably the least popular of the 3, though it's closest to your current code."

Where did they get R.id.realtabcontent ?

That's the ID of a widget in a layout. Specifically it would be in R.layout.fragment_tabs, which the documentation neglects to show.

Fortunately, this appears to be from the support package samples, and that layout can be found here, with the definition at the time of this writing as:

<android.support.v4.app.FragmentTabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <TabWidget
            android:id="@android:id/tabs"
            android:orientation="horizontal"
            android:layout_width="match_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="@+id/realtabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>

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

And another thing that isn't explained; how should the xml of the tab activity look?

See above.

Do I need to adjust it to FragmentTabLayout or is it good this way?

Well, if nothing else, you will need to replace TabHost with FragmentTabHost.

But, frankly, FragmentTabHost is worse off, documentation-wise, than I was remembering when I commented on your earlier question.



来源:https://stackoverflow.com/questions/18137121/creating-fragmenttabhost-causing-some-trouble

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!