想要添加带有ActionSherlockBar的Tabs

时间:2013-08-06 11:45:44

标签: android android-actionbar android-tabs

我想用ActionSherlockBar添加Tabs所以我使用了来自某些(resource)的代码并使用了相同的代码,就像我给的那样。但在所有代码之后,我仍然没有得到我想要的东西。即使它没有显示任何类型的错误帮助我 感谢您的建议

TabListener:

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.
  }
}

TabFragement:

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"/>

0 个答案:

没有答案