I am using an ActionBar for my main page navigation which contains four tabs to the different areas in my app.
One of the areas is a maps section, which should contain two sub tabs to show a list of items populated from a SQLite database.
So basically, I would like to implement nested navigation from my page fragments. The code I have so far is:
//MainActivity.java
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setCustomView(R.layout.rowlayout);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout layoutView = (RelativeLayout)inflater.inflate(R.layout.action_bar_tab, null);
TextView tabText = (TextView) layoutView.findViewById(R.id.tabText);
ImageView tabImage = (ImageView) layoutView.findViewById(R.id.tabImage);
String dayOneName = getResources().getString(R.string.day_one);
Tab tab = actionBar.newTab();
tab.setIcon(R.drawable.cal);
TabListener<AgendaMain> dayOne = new TabListener<AgendaMain>(this,
dayOneName, AgendaMain.class);
tab.setTabListener(dayOne);
// set custom view
tabText.setText(dayOneName);
tabImage.setImageResource(R.drawable.cal);
tab.setCustomView(layoutView);
actionBar.addTab(tab);
// Plus three more navigation tabs
private class TabListener<T extends Fragment> implements
ActionBar.TabListener {
private Fragment mFragment;
private final Activity mActivity;
private final String mTag;
private final Class<T> mClass;
public TabListener(Activity activity, String tag, Class<T> clz) {
mActivity = activity;
mTag = tag;
mClass = clz;
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// Check if the fragment is already initialized
if (mFragment == null) {
// If not, instantiate and add it to the activity
mFragment = Fragment.instantiate(mActivity, mClass.getName());
ft.add(android.R.id.content, mFragment, mTag);
} else {
// If it exists, simply attach it in order to show it
ft.attach(mFragment);
}
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
// Detach the fragment, because another one is being attached
ft.detach(mFragment);
}
}
@Override
public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {
// User selected the already selected tab. Usually do nothing.
}
}
And the tabs above link to fragments, in which I want to add two new tabs, with both tabs showing a ListFragment.
I have tried implementing a FragmentTabHost from here, but encounter problems between android.support.v4.app and the TabListener from MainActivity.java. All code is working so far, just trying to add tabs to fragments!
If anyone could suggest/show what the best method is to implement this it would be greatly appreciated (: I have only managed to show static content so far which is no good! (I shall post more of my code if requested, I doubt it would help as I have had no luck!)