-
-
Save rrouse/4148334 to your computer and use it in GitHub Desktop.
Android FragmentTabsPager with Actionbarsherlock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import android.content.Context; | |
import android.os.Bundle; | |
import android.support.v4.app.Fragment; | |
import android.support.v4.app.FragmentActivity; | |
import android.support.v4.app.FragmentPagerAdapter; | |
import android.support.v4.app.FragmentTransaction; | |
import android.support.v4.view.ViewPager; | |
import com.actionbarsherlock.app.ActionBar; | |
import com.actionbarsherlock.app.ActionBar.Tab; | |
import com.actionbarsherlock.app.SherlockFragmentActivity; | |
/** | |
* Copyright (C) 2012 Simon Grätzer | |
* | |
* @author [email protected] | |
* | |
*/ | |
public class ActionbarTabsPager extends SherlockFragmentActivity { | |
ViewPager mViewPager; | |
TabsAdapter mTabsAdapter; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
ActionBar bar = getSupportActionBar(); | |
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); | |
bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE); | |
mViewPager = (ViewPager)findViewById(R.id.pager); | |
// Add the tabs | |
mTabsAdapter = new TabsAdapter(this, bar, mViewPager); | |
mTabsAdapter.addTab(bar.newTab().setText(R.string.example1).setIcon(R.drawable.example1), | |
Example1Fragment.class, null); | |
mTabsAdapter.addTab(bar.newTab().setText(R.string.example2).setIcon(R.drawable.example2), | |
Example2Fragment.class, null); | |
setTitle(R.string.app_name); | |
if (savedInstanceState != null) { | |
bar.setSelectedNavigationItem(savedInstanceState.getInt("tab")); | |
} | |
} | |
@Override | |
protected void onSaveInstanceState(Bundle outState) { | |
super.onSaveInstanceState(outState); | |
outState.putInt("tab", getSupportActionBar().getSelectedNavigationIndex()); | |
} | |
/** | |
* This is a helper class that implements the management of tabs and all | |
* details of connecting a ViewPager with associated TabHost. It relies on a | |
* trick. Normally a tab host has a simple API for supplying a View or | |
* Intent that each tab will show. This is not sufficient for switching | |
* between pages. So instead we make the content part of the tab host | |
* 0dp high (it is not shown) and the TabsAdapter supplies its own dummy | |
* view to show as the tab content. It listens to changes in tabs, and takes | |
* care of switch to the correct paged in the ViewPager whenever the selected | |
* tab changes. | |
*/ | |
public static class TabsAdapter extends FragmentPagerAdapter | |
implements ViewPager.OnPageChangeListener, ActionBar.TabListener { | |
private final Context mContext; | |
private final ActionBar mBar; | |
private final ViewPager mViewPager; | |
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>(); | |
static final class TabInfo { | |
private final Class<?> clss; | |
private final Bundle args; | |
TabInfo(Class<?> _class, Bundle _args) { | |
clss = _class; | |
args = _args; | |
} | |
} | |
public TabsAdapter(FragmentActivity activity, ActionBar bar, ViewPager pager) { | |
super(activity.getSupportFragmentManager()); | |
mContext = activity; | |
mBar = bar; | |
mViewPager = pager; | |
mViewPager.setAdapter(this); | |
mViewPager.setOnPageChangeListener(this); | |
} | |
public void addTab(ActionBar.Tab tab, Class<? extends Fragment> clss, Bundle args) { | |
TabInfo info = new TabInfo(clss, args); | |
tab.setTag(info); | |
tab.setTabListener(this); | |
mTabs.add(info); | |
mBar.addTab(tab); | |
notifyDataSetChanged(); | |
} | |
@Override | |
public int getCount() { | |
return mTabs.size(); | |
} | |
@Override | |
public Fragment getItem(int position) { | |
TabInfo info = mTabs.get(position); | |
return Fragment.instantiate(mContext, info.clss.getName(), info.args); | |
} | |
@Override | |
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { | |
} | |
@Override | |
public void onPageSelected(int position) { | |
mBar.setSelectedNavigationItem(position); | |
} | |
@Override | |
public void onPageScrollStateChanged(int state) { | |
} | |
@Override | |
public void onTabSelected(Tab tab, FragmentTransaction ft) { | |
Object tag = tab.getTag(); | |
for (int i=0; i<mTabs.size(); i++) { | |
if (mTabs.get(i) == tag) { | |
mViewPager.setCurrentItem(i); | |
} | |
} | |
} | |
@Override | |
public void onTabUnselected(Tab tab, FragmentTransaction ft) { | |
} | |
@Override | |
public void onTabReselected(Tab tab, FragmentTransaction ft) { | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="vertical" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<android.support.v4.view.ViewPager | |
android:id="@+id/pager" | |
android:layout_width="match_parent" | |
android:layout_height="0dp" | |
android:layout_weight="1"/> | |
</LinearLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment