我使用 ActionbarSherlock 和 SlidingMenu。
我的 MainActivity 不访问数据库,也不解析任何内容。一切都是静态的。
选项卡会根据您从 SlidingMenu 中选择的“部分”动态生成。单击 2 次后,应用程序变得非常缓慢。
下面是我的主要观点。
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" />
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#0000FF" >
</FrameLayout>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp" >
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
用于管理 Tabhost & Fragments 的代码:
public void selectPage(MenuPageItem page, Page subNavigationPage) {
if (page == null || subNavigationPage == null
|| (page.equals(selectedPage) && subNavigationPage.equals(selectedSubNavigation))) {
return;
}
Log.d(TAG, "Show the page: " + page.getTitle() + " / subNavigationPage: " + subNavigationPage.getTitle());
boolean needUpdateTabUI = !(page.equals(selectedPage));
selectedPage = page;
selectedSubNavigation = subNavigationPage;
// 1) Manage tabHost
if (needUpdateTabUI) {
Log.d(TAG, "need to update tab layout");
updatePageTabUI();
}
// Change selected tab
mTabHost.setCurrentTabByTag(selectedSubNavigation.getTitle());
// 2) Change Contentfragment
Log.d(TAG, "update fragment");
// TODO: check the current fragment is not already the good one and just
// call its updateData method
updatePageFragment();
// Close the sliding menu if opened
if (getSlidingMenu().isShown()) {
Handler h = new Handler();
h.postDelayed(new Runnable() {
public void run() {
getSlidingMenu().showContent();
}
}, 50);
}
}
private void updatePageTabUI() {
if (selectedPage.getPages().size() > 1) {
// More than one sub navigation -> show the tab host
// mTabHost.setVisibility(View.VISIBLE);
mTabHost.setup();
mTabHost.clearAllTabs();
int i = 0;
for (final Page subnav : selectedPage.getPages()) {
Log.d(TAG, "add tab tag=" + subnav.getTitle() + " title=" + subnav.getTitle());
mTabHost.addTab(mTabHost.newTabSpec(subnav.getTitle()).setIndicator(subnav.getTitle())
.setContent(new EmptyTabFactory()));
mTabHost.getTabWidget().getChildAt(i).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
selectPage(selectedPage, subnav);
}
});
i++;
}
} else {
// Only one sub navigation -> hide the tab host
// mTabHost.setVisibility(View.GONE);
mTabHost.setup();
mTabHost.clearAllTabs();
}
mTabHost.requestLayout();
mTabHost.invalidate();
}
private void updatePageFragment() {
try {
PageFragment fragContent = (PageFragment) selectedSubNavigation.getFragmentClass().newInstance();
fragContent.setSubNavigationPage(selectedSubNavigation);
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragContent);
fragmentTransaction.commit();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}