I'm using a navigation drawer and loading fragments in every time a menu item is selected. But I have one fragment that I want to use tabs/swipe views on so this fragment will itself have multiple fragments I'm switching in between.
My question is what's the best way to implement the swipe view/tabs inside a fragment that's loaded in the main activity. Any good tutorials on this? Should I add tabs to the action bar in the main activity or is there a better way to do this?
Any code snippet examples I see online or from the android developer site causes my app to crash and I have a tough time debugging the problem.
Any examples or ideas?
Thanks.
Here's some code I started but how can I change things like style the tabs and alter background color, underline tab color, font, etc. I haven't actually added fragment layouts/data to the tabs but I have a dummy fragment I load that's empty for now. So I can begin changing the fragments I'm loading into the tabs easy by just doing standard XML layout stuff, but what about customizing the tabs themselves and their appearance?
I used the adapter I did because I don't want to destroy anything, I need access to all the information across tabs since it'll be a step 1 to step X sort of thing, will that be a memory issue? There's only like 5-8, not hundreds of tabs.
THANKS.
public class PagerAdapter extends FragmentPagerAdapter {
public PagerAdapter(FragmentManager fragment) {
super(fragment);
}
@Override
public Fragment getItem(int i)
{
//testing, remove later
Fragment fragment = new DummyFragment();
return fragment;
}
@Override
public int getCount() {
return 6; //6 tabs
}
@Override
public CharSequence getPageTitle(int position) {
return "OBJECT " + (position + 1); //dummy title
}
}
public class TabFragment extends Fragment {
private FragmentPagerAdapter mPagerAdapter;
private ViewPager mViewPager;
//@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_tabs, container, false);
//this will be our swipe/tab view populated with each checkout
//piece fragment
mPagerAdapter =
new PagerAdapter(
getActivity().getSupportFragmentManager());
mViewPager = (ViewPager)view.findViewById(R.id.pager);
mViewPager.setAdapter(mPagerAdapter);
return view;
}
}
public class DummyFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_dummy, container, false);
return view;
}
}
My app uses this same pattern, you will need to set your minimum API level to 17 and use getChildFragmentManager. So basically the Fragment you load when you click on a nav drawer item will host a bunch of child fragments inside of it. I found it useful to make the child fragments static inner classes for this.
Your Fragment you load inside the activity should have an XML layout like this
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.PagerTitleStrip
android:id="@+id/pagerTitleStrip"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_gravity="top"
android:paddingTop="4dp"
android:paddingBottom="4dp"/>
</android.support.v4.view.ViewPager>
So you inflate this as the parent fragments layout, and set an adapter with a bunch of child fragments.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.