Skip to content Skip to sidebar Skip to footer

How To Add Tabhost With Navigation Drawer?

I am new to android development,I used navigation drawer from this http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/ Now I want to add Tabhost with

Solution 1:

So in order to do this modify the HomeFragment like this:

publicclassHomeFragmentextendsFragment {

private TabHost mTabHost;
 private ViewPager mViewPager;
 private TabsPagerAdapter mTabsAdapter;

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

     ViewrootView= inflater.inflate(R.layout.fragment_home, container, false);
     mViewPager = (ViewPager)rootView.findViewById(R.id.pager);
     mTabsAdapter= newYourAdapter(getSupportFragmentManager());
     mViewPager.setAdapter(mTabsAdapter);
     PagerSlidingTabStriptabs= (PagerSlidingTabStrip)rootView.findViewById(R.id.tabs);
     tabs.setViewPager(pager);
     return rootView;
 }  

Your Adapter Class

publicclassYourAdapterextendsFragmentStatePagerAdapter {
    private String[] titles = { "Item 1", "Item 2", "Item 3" };
    publicYourAdapter(FragmentManager fm) {
        super(fm);
    }

    @Overridepublic Fragment getItem(int i) {
        switch(i){
           case0:{
              returnnewFragementA();
           }case1:{
              returnnewFragmentB();
           }case2:{
              returnnewFragmentC();
           }
        }
    }

    @OverridepublicintgetCount() {
        return titles.length;
    }

    @Overridepublic CharSequence getPageTitle(int position) {
        return titles[position];
    }
}

Your layout file for the viewpager:

Compile this dependency in order to use it: compile 'com.astuetz:pagerslidingtabstrip:1.0.1'

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><com.astuetz.PagerSlidingTabStripandroid:id="@+id/tabs"android:layout_width="match_parent"android:layout_height="48dip" /><android.support.v4.view.ViewPagerandroid:id="@+id/pager"android:layout_width="match_parent"android:layout_height="wrap_content" /></LinearLayout>

The Fragment that the viewpager will create for each tab will be like:

publicclassFragmentAextendsFragment {

    @Overridepublic View onCreateView(LayoutInflater inflater,
            ViewGroup container, Bundle savedInstanceState) {
        ViewrootView= inflater.inflate(R.layout.my_layout_file, container, false);
        //Simple implementation how to target text view in your layoutButtontv= (Button)rootView.findViewById(R.id.my_button_view);
        return rootView;
    }
}

And the layout:

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:background="#ccc"android:layout_height="match_parent"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="New Button"android:id="@+id/my_button_view"android:layout_marginTop="175dp" /></RelativeLayout>

I hope it helps!!!!

Solution 2:

You not added any view for your TabHost View. Try to add view in your setIndicator() method like this.

publicclassHomeFragmentextendsFragment {
private FragmentTabHost mTabHost;

@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    mTabHost = newFragmentTabHost(getActivity());
    mTabHost.setup(getActivity(), getChildFragmentManager(),
            R.layout.fragment_home);
    Bundlearg1=newBundle();
    arg1.putInt("Arg for Frag1", 1);
    Viewview= LayoutInflater.from(getActivity()).inflate(
            R.layout.yourview1, null);
    mTabHost.addTab(mTabHost.newTabSpec("Tab1").setIndicator(view),
            PhotosActivity.class, arg1);
    Bundlearg2=newBundle();
    arg2.putInt("Arg for Frag2", 2);
    Viewview= LayoutInflater.from(getActivity()).inflate(
            R.layout.yourview2, null);
    mTabHost.addTab(mTabHost.newTabSpec("Tab2").setIndicator(view),
            PhotosActivity.class, arg2);
    return mTabHost;
}

@OverridepublicvoidonDestroyView() {
    super.onDestroyView();
    mTabHost = null;
}

Post a Comment for "How To Add Tabhost With Navigation Drawer?"