web-dev-qa-db-fra.com

Ajouter un onglet à l'intérieur du fragment dans Android?

J'essaie d'ajouter un TabHost à l'intérieur d'un fragment. Le code est donné ci-dessous. Ici à l'intérieur du Fragment. J'essaie d'ajouter TabHost pour afficher deux onglets:

package com.nordicsoft.dilosysNewVersion;

import Android.app.Fragment;
import Android.content.Intent;
import Android.os.Bundle;
import Android.view.LayoutInflater;
import Android.view.View;
import Android.view.ViewGroup;
import Android.widget.TabHost;

public class Fragment_Coupons extends Fragment {
    TabHost tabHost;
    TabHost.TabSpec spec;

    public Fragment_Coupons() {
    }

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

        View rootView = inflater.inflate(R.layout.fragment_coupon, container,
                false);
        tabHost = (TabHost) rootView.findViewById(Android.R.id.tabhost);
        tabHost.setup();
        //Add_Text_To_Tab("Android", "IOS");

        Intent intentAndroid = new Intent().setClass(getActivity(),
                Aclass.class);
        spec = tabHost.newTabSpec("Android").setContent(intentAndroid)
                .setIndicator("Android");

        tabHost.addTab(spec);

        Intent intentBus = new Intent().setClass(getActivity(), Bclass.class);
        spec = tabHost.newTabSpec("Welcome").setIndicator("Welcome")
                .setContent(intentBus).setIndicator("Welcome");
        tabHost.addTab(spec);
        return rootView;
    }

}

Le code XML:

<?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">
        <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>
</TabHost>

Cela me donne l'erreur:

12-09 17:40:53.509: E/AndroidRuntime(18103): FATAL EXCEPTION: main
12-09 17:40:53.509: E/AndroidRuntime(18103): Java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
12-09 17:40:53.509: E/AndroidRuntime(18103):    at Android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.Java:690)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at Android.widget.TabHost.setCurrentTab(TabHost.Java:356)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at Android.widget.TabHost.addTab(TabHost.Java:246)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at com.nordicsoft.dilosysNewVersion.Fragment_Coupons.onCreateView(Fragment_Coupons.Java:32)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at Android.app.FragmentManagerImpl.moveToState(FragmentManager.Java:828)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at Android.app.FragmentManagerImpl.moveToState(FragmentManager.Java:1032)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at Android.app.BackStackRecord.run(BackStackRecord.Java:622)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at Android.app.FragmentManagerImpl.execPendingActions(FragmentManager.Java:1382)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at Android.app.FragmentManagerImpl$1.run(FragmentManager.Java:426)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at Android.os.Handler.handleCallback(Handler.Java:605)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at Android.os.Handler.dispatchMessage(Handler.Java:92)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at Android.os.Looper.loop(Looper.Java:137)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at Android.app.ActivityThread.main(ActivityThread.Java:4517)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at Java.lang.reflect.Method.invokeNative(Native Method)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at Java.lang.reflect.Method.invoke(Method.Java:511)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:993)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:760)
12-09 17:40:53.509: E/AndroidRuntime(18103):    at dalvik.system.NativeStart.main(Native Method)
12-09 17:46:02.770: I/Process(18103): Sending signal. PID: 18103 SIG: 9
36
NRahman

Essayez de faire cela pour gérer les onglets:

public class MainFragment extends Fragment {
    private FragmentTabHost mTabHost;

    //Mandatory Constructor
    public MainFragment() {
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

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

        View rootView = inflater.inflate(R.layout.fragment_tabs,container, false);


        mTabHost = (FragmentTabHost)rootView.findViewById(Android.R.id.tabhost);
        mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);

        mTabHost.addTab(mTabHost.newTabSpec("fragmentb").setIndicator("Fragment B"),
                FragmentB.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("fragmentc").setIndicator("Fragment C"),
                FragmentC.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("fragmentd").setIndicator("Fragment D"),
                FragmentD.class, null);


        return rootView;
    }
}

Avec la mise en page:

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

        <FrameLayout
            Android:id="@+id/realtabcontent"
            Android:layout_width="match_parent"
            Android:layout_height="0dp"
            Android:layout_weight="1"/>

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

La MotherActivity pour héberger le MainFragment:

public class MotherActivity extends FragmentActivity {

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

        MainFragment fragmenttab = new MainFragment();
        getSupportFragmentManager().beginTransaction()
        .add(R.id.item_detail_container, fragmenttab).commit();


    }

Et la disposition MotherActivity:

<FrameLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    xmlns:tools="http://schemas.Android.com/tools"
    Android:id="@+id/item_detail_container"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent"
 />

Après cela, créez simplement le fragment normal B et C, etc. Classe. Le résultat sera:

Tab Fragment inside Fragment Result

68
ElCharro

Essayez d'ajouter cette ligne

tabHost.setup(this.getActivity().getLocalActivityManager());

avant la ligne

tabHost.addTab(spec);

ou essayez d'ajouter:

LocalActivityManager mLocalActivityManager = new LocalActivityManager(this.getActivity(), false);
mLocalActivityManager.dispatchCreate(savedInstanceState);
Host.setup(mLocalActivityManager);
1
SalGad