Android嵌套选项卡导航4.2
我正在使用一个ActionBar进行主页导航,其中包含四个选项卡,指向我的应用程序中的不同区域 其中一个区域是maps部分,它应该包含两个子选项卡,以显示从SQLite数据库填充的项目列表 所以基本上,我想从我的页面片段实现嵌套导航。到目前为止,我掌握的代码是:Android嵌套选项卡导航4.2,android,tabs,Android,Tabs,我正在使用一个ActionBar进行主页导航,其中包含四个选项卡,指向我的应用程序中的不同区域 其中一个区域是maps部分,它应该包含两个子选项卡,以显示从SQLite数据库填充的项目列表 所以基本上,我想从我的页面片段实现嵌套导航。到目前为止,我掌握的代码是: //MainActivity.java ActionBar actionBar = getActionBar(); actionBar.setDisplayShowTitleEnabled(tru
//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.
}
}
//MainActivity.java
ActionBar ActionBar=getActionBar();
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setCustomView(R.layout.rowlayout);
actionBar.setNavigationMode(actionBar.NAVIGATION\u MODE\u选项卡);
LayoutInflater充气器=(LayoutInflater)this.getSystemService(Context.LAYOUT\u充气器\u服务);
RelativeLayout layoutView=(RelativeLayout)充气器充气(R.layout.action\u bar\u选项卡,空);
TextView tabText=(TextView)layoutView.findViewById(R.id.tabText);
ImageView选项卡image=(ImageView)layoutView.findViewById(R.id.tabImage);
String dayOneName=getResources().getString(R.String.day_one);
Tab=actionBar.newTab();
tab.setIcon(R.drawable.cal);
TabListener,但在android.support.v4.app和MainActivity.java中的TabListener之间遇到问题。到目前为止,所有代码都正常工作,只是尝试将选项卡添加到片段中
如果有人能建议/展示实现此功能的最佳方法,我将不胜感激(:到目前为止,我只展示了静态内容,这并不好!)TabHost是一种不推荐使用的解决方案,请使用ViewPager,使用FragmentPagerAdapter或FragmentStatePagerAdapter实现示例如下:
使用ViewPager而不是什么?我有一个操作栏,希望在其中一个部分下使用嵌套的选项卡。例如,“地图”在操作栏中,当用户单击“地图”时,下面会出现两个选项卡。如果可能,我只想要普通选项卡,而不是ViewPager:)哦,对不起。。。然后我没有正确理解你的问题。。。但是你想做的事情不能用标准技术来完成,那么你需要实现一个定制的actionbar解决方案hh ok,谢谢你的帮助!:)我只创建了两行TabHost的嵌套选项卡,但理想情况下希望它下面有一个ActionBar和选项卡,但找不到任何示例:(