I use ActionbarSherlock and SlidingMenu.
My MainActivity does not access the database and does not analyze what ever. Everything is static.
Tabs are generated dynamically depending on the "Section" that you select from SlidingMenu. After 2 clicks, the application becomes significantly slow.
Below is my main look.
<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>
Code used to manage Tabhost and 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(); } }
source share