Android Adding Tab inside Fragment

后端 未结 4 1347
挽巷
挽巷 2020-12-15 01:15

I am Trying to add a TabHost Inside a Fragment ...The Code is given bellow.Here inside the Fragment I am Trying to add TabHost to show two Tab

public class T         


        
相关标签:
4条回答
  • 2020-12-15 01:29

    I believe this could work for you, if you are targeting API17 +. If not you should take a look at ViewContainers, and swipeable views.

    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentTabHost;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    public class TabHostParentFragment extends Fragment {
    
    private FragmentTabHost tabHost;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    tabHost = new FragmentTabHost(getActivity());
    tabHost.setup(getActivity(), getChildFragmentManager(), R.layout.my_parent_fragment);
    
    Bundle arg1 = new Bundle();
    arg1.putInt("Arg for Frag1", 1);
    tabHost.addTab(tabHost.newTabSpec("Tab1").setIndicator("Frag Tab1"),
        NewsFragment.class, arg1);
    
    Bundle arg2 = new Bundle();
    arg2.putInt("Arg for Frag2", 2);
    tabHost.addTab(tabHost.newTabSpec("Tab2").setIndicator("Frag Tab2"),
        MyNestedFragment2.class, arg2);
    
    return tabHost;
    }
    
    0 讨论(0)
  • 2020-12-15 01:31
    import android.app.Fragment;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AnalogClock;
    import android.widget.TabHost;
    
    public class TabFragment extends Fragment {
    
        private TabHost mTabHost;
        View rootView;
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            rootView = inflater.inflate(R.layout.tabinterview, container, false);
            mTabHost = (TabHost) rootView.findViewById(R.id.tabhost);
            mTabHost.setup();
            TabHost.TabSpec spec = mTabHost.newTabSpec("tag");
            spec.setIndicator("Android");
            spec.setContent(new TabHost.TabContentFactory() {
    
                @Override
                public View createTabContent(String tag) {
                    // TODO Auto-generated method stub
                    return (new AnalogClock(getActivity()));
                }
            });
            mTabHost.addTab(spec);
            spec = mTabHost.newTabSpec("tag1");
            spec.setIndicator("java");
            spec.setContent(new TabHost.TabContentFactory() {
    
                @Override
                public View createTabContent(String tag) {
                    // TODO Auto-generated method stub
                    return (new AnalogClock(getActivity()));
                }
            });
            mTabHost.addTab(spec);
            spec = mTabHost.newTabSpec("tag2");
            spec.setIndicator("Favourate");
            spec.setContent(new TabHost.TabContentFactory() {
    
                @Override
                public View createTabContent(String tag) {
                    // TODO Auto-generated method stub
                    return (new AnalogClock(getActivity()));
                }
            });
            mTabHost.addTab(spec);
            return rootView;
        }
    }
    
    this code how to give Page Link
    
    0 讨论(0)
  • 2020-12-15 01:34

    In case you use: https://stackoverflow.com/a/34224495/1386969

    Make sure the layout looks like:

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".view.user.DetailsFragment">
    
    <TabHost
        android:id="@+id/tabhost"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent">
        </TabWidget>
    
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent">
        </FrameLayout>
    </TabHost>
    

    0 讨论(0)
  • 2020-12-15 01:55

    checkout below code it will definitely help you if u still looking to for answer & and it will work on API 11 for old version

    import android.app.Fragment;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AnalogClock;
    import android.widget.TabHost;
    
    public class TabFragment extends Fragment {
    
        private TabHost mTabHost;
        View rootView;
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            rootView = inflater.inflate(R.layout.tabinterview, container, false);
            mTabHost = (TabHost) rootView.findViewById(R.id.tabhost);
            mTabHost.setup();
            TabHost.TabSpec spec = mTabHost.newTabSpec("tag");
            spec.setIndicator("Android");
            spec.setContent(new TabHost.TabContentFactory() {
    
                @Override
                public View createTabContent(String tag) {
                    // TODO Auto-generated method stub
                    return (new AnalogClock(getActivity()));
                }
            });
            mTabHost.addTab(spec);
            spec = mTabHost.newTabSpec("tag1");
            spec.setIndicator("java");
            spec.setContent(new TabHost.TabContentFactory() {
    
                @Override
                public View createTabContent(String tag) {
                    // TODO Auto-generated method stub
                    return (new AnalogClock(getActivity()));
                }
            });
            mTabHost.addTab(spec);
            spec = mTabHost.newTabSpec("tag2");
            spec.setIndicator("Favourate");
            spec.setContent(new TabHost.TabContentFactory() {
    
                @Override
                public View createTabContent(String tag) {
                    // TODO Auto-generated method stub
                    return (new AnalogClock(getActivity()));
                }
            });
            mTabHost.addTab(spec);
            return rootView;
        }
    }
    
    0 讨论(0)
提交回复
热议问题