1

我的标签:
private void tabSherlock() { actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    ActionBar.Tab tab1 = actionBar.newTab();
    tab1.setText("Home");
    tab1.setTabListener(new TabListener<HomeFragment>(this, "Home",
            HomeFragment.class));
    actionBar.addTab(tab1);

    ActionBar.Tab tab4 = actionBar.newTab();
    tab4.setText("Tab4");
    tab4.setTabListener(new TabListener<BuzzFragment>(this, "Tab4",
            BuzzFragment.class));
    actionBar.addTab(tab4);
}

选项卡监听器:

public class TabListener<T extends Fragment> implements
        ActionBar.TabListener {
    private Fragment mFragment;
    private final SherlockFragmentActivity mActivity;
    private final String mTag;
    private final Class<T> mClass;

    /**
     * Constructor used each time a new tab is created.
     * 
     * @param activity
     *            The host Activity, used to instantiate the fragment
     * @param tag
     *            The identifier tag for the fragment
     * @param clz
     *            The fragment's Class, used to instantiate the fragment
     */
    public TabListener(SherlockFragmentActivity activity, String tag,
            Class<T> clz) {
        mActivity = activity;
        mTag = tag;
        mClass = clz;

        android.support.v4.app.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);
        }

    }

    /* The following are each of the ActionBar.TabListener callbacks */

    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // User selected the already selected tab. Usually do nothing.

        Log.d(TAG, "onTabReselected");
    }

    @Override
    public void onTabSelected(com.actionbarsherlock.app.ActionBar.Tab tab,
            android.support.v4.app.FragmentTransaction ft) {
        Log.d(TAG, "onTabSelected");

        Fragment preInitializedFragment = mActivity
                .getSupportFragmentManager().findFragmentByTag(mTag);
        if (preInitializedFragment == null) {
            mFragment = SherlockFragment.instantiate(mActivity,
                    mClass.getName());
            ft.add(android.R.id.content, mFragment, mTag);
            // ft.commit();
        } else {
            ft.attach(preInitializedFragment);
            // ft.commit();
        }

        // ft = mActivity.getSupportFragmentManager().beginTransaction();
        //
        // if (mFragment == null) {
        // mFragment = Fragment.instantiate(mActivity, mClass.getName());
        // ft.add(android.R.id.content, mFragment, mTag);
        // ft.commit();
        // } else {
        // ft.attach(mFragment);
        // ft.commit();
        // }
    }

    @Override
    public void onTabUnselected(
            com.actionbarsherlock.app.ActionBar.Tab tab,
            android.support.v4.app.FragmentTransaction ft) {

        Log.d(TAG, "onTabUnselected");

        Fragment preInitializedFragment = mActivity
                .getSupportFragmentManager().findFragmentByTag(mTag);

        if (preInitializedFragment != null) {
            ft.detach(preInitializedFragment);
            // ft.commit();
        } else if (mFragment != null) {
            ft.detach(mFragment);
            // ft.commit();
        }

        // ft = mActivity.getSupportFragmentManager().beginTransaction();
        //
        // if (mFragment != null) {
        // ft.detach(mFragment);
        // ft.commitAllowingStateLoss();
        // }
    }

    @Override
    public void onTabReselected(
            com.actionbarsherlock.app.ActionBar.Tab tab,
            android.support.v4.app.FragmentTransaction ft) {

        Log.d(TAG, "onTabReselected");

    }
}

一个选项卡包含项目单击移动到详细信息片段的列表视图:尝试 {

 // FragmentTransaction fragmentTransaction =
 getFragmentManager().beginTransaction();
 // fragmentTransaction.addToBackStack(name)
 android.support.v4.app.FragmentTransaction transaction =
 getSupportFragmentManager()
 .beginTransaction();

 // Replace whatever is in the fragment_container view with this
 // fragment,
 // and add the transaction to the back stack so the user can
 // navigate back
 // transaction.replace(android.R.id.content, newFragment);
 transaction.replace(android.R.id.content, newFragment, "Place");
 transaction.addToBackStack("Place");

 // Commit the transaction
 transaction.commit();
 } catch (Exception e) {

 e.printStackTrace();
 }

如果我点击另一个选项卡视图会从细节片段中重叠,任何人都可以帮助我提前谢谢。

4

0 回答 0