How to add tabhost with navigation drawer?

删除回忆录丶 提交于 2019-11-28 13:06:05

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

public class HomeFragment extends Fragment {

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

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

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

Your Adapter Class

public class YourAdapter extends FragmentStatePagerAdapter {
    private String[] titles = { "Item 1", "Item 2", "Item 3" };
    public YourAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        switch(i){
           case 0:{
              return new FragementA();
           }case 1:{
              return new FragmentB();
           }case 2:{
              return new FragmentC();
           }
        }
    }

    @Override
    public int getCount() {
        return titles.length;
    }

    @Override
    public 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'

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <com.astuetz.PagerSlidingTabStrip
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="48dip" />

    <android.support.v4.view.ViewPager
        android: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:

public class FragmentA extends Fragment {

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

And the layout:

<RelativeLayout xmlns: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">

    <Button
        android: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!!!!

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

public class HomeFragment extends Fragment {
private FragmentTabHost mTabHost;

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

@Override
public void onDestroyView() {
    super.onDestroyView();
    mTabHost = null;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!