我有一个项目,我在其中使用 Android 支持库来包含 3.0 之前的设备的 ActionBar。
我的项目也有标签。标签是Fragments
. 这样,我android.support.v4.app.Fragment
从支持库的类中扩展了每个选项卡。
但是,我对 3.0 之前的设备有疑问。没有显示操作栏。所以,也没有标签。
我找不到解决办法......
所以我的“主要活动”(处理带有选项卡的 ActionBar)看起来像这样:
public class Hauptmenue_extended extends ActionBarActivity implements
OnClickListener {
actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab tabB = actionBar.newTab();
tabB.setText("Home");
tabB.setIcon(R.drawable.icon_home);
tabB.setTabListener(new TabListener<Startmenue_activity>(this, "Start",
Startmenue_activity.class));
actionBar.addTab(tabB);
Tab tabA = actionBar.newTab();
tabA.setText("");
tabA.setIcon(R.drawable.icon_nachrichten_sel);
tabA.setTabListener(new TabListener<Nachrichten_activity>(this,
"Nachrichten", Nachrichten_activity.class));
actionBar.addTab(tabA);
Tab tabC = actionBar.newTab();
tabC.setText("");
tabC.setIcon(R.drawable.icon_favoriten);
tabC.setTabListener(new TabListener<Favoriten_activity>(this,
"Favoriten", Favoriten_activity.class));
actionBar.addTab(tabC);
我的 TabListener 看起来像这样:
public static class TabListener<T extends Fragment> implements
ActionBar.TabListener {
private final FragmentActivity myActivity;
private final String myTag;
private final Class<T> myClass;
public TabListener(FragmentActivity activity, String tag, Class<T> cls) {
myActivity = activity;
myTag = tag;
myClass = cls;
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Fragment myFragment = myActivity.getSupportFragmentManager()
.findFragmentByTag(myTag);
// Check if the fragment is already initialized
if (myFragment == null) {
// If not, instantiate and add it to the activity
myFragment = Fragment
.instantiate(myActivity, myClass.getName());
ft.add(android.R.id.content, myFragment, myTag);
} else {
// If it exists, simply attach it in order to show it
ft.attach(myFragment);
}
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
Fragment myFragment = myActivity.getSupportFragmentManager()
.findFragmentByTag(myTag);
if (myFragment != null) {
// Detach the fragment, because another one is being attached
ft.detach(myFragment);
}
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
每个 Fragment 或 Tab 看起来像这样:
public class Nachrichten_activity extends Fragment implements
OnChildClickListener, OnClickListener { ...
有人有想法吗?:)