Add FragmentTabHost inside fragment

丶灬走出姿态 提交于 2019-12-08 02:56:01

问题


I have an application with a side menu created from fragments.

The problem comes when I want to create tabs in one of the fragments.

I have tried many examples and not work for me.

fragment_home.xml

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

</LinearLayout>

FragmentHome.java

    public class FragmentInicio extends Fragment {

    private FragmentTabHost mTabHost;

    public FragmentInicio() {}


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        try{
            //HERE TABHOST????

        }catch(Exception e){
            Log.e("UDI", e.getMessage());
        }

        return view;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        Log.d("EVENT", "OnCreate");

    }


    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onViewCreated(view, savedInstanceState);
    }


    @Override
    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
        super.onAttach(activity);
        ((HomeActivity) activity).onSectionAttached(1);
    }
}

What can I do?

Example:

http://webdemo.com.es/sample2.jpg

Thank you!


回答1:


I know I'm late but this might help someone else.

To use FragmentTabHost inside fragment you can do this.

Create layout resource file

fragment_tab_host.xml

<?xml version="1.0" encoding="utf-8"?>
<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>

FrameLayout with id realtabcontent is which will show current tab content which is in this case FirstFragment, SecondFragment and ThirdFragment

TabHostWidgetFragment.java code will be

    public class TabHostWidgetFragment extends Fragment {

    private static final String TAG = TabHostWidgetFragment.class.getSimpleName();

    public TabHostWidgetFragment() { }

    private FragmentTabHost fragmentTabHost;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        Log.i(TAG, "onCreateView: ");
        return inflater.inflate(R.layout.fragment_frag_tab_host_widget, container, false);
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.i(TAG, "onViewCreated: ");
        fragmentTabHost = view.findViewById(android.R.id.tabhost);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.i(TAG, "onActivityCreated: ");

        fragmentTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
        fragmentTabHost.addTab(fragmentTabHost.newTabSpec("First Tab").setIndicator("First Tab"), FirstFragment.class, null);
        fragmentTabHost.addTab(fragmentTabHost.newTabSpec("Second Tab").setIndicator("Second Tab"), SecondFragment.class, null);
        fragmentTabHost.addTab(fragmentTabHost.newTabSpec("Third Tab").setIndicator("Third Tab"), ThirdFragment.class, null);

    }


    public class FirstFragment extends Fragment {

        private static final String TAG = FirstFragment.class.getSimpleName();
        public FirstFragment() { }

        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                                 @Nullable Bundle savedInstanceState) {
            Log.i(TAG, "onCreateView: ");
            return inflater.inflate(R.layout.fragment_first, container, false);
        }

        @Override
        public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            Log.i(TAG, "onViewCreated: ");
        }

    }


    public class SecondFragment extends Fragment {

        private static final String TAG = SecondFragment.class.getSimpleName();
        public SecondFragment() { }

        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                                 @Nullable Bundle savedInstanceState) {
            Log.i(TAG, "onCreateView: ");
            return inflater.inflate(R.layout.fragment_second, container, false);
        }


        @Override
        public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            Log.i(TAG, "onViewCreated: ");
        }

    }


     public class ThirdFragment extends Fragment {

        private static final String TAG = ThirdFragment.class.getSimpleName();
        public ThirdFragment() { }


        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                                 @Nullable Bundle savedInstanceState) {
            Log.i(TAG, "onCreateView: ");
            return inflater.inflate(R.layout.fragment_third, container, false);
        }


        @Override
        public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            Log.i(TAG, "onViewCreated: ");
        }

    }


}

Layout resources for FristFragment, SecondFragment and ThirdFragment

fragment_first.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/firstTab"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/nature1"/>
</LinearLayout>

fragment_second.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/secondTab"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/nature2"/>
</LinearLayout>

fragment_third.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/thirdTab"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:scaleType="centerInside"
        android:src="@drawable/nature3"/>
</LinearLayout>




If you want to make it swipeable then add ViewPager after FrameLayout with id realtabcontent as written below and implement viewPager logic.

<?xml version="1.0" encoding="utf-8"?>
<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="0"/>
        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>
    </LinearLayout>
</android.support.v4.app.FragmentTabHost>

And TabHostWidgetFragment.java class will be

Create separate or inner MyTabFactory.java class

public class MyTabFactory implements TabHost.TabContentFactory {

    private final Context context;

    public MyTabFactory(Context context) {
        this.context = context;
    }

    @Override
    public View createTabContent(String s) {
        View v = new View(context);
        v.setMinimumWidth(0);
        v.setMinimumHeight(0);
        return v;
    }
}

And then add tabs to tabhost like this. This is essential to setContent with MyTabFactory. If you add tabs like I mentioned above in code snippet, there will be issue which is, It will add fragments two times, one because of TabHost and one because of ViewPager.

fragmentTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
fragmentTabHost.addTab(fragmentTabHost.newTabSpec("First Tab").setIndicator("First Tab").setContent(new MyTabFactory(getActivity())));
fragmentTabHost.addTab(fragmentTabHost.newTabSpec("Second Tab").setIndicator("Second Tab").setContent(new MyTabFactory(getActivity())));
fragmentTabHost.addTab(fragmentTabHost.newTabSpec("Third Tab").setIndicator("Third Tab").setContent(new MyTabFactory(getActivity())));




You can either use android.support.v4.app.FragmentTabHost or TabHost It will work same for both.



来源:https://stackoverflow.com/questions/32437307/add-fragmenttabhost-inside-fragment

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