在片段内添加FragmentTabHost [英] Add FragmentTabHost inside fragment

查看:92
本文介绍了在片段内添加FragmentTabHost的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有从片段创建的侧面菜单的应用程序.

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);
    }
}

我该怎么办?

示例:

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

谢谢!

推荐答案

我知道我来晚了,但这可能会对其他人有所帮助.

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

要在片段中使用FragmentTabHost,可以执行此操作.

To use FragmentTabHost inside fragment you can do this.

创建布局资源文件

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>

id为realtabcontent的FrameLayout将显示当前选项卡的内容,在本例中为FirstFragment,SecondFragment和ThirdFragment

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

TabHostWidgetFragment.java代码将是

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: ");
        }

    }


}

FristFragment,SecondFragment和ThirdFragment的布局资源

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>




如果要使其可滑动,请在ID为realtabcontent的FrameLayout之后添加ViewPager,如下所示,并实现viewPager逻辑.




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>

TabHostWidgetFragment.java类将

And TabHostWidgetFragment.java class will be

创建单独的或内部的MyTabFactory.java类

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;
    }
}

然后像这样将标签添加到tabhost.这对于使用MyTabFactory的setContent是必不可少的.如果您添加如我上面在代码片段中提到的选项卡,则会出现问题,它将添加两次片段,一次是由于TabHost,另一次是由于ViewPager.

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())));




您可以使用 android.support.v4.app.FragmentTabHost TabHost .这两种方法都相同.




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

这篇关于在片段内添加FragmentTabHost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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