Tab contents using the actionbarsherlock tab style

Referring to this code below (taken from https://gist.github.com/1126843 ), how to set the contents of tabs?

public class NativeTabActivity extends Activity { private TabHost mTabHost; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mTabHost = (TabHost)findViewById(android.R.id.tabhost); mTabHost.setup(); addTab(new TextView(this), "Tab 1"); addTab(new TextView(this), "Tab 2"); addTab(new TextView(this), "Tab 3"); } private void addTab(final View content, final String title) { View tabView = LayoutInflater.from(this).inflate(R.layout.abs__action_bar_tab_layout, null); TextView tv = (TextView) tabView.findViewById(R.id.abs__tab); tv.setText(title); TabSpec setContent = mTabHost.newTabSpec(title).setIndicator(tabView).setContent(new TabContentFactory() { public View createTabContent(String tag) { return content; } }); mTabHost.addTab(setContent); } } 

from the code, it seems I need to put the content in the createTabContent (String tag) view, but how to do it?

+4
source share
2 answers

I use fragments for my implementation of Sherlock.

 public class ActionBarTabs extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActionBar bar = getSupportActionBar(); bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE); bar.addTab(bar.newTab() .setText("Home") .setTabListener(new TabListener<DashBoardFragment>( this, "home", DashBoardFragment.class, null))); bar.addTab(bar.newTab() .setText("Inventory") .setTabListener(new TabListener<InventoryFragment>( this, "inventory", InventoryFragment.class, null))); if (savedInstanceState != null) { bar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0)); } } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: // app icon in action bar clicked; go Location selection Intent intent = new Intent(ActionBarTabs.this, LocationSelectorActivity.class); ActionBarTabs.this.startActivityForResult(intent,0); return true; default: return super.onOptionsItemSelected(item); } } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putInt("tab", getSupportActionBar().getSelectedNavigationIndex()); } public class TabListener<T extends Fragment> implements ActionBar.TabListener { private final FragmentActivity mActivity; private final String mTag; private final Class<T> mClass; private final Bundle mArgs; private Fragment mFragment; public TabListener(FragmentActivity activity, String tag, Class<T> clz, Bundle args) { mActivity = activity; mTag = tag; mClass = clz; mArgs = args; FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction(); // Check to see if we already have a fragment for this tab, probably // from a previously saved state. If so, deactivate it, because our // initial state is that a tab isn't shown. mFragment = mActivity.getSupportFragmentManager().findFragmentByTag(mTag); if (mFragment != null && !mFragment.isDetached()) { ft.detach(mFragment); } } @Override public void onTabSelected(Tab tab) { FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction(); if (mFragment == null) { mFragment = Fragment.instantiate(mActivity, mClass.getName(), mArgs); ft.add(android.R.id.content, mFragment, mTag); ft.commit(); } else { ft.attach(mFragment); ft.commit(); } } @Override public void onTabUnselected(Tab tab) { FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction(); if (mFragment != null) { ft.detach(mFragment); ft.commitAllowingStateLoss(); } } @Override public void onTabReselected(Tab tab) { } } } 

The key change for loading different fragments into tabs is simply to change "YOUR_FRAGMENT_NAME" to the name of your fragment class in these lines:

 bar.addTab(bar.newTab() .setText("Home") .setTabListener(new TabListener<YOUR_FRAGMENT_NAME>( this, "home", YOUR_FRAGMENT_NAME.class, null))); 

Hope this helps!

+8
source

I found Rymnel's answer very useful, but I had to make some changes to make it work with ActionBarSherlock 4.0. I'm having trouble redefining the onTab methods passing in FragmentTransactions, so I just used the default methods and reassigned the "ft" var inside the method. I'm sure there is a cleaner way to do this, but here is my working code:

 public class TabTestActivity extends SherlockFragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActionBar bar = getSupportActionBar(); bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE); bar.setDisplayHomeAsUpEnabled(true); bar.setDisplayShowTitleEnabled(true); bar.setTitle("Activity Title"); bar.addTab(bar .newTab() .setText("Tab 1") .setTabListener( new TabListener<TabTest1>(this, "tab1", TabTest1.class, null))); bar.addTab(bar .newTab() .setText("Tab 2") .setTabListener( new TabListener<TabTest2>(this, "tab2", TabTest2.class, null))); bar.addTab(bar .newTab() .setText("Tab 3") .setTabListener(new TabListener<TabTest3>(this, "tab3", TabTest3.class, null))); if (savedInstanceState != null) { bar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0)); } } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: // app icon in action bar clicked; go home Intent intent = new Intent(this, DashboardActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); return true; default: return super.onOptionsItemSelected(item); } } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putInt("tab", getSupportActionBar() .getSelectedNavigationIndex()); } public class TabListener<T extends Fragment> implements ActionBar.TabListener { private final FragmentActivity mActivity; private final String mTag; private final Class<T> mClass; private final Bundle mArgs; private Fragment mFragment; public TabListener(FragmentActivity activity, String tag, Class<T> clz, Bundle args) { mActivity = activity; mTag = tag; mClass = clz; mArgs = args; FragmentTransaction ft = mActivity.getSupportFragmentManager() .beginTransaction(); // Check to see if we already have a fragment for this tab, probably // from a previously saved state. If so, deactivate it, because our // initial state is that a tab isn't shown. mFragment = mActivity.getSupportFragmentManager() .findFragmentByTag(mTag); if (mFragment != null && !mFragment.isDetached()) { ft.detach(mFragment); } } @Override public void onTabSelected(Tab tab, FragmentTransaction ft) { ft = mActivity.getSupportFragmentManager() .beginTransaction(); if (mFragment == null) { mFragment = Fragment.instantiate(mActivity, mClass.getName(), mArgs); ft.add(android.R.id.content, mFragment, mTag); ft.commit(); } else { ft.attach(mFragment); ft.commit(); } } @Override public void onTabUnselected(Tab tab, FragmentTransaction ft) { ft = mActivity.getSupportFragmentManager() .beginTransaction(); if (mFragment != null) { ft.detach(mFragment); ft.commitAllowingStateLoss(); } } @Override public void onTabReselected(Tab tab, FragmentTransaction ft) { } } 
+14
source

Source: https://habr.com/ru/post/1398480/


All Articles