我想用 ActionSherlockBar 添加选项卡所以我使用了一些(资源)中的代码,并使用了与我给出的相同的代码。但毕竟代码我仍然没有得到我想要的。即使它没有显示任何类型的错误帮助我谢谢您的建议
选项卡监听器:
public class TabListener<T extends Fragment> implements ActionBar.TabListener{
private TabFragment 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;
}
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 = (TabFragment) Fragment.instantiate(
mActivity, mClass.getName());
mFragment.setProviderId(mTag); // id for event provider
ft.add(android.R.id.content, mFragment, mTag);
} else {
// If it exists, simply attach it in order to show it
ft.attach(mFragment);
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
// Detach the fragment, because another one is being attached
ft.detach(mFragment);
}
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// User selected the already selected tab. Usually do nothing.
}
}
标签片段:
public class TabFragment extends SherlockFragment {
// your member variables here
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.customer_home, container, false);
// do your view initialization here
return view;
}
public void setProviderId(String mTag) {
// TODO Auto-generated method stub
}
}
Customer_home(工薪阶层):
public class Customer_home extends SherlockFragmentActivity {
// store the active tab here
public static String ACTIVE_TAB = "activeTab";
@Override
public void onCreate(Bundle savedInstanceState) {
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// add tabs
Tab tab1 = actionBar.newTab()
.setText("TabTitle1")
.setTabListener(new TabListener<TabFragment>(
this, "tab1", TabFragment.class));
actionBar.addTab(tab1);
Tab tab2 = actionBar.newTab()
.setText("TabTitle2")
.setTabListener(new TabListener<TabFragment>(
this, "tab2", TabFragment.class));
actionBar.addTab(tab2);
// check if there is a saved state to select active tab
if( savedInstanceState != null ){
getSupportActionBar().setSelectedNavigationItem(
savedInstanceState.getInt(ACTIVE_TAB));
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
// save active tab
outState.putInt(ACTIVE_TAB,
getSupportActionBar().getSelectedNavigationIndex());
super.onSaveInstanceState(outState);
}
}
AndroidMainfest:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.Spajs" >
<activity
android:name=".SplashScreenActivity"
android:noHistory="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"
android:label="@string/app_name"
android:noHistory="true"
android:windowSoftInputMode="stateAlwaysHidden"></activity>
<activity android:name=".Login"
android:noHistory="true"
android:windowSoftInputMode="stateAlwaysHidden"
android:theme="@style/Theme.Sherlock.Dialog">
</activity>
<activity android:name=".Customer_home"
android:windowSoftInputMode="stateAlwaysHidden"/>
<activity android:name=".TabFragment"/>
<activity android:name=".TabListener"/>