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