0
public class MyTabListener <T extends SherlockFragment> implements TabListener{

private SherlockFragment mFragment;
private final SherlockFragmentActivity mActivity;
private final String mTag;
private final Class<T> mClass;

public MyTabListener(SherlockFragmentActivity mActivity, String mTag, Class<T> mClass) {

    this.mActivity = mActivity;
    this.mTag = mTag;
    this.mClass = mClass;
}

@Override
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 = (SherlockFragment) SherlockFragment.instantiate(
                mActivity, mClass.getName());

        ft.replace(android.R.id.content, mFragment, mTag);
    } else {
        // If it exists, simply attach it in order to show it
        ft.show(mFragment);
    }
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {

    if (mFragment != null) {
        // Detach the fragment, because another one is being attached
        ft.hide(mFragment);
    }
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub

}}

该代码隐藏了片段,但不再显示它们......有什么建议吗?

4

1 回答 1

1

我通过替换解决了它replace()add()所以代码看起来像这样

public class MyTabListener <T extends SherlockFragment> implements TabListener{

private SherlockFragment mFragment;
private final SherlockFragmentActivity mActivity;
private final String mTag;
private final Class<T> mClass;

public MyTabListener(SherlockFragmentActivity mActivity, String mTag, Class<T> mClass) {

    this.mActivity = mActivity;
    this.mTag = mTag;
    this.mClass = mClass;
}

@Override
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 = (SherlockFragment) SherlockFragment.instantiate(
                mActivity, mClass.getName());

        ft.add(android.R.id.content, mFragment, mTag);
    } else {
        // If it exists, simply attach it in order to show it
        ft.show(mFragment);
    }
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {

    if (mFragment != null) {
        // Detach the fragment, because another one is being attached
        ft.hide(mFragment);
    }
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub

}}
于 2013-06-30T21:20:13.883 回答