How do I implement swiping between tabs on Android?
Solution 1
NOTE: This is an excerpt from the Android Training class Implementing Effective Navigation.
To implement this (in Android 3.0 or above), you can use a ViewPager in conjunction with the ActionBar tabs API.
Upon observing the current page changing, select the corresponding tab. You can set up this behavior using an ViewPager.OnPageChangeListener in your activity's onCreate()
method:
@Override
public void onCreate(Bundle savedInstanceState) {
...
mViewPager.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// When swiping between pages, select the
// corresponding tab.
getActionBar().setSelectedNavigationItem(position);
}
});
...
}
And upon selecting a tab, switch to the corresponding page in the ViewPager. To do this, add an ActionBar.TabListener to your tab when creating it using the newTab()
method:
actionBar.newTab()
...
.setTabListener(new ActionBar.TabListener() {
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction ft) {
// When the tab is selected, switch to the
// corresponding page in the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
...
}));
Solution 2
If you are targeting APIs below Android 3.0, you cannot use Roman's solution above.
I wrote a blog post here about how to accomplish the same thing with ActionBarSherlock if anyone is interested.
Related videos on Youtube
Roman Nurik
Android Developer Relations @ Google Also: JavaScript/Python guy, web/graphic designer, etc.
Updated on October 25, 2020Comments
-
Roman Nurik over 3 years
One of the key design recommendations in Android 4.0 for tabs is to allow swiping between them where appropriate. This behavior enables users to swipe horizontally across the selected tab's contents to navigate to adjacent tabs, without needed to directly interact with the tabs themselves.
How can this be implemented?
-
user289463 about 12 yearsHi Roman, Please work out an example that implements ant adapter to implement swiping multi-pane layouts containing multiple dynamically created/replaceable fragments. Challenges: - The FragmentManager uses a resource ID to add Fragments; IDs must be unique across pages (you can't specify a ViewGroup instance as a container) - Container views must exist before FragmentManager attempts to restore fragments - Retained instances vs orientation change bug #1:NullPointerException around when FragmentManager attempts to save Fragment state; #2:NullPointerException around LoaderInfo.onLoadComplete
-
Roman Nurik about 12 yearsYou should ask this as a new question.
-
ccpizza over 11 yearsIt is possible to use the solution for earlier versions if you add the android compability support library developer.android.com/tools/extras/support-library.html. With the latest Eclipse ADT plugin it is as easy as right-clicking the project and picking
Android Tools
>Add Support Library
. The project will have to be built against API 14 or higher (i.e Android 4+) while using a manifest with e.g. ` <uses-sdk android:minSdkVersion="7">` to run on android 2.1. -
Name is Nilay over 11 years@JesperB-What if I need to implement Swipe among tabs in my TabHost for android app targeting APIs below Android 2.3.3 and without using ActionBarSherlock..?? Can u suggest me something for that..??
-
Name is Nilay over 11 years@JesperB-Actually I have already made the app with tabview working perfectly fine...I just need to update the feature of Swipe !!
-
Mukesh Bhojwani about 11 yearsHi ccpizza, why did you say "The project will have to be built against API 14 or higher (i.e Android 4+)" is this necessary?
-
Maxim about 11 yearsStrange things happen if you add menu icons to actionbar and use ViewPager that way...
-
Markus Junginger about 11 yearsDon't forget: if you use API level 13 and above, you should go for android-support-v13.jar. It contains android.support.v13.app.FragmentPagerAdapter, which does not rely on support Fragments and Activities, but can be used with the "real" thing.