Reputation:
I got 3 navigation tabs with 3 different fragments in them.
How to add swipe between the fragments?
Upvotes: 0
Views: 429
Reputation: 7456
Android made something for this exact reason called ViewPager!
in your onCreate method, do something like this:
pager = (ViewPager) findViewById(R.id.pager);
adapter = new MyPagerAdapter(getSupportFragmentManager());
pager.setAdapter(adapter);
then,
Include the following into your MainActivity:
public class MyPagerAdapter extends FragmentStatePagerAdapter {
SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();
private final String[] TITLES = getResources().getStringArray(R.array.tabs);
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public CharSequence getPageTitle(int position) {
return TITLES[position];
}
@Override
public int getCount() {
return TITLES.length;
}
@Override
public Fragment getItem(int position) {
switch(position) {
case 0: return OneFragment.newInstance(position);
case 1: return TwoFragment.newInstance(position);
case 2: return ThreeFragment.newInstance(position);
}
return null;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
registeredFragments.put(position, fragment);
return fragment;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
registeredFragments.remove(position);
super.destroyItem(container, position, object);
}
public Fragment getRegisteredFragment(int position) {
return registeredFragments.get(position);
}
}
The code I posted is code I use. It is more sophisticated than usual. It keeps track of the Fragments (each tab), allowing you to access Fragments from one another. So if you were in OneFragment, and wanted access to TwoFragment, you'd do something like:
in OneFragment
private void getTwoFragmentData() {
data = (MainActivity) getActivity.getTwoFragmentData();
}
in MainActivity
public Data getTwoFragmentData() {
TwoFragment twoFragment = (TwoFragment) adapter.getRegisteredFragment(1);
return twoFragment.getData();
}
Data above is just a placeholder for however you would interface with it, but the above code is the general structure of how you'd do it.
Also, you would edit the getItem
method. And make the switch, case
accordingly.
As an extra, you could do pager.setOffScreenPageLimit(x)
, to also load the content x
tabs away from your current tab, which is useful for retaining information. For 3 tabs, I would use x = 2
, but ultimately, be wary because that is extra memory used.
Finally, the last little extra is that I use a library called com.astuetz.PagerSlidingTabStrip
which is awesome for getting the bar underneath the tabs to swipe with you and make it look really clean. Adding it is very simple and you'll find more details here
My activity_main.xml
looks like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- <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"
<!-- android:layout_below="@+id/tabs" -->
tools:context=".MainActivity" />
</RelativeLayout>
Ignore the commented out stuff. That is what I use in addition to get the library I stated above integrated.
Upvotes: 1